Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ You can add parameters when you launch the tool. At the moment you can add these
phparkitect check --config=/project/yourConfigFile.php
```
* `--target-php-version`: With this parameter, you can specify which PHP version should use the parser. This can be useful to debug problems and to understand if there are problems with a different PHP version.
Supported PHP versions are: 7.4, 8.0, 8.1, 8.2 8.3
Supported PHP versions are: 7.4, 8.0, 8.1, 8.2, 8.3, 8.4, 8.5
* `--stop-on-failure`: With this option the process will end immediately after the first violation.
* `--autoload`: specify the path of an autoload file to be loaded when running phparkitect.

Expand Down
15 changes: 15 additions & 0 deletions src/Analyzer/FileVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function enterNode(Node $node): void

// handles attribute definition like #[MyAttribute]
$this->handleAttributeNode($node);

// handles property hooks like public string $name { get => ...; set { ... } }
$this->handlePropertyHookNode($node);
}

public function getClassDescriptions(): array
Expand Down Expand Up @@ -349,4 +352,16 @@ private function addParamDependency(Node\Param $node): void
$this->classDescriptionBuilder
->addDependency(new ClassDependency($type->toString(), $node->getLine()));
}

private function handlePropertyHookNode(Node $node): void
{
if (!($node instanceof Node\PropertyHook)) {
return;
}

// Handle parameters in set hooks (e.g., set(MyClass $value) { ... })
foreach ($node->params as $param) {
$this->addParamDependency($param);
}
}
}
71 changes: 71 additions & 0 deletions tests/Unit/Analyzer/FileParser/CanParsePropertyHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,75 @@ public function __construct(string $firstName, string $lastName) {

self::assertInstanceOf(ClassDescription::class, $cd[0]);
}

public function test_it_collects_dependencies_from_property_hooks(): void
{
$code = <<< 'EOF'
<?php
namespace App\Foo;

use App\Services\Formatter;
use App\Services\Validator;
use App\Services\Logger;

class User {
public string $name {
get {
$formatter = new Formatter();
return $formatter->format($this->name);
}
set {
$validator = new Validator();
$validator->validate($value);
$this->name = $value;
Logger::log('Name set');
}
}
}
EOF;

$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_4);
$fp->parse($code, 'relativePathName');

$cd = $fp->getClassDescriptions();

self::assertInstanceOf(ClassDescription::class, $cd[0]);

$dependencies = $cd[0]->getDependencies();
$dependencyNames = array_map(fn ($dep) => $dep->getFQCN()->toString(), $dependencies);

self::assertContains('App\Services\Formatter', $dependencyNames);
self::assertContains('App\Services\Validator', $dependencyNames);
self::assertContains('App\Services\Logger', $dependencyNames);
}

public function test_it_collects_dependencies_from_property_hook_parameters(): void
{
$code = <<< 'EOF'
<?php
namespace App\Foo;

use App\ValueObjects\Name;

class User {
public string $name {
set (Name $name) {
$this->name = $name->toString();
}
}
}
EOF;

$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_8_4);
$fp->parse($code, 'relativePathName');

$cd = $fp->getClassDescriptions();

self::assertInstanceOf(ClassDescription::class, $cd[0]);

$dependencies = $cd[0]->getDependencies();
$dependencyNames = array_map(fn ($dep) => $dep->getFQCN()->toString(), $dependencies);

self::assertContains('App\ValueObjects\Name', $dependencyNames);
}
}