From 99b15619dffecccf78286285723134e443ad0987 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sun, 29 Sep 2024 09:42:49 +0200 Subject: [PATCH] Implement Class_::file() --- src/StaticAnalysis/CodeUnitFindingVisitor.php | 15 +++++++++++++++ src/StaticAnalysis/ParsingFileAnalyser.php | 2 +- src/StaticAnalysis/Value/Class_.php | 17 ++++++++++++++++- .../CodeUnitFindingVisitorTest.php | 2 +- tests/tests/StaticAnalysis/Value/ClassTest.php | 6 ++++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/StaticAnalysis/CodeUnitFindingVisitor.php b/src/StaticAnalysis/CodeUnitFindingVisitor.php index b6082cb54..ac9d93a1e 100644 --- a/src/StaticAnalysis/CodeUnitFindingVisitor.php +++ b/src/StaticAnalysis/CodeUnitFindingVisitor.php @@ -34,6 +34,11 @@ */ final class CodeUnitFindingVisitor extends NodeVisitorAbstract { + /** + * @var non-empty-string + */ + private string $file; + /** * @var array */ @@ -54,6 +59,14 @@ final class CodeUnitFindingVisitor extends NodeVisitorAbstract */ private array $functions = []; + /** + * @param non-empty-string $file + */ + public function __construct(string $file) + { + $this->file = $file; + } + public function enterNode(Node $node): void { if ($node instanceof Interface_) { @@ -109,6 +122,7 @@ public function leaveNode(Node $node): void $this->classes[$namespacedClassName]->name(), $this->classes[$namespacedClassName]->namespacedName(), $this->classes[$namespacedClassName]->namespace(), + $this->classes[$namespacedClassName]->file(), $this->classes[$namespacedClassName]->startLine(), $this->classes[$namespacedClassName]->endLine(), $this->classes[$namespacedClassName]->parentClass(), @@ -274,6 +288,7 @@ private function processClass(Class_ $node): void $name, $namespacedName, $this->namespace($namespacedName, $name), + $this->file, $node->getStartLine(), $node->getEndLine(), $parentClass, diff --git a/src/StaticAnalysis/ParsingFileAnalyser.php b/src/StaticAnalysis/ParsingFileAnalyser.php index 3608fdc39..1c9f556b0 100644 --- a/src/StaticAnalysis/ParsingFileAnalyser.php +++ b/src/StaticAnalysis/ParsingFileAnalyser.php @@ -172,7 +172,7 @@ private function analyse(string $filename): void assert($nodes !== null); $traverser = new NodeTraverser; - $codeUnitFindingVisitor = new CodeUnitFindingVisitor; + $codeUnitFindingVisitor = new CodeUnitFindingVisitor($filename); $lineCountingVisitor = new LineCountingVisitor($linesOfCode); $ignoredLinesFindingVisitor = new IgnoredLinesFindingVisitor($this->useAnnotationsForIgnoringCode, $this->ignoreDeprecatedCode); $executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source); diff --git a/src/StaticAnalysis/Value/Class_.php b/src/StaticAnalysis/Value/Class_.php index 48b85b42a..c558011a2 100644 --- a/src/StaticAnalysis/Value/Class_.php +++ b/src/StaticAnalysis/Value/Class_.php @@ -25,6 +25,11 @@ private string $namespacedName; private string $namespace; + /** + * @var non-empty-string + */ + private string $file; + /** * @var non-negative-int */ @@ -58,6 +63,7 @@ /** * @param non-empty-string $name * @param non-empty-string $namespacedName + * @param non-empty-string $file * @param non-negative-int $startLine * @param non-negative-int $endLine * @param ?non-empty-string $parentClass @@ -65,11 +71,12 @@ * @param list $traits * @param array $methods */ - public function __construct(string $name, string $namespacedName, string $namespace, int $startLine, int $endLine, ?string $parentClass, array $interfaces, array $traits, array $methods) + public function __construct(string $name, string $namespacedName, string $namespace, string $file, int $startLine, int $endLine, ?string $parentClass, array $interfaces, array $traits, array $methods) { $this->name = $name; $this->namespacedName = $namespacedName; $this->namespace = $namespace; + $this->file = $file; $this->startLine = $startLine; $this->endLine = $endLine; $this->parentClass = $parentClass; @@ -104,6 +111,14 @@ public function namespace(): string return $this->namespace; } + /** + * @return non-empty-string + */ + public function file(): string + { + return $this->file; + } + /** * @return non-negative-int */ diff --git a/tests/tests/StaticAnalysis/CodeUnitFindingVisitorTest.php b/tests/tests/StaticAnalysis/CodeUnitFindingVisitorTest.php index ba7f45ffb..b5fccd411 100644 --- a/tests/tests/StaticAnalysis/CodeUnitFindingVisitorTest.php +++ b/tests/tests/StaticAnalysis/CodeUnitFindingVisitorTest.php @@ -264,7 +264,7 @@ private function findCodeUnits(string $filename): CodeUnitFindingVisitor assert($nodes !== null); $traverser = new NodeTraverser; - $codeUnitFindingVisitor = new CodeUnitFindingVisitor; + $codeUnitFindingVisitor = new CodeUnitFindingVisitor($filename); $traverser->addVisitor(new NameResolver); $traverser->addVisitor(new ParentConnectingVisitor); diff --git a/tests/tests/StaticAnalysis/Value/ClassTest.php b/tests/tests/StaticAnalysis/Value/ClassTest.php index e47e8385a..bd5313010 100644 --- a/tests/tests/StaticAnalysis/Value/ClassTest.php +++ b/tests/tests/StaticAnalysis/Value/ClassTest.php @@ -32,6 +32,11 @@ public function testHasNamespaced(): void $this->assertSame('example', $this->class()->namespace()); } + public function testHasFile(): void + { + $this->assertSame('file.php', $this->class()->file()); + } + public function testHasStartLine(): void { $this->assertSame(1, $this->class()->startLine()); @@ -81,6 +86,7 @@ private function class(?string $parentClass = null, array $interfaces = [], arra 'Example', 'example\Example', 'example', + 'file.php', 1, 2, $parentClass,