Skip to content

Commit 10f74b5

Browse files
added check dependencies for properties (#308)
1 parent 325d35e commit 10f74b5

4 files changed

Lines changed: 48 additions & 6 deletions

File tree

src/Analyzer/FileVisitor.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ public function enterNode(Node $node): void
134134
}
135135
}
136136

137+
/**
138+
* matches parameters dependency in property definitions like
139+
* public NotBlank $foo;.
140+
*
141+
* @see FileVisitorTest::test_it_parse_typed_property
142+
*/
143+
if ($node instanceof Node\Stmt\Property && null !== $this->classDescriptionBuilder) {
144+
if (null === $node->type) {
145+
return;
146+
}
147+
148+
if (!method_exists($node->type, 'toString')) {
149+
return;
150+
}
151+
152+
$this->classDescriptionBuilder->addDependency(new ClassDependency($node->type->toString(), $node->getLine()));
153+
}
154+
137155
if (null !== $this->classDescriptionBuilder && null !== $node->getDocComment()) {
138156
/** @var Doc $docComment */
139157
$docComment = $node->getDocComment();

tests/E2E/_fixtures/mvc/Services/CartService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace App\Services;
55

6-
use ContainerAwareInterface;
7-
8-
class CartService implements ContainerAwareInterface
6+
class CartService implements \ContainerAwareInterface
97
{
108
}

tests/E2E/_fixtures/mvc/Services/Foo/Bar.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace App\Services\Foo;
55

6-
use ContainerAwareInterface;
7-
8-
class Bar implements ContainerAwareInterface
6+
class Bar implements \ContainerAwareInterface
97
{
108
}

tests/Unit/Analyzer/FileVisitorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,32 @@ public function getFoo(): int
523523

524524
$this->assertCount(2, $violations);
525525
}
526+
527+
public function test_it_parse_typed_property(): void
528+
{
529+
$code = <<< 'EOF'
530+
<?php
531+
namespace MyProject\AppBundle\Application;
532+
533+
use Symfony\Component\Validator\Constraints\NotBlank;
534+
535+
class ApplicationLevelDto
536+
{
537+
public NotBlank $foo;
538+
}
539+
EOF;
540+
541+
/** @var FileParser $fp */
542+
$fp = FileParserFactory::createFileParser(TargetPhpVersion::create('8.1'));
543+
$fp->parse($code, 'relativePathName');
544+
545+
$cd = $fp->getClassDescriptions();
546+
547+
$violations = new Violations();
548+
549+
$notHaveDependencyOutsideNamespace = new DependsOnlyOnTheseNamespaces('MyProject\AppBundle\Application');
550+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
551+
552+
$this->assertCount(1, $violations);
553+
}
526554
}

0 commit comments

Comments
 (0)