Skip to content

Commit 7ccf61b

Browse files
getting dependencies if we call a static constant (#271)
1 parent 65f0af5 commit 7ccf61b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Analyzer/FileVisitor.php

+14
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ public function enterNode(Node $node): void
6161
}
6262
}
6363

64+
/**
65+
* adding static classes as dependencies
66+
* $constantValue = StaticClass::constant;.
67+
*
68+
* @see FileVisitorTest::test_it_should_return_errors_for_const_outside_namespace
69+
*/
70+
if ($node instanceof Node\Expr\ClassConstFetch &&
71+
method_exists($node->class, 'toString') &&
72+
null !== $this->classDescriptionBuilder
73+
) {
74+
$this->classDescriptionBuilder
75+
->addDependency(new ClassDependency($node->class->toString(), $node->getLine()));
76+
}
77+
6478
/**
6579
* adding static function classes as dependencies
6680
* $static = StaticClass::foo();.

tests/Unit/Analyzer/FileVisitorTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,33 @@ class Foo {}
380380
$cd[0]->getAttributes()
381381
);
382382
}
383+
384+
public function test_it_should_return_errors_for_const_outside_namespace(): void
385+
{
386+
$code = <<< 'EOF'
387+
<?php
388+
namespace Root\Cars;
389+
use AnotherNamespace\CarMake;
390+
class KiaSportage extends AbstractCar
391+
{
392+
public function __construct()
393+
{
394+
parent::__construct(CarMake::KIA, 'Sportage');
395+
}
396+
}
397+
EOF;
398+
399+
/** @var FileParser $fp */
400+
$fp = FileParserFactory::createFileParser(TargetPhpVersion::create('7.4'));
401+
$fp->parse($code, 'relativePathName');
402+
403+
$cd = $fp->getClassDescriptions();
404+
405+
$violations = new Violations();
406+
407+
$notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace('Root\Cars');
408+
$notHaveDependencyOutsideNamespace->evaluate($cd[0], $violations, 'we want to add this rule for our software');
409+
410+
$this->assertCount(1, $violations);
411+
}
383412
}

0 commit comments

Comments
 (0)