diff --git a/src/AnalysableFile.php b/src/AnalysableFile.php index 4ee9245..fc316fd 100644 --- a/src/AnalysableFile.php +++ b/src/AnalysableFile.php @@ -1,6 +1,9 @@ arguments->getOption('metric'); } $metric = new $this->metrics[$metricSlug]; - $traverser->addVisitor(new \NdB\PhpDocCheck\NodeVisitor($analysisResult, $this, $metric, $this->groupManager)); + $traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver); + $traverser->addVisitor( + new \NdB\PhpDocCheck\NodeVisitors\ParentConnector() + ); + $traverser->addVisitor( + new \NdB\PhpDocCheck\NodeVisitors\MetricChecker($analysisResult, $this, $metric, $this->groupManager) + ); $traverser->traverse($statements); return $analysisResult; } diff --git a/src/Findings/Finding.php b/src/Findings/Finding.php index 5a8e587..8abd99c 100644 --- a/src/Findings/Finding.php +++ b/src/Findings/Finding.php @@ -78,7 +78,7 @@ public function jsonSerialize() : array 'metric'=> $this->metric, 'sourceFile'=> $this->sourceFile, 'node'=> array( - 'name' => isset($this->node->name)?$this->node->name:false, + 'name' => $this->node->getAttribute('FQSEN'), 'getStartLine' => $this->getLine() ) ); diff --git a/src/NodeVisitor.php b/src/NodeVisitors/MetricChecker.php similarity index 75% rename from src/NodeVisitor.php rename to src/NodeVisitors/MetricChecker.php index f6003bf..9283693 100644 --- a/src/NodeVisitor.php +++ b/src/NodeVisitors/MetricChecker.php @@ -1,11 +1,11 @@ analysisResult =& $analysisResult; $this->sourceFile = $file; @@ -37,8 +37,16 @@ public function leaveNode(\PhpParser\Node $node) $name = 'Anonymous function'; if (\property_exists($node, 'name')) { - $name = $node->name; + $parent = $node->getAttribute('parent'); + if (!empty($parent) && \property_exists($parent, 'namespacedName')) { + $name = $parent->namespacedName . '::'; + $name .= $node->name . '()'; + } + if (\property_exists($node, 'namespacedName')) { + $name = $node->namespacedName . '()'; + } } + $node->setAttribute('FQSEN', $name); if (empty($node->getDocComment())) { if ($metricValue >= $this->arguments->getOption('complexity-error-threshold')) { $finding = new \NdB\PhpDocCheck\Findings\Error( diff --git a/src/NodeVisitors/ParentConnector.php b/src/NodeVisitors/ParentConnector.php new file mode 100644 index 0000000..bfca581 --- /dev/null +++ b/src/NodeVisitors/ParentConnector.php @@ -0,0 +1,31 @@ +stack = []; + } + public function enterNode(\PhpParser\Node $node) + { + if (!empty($this->stack)) { + $node->setAttribute('parent', $this->stack[count($this->stack)-1]); + } + $this->stack[] = $node; + } + + /** + * @inheritdoc + */ + public function leaveNode(\PhpParser\Node $node) + { + array_pop($this->stack); + } +} diff --git a/tests/NodeVisitorTest.php b/tests/NodeVisitors/MetricCheckerTest.php similarity index 73% rename from tests/NodeVisitorTest.php rename to tests/NodeVisitors/MetricCheckerTest.php index 1240fc6..2fd5767 100644 --- a/tests/NodeVisitorTest.php +++ b/tests/NodeVisitors/MetricCheckerTest.php @@ -1,12 +1,12 @@ createMock(AnalysisResult::class); + $analysisResult = $this->createMock(\NdB\PhpDocCheck\AnalysisResult::class); $analysisResult->expects($this->once()) ->method('addProgress') ->with($this->isInstanceOf(\NdB\PhpDocCheck\Findings\Warning::class)); @@ -17,14 +17,14 @@ public function testCanAnalyseNodesForWarningFindings() $metric = $this->createMock(\NdB\PhpDocCheck\Metrics\Metric::class); $metric->method('getValue')->willReturn(4); $groupManager = new \NdB\PhpDocCheck\GroupManager('none', 'natural'); - $nodeVisitor = new NodeVisitor($analysisResult, $analysableFile, $metric, $groupManager); + $metricChecker = new MetricChecker($analysisResult, $analysableFile, $metric, $groupManager); $node = $this->createMock(\PhpParser\Node\Stmt\Function_::class); - $nodeVisitor->leaveNode($node); + $metricChecker->leaveNode($node); } public function testCanAnalyseNodesForErrorFindings() { - $analysisResult = $this->createMock(AnalysisResult::class); + $analysisResult = $this->createMock(\NdB\PhpDocCheck\AnalysisResult::class); $analysisResult->expects($this->once()) ->method('addProgress') ->with($this->isInstanceOf(\NdB\PhpDocCheck\Findings\Error::class)); @@ -35,8 +35,8 @@ public function testCanAnalyseNodesForErrorFindings() $metric = $this->createMock(\NdB\PhpDocCheck\Metrics\Metric::class); $metric->method('getValue')->willReturn(9); $groupManager = $this->createMock(\NdB\PhpDocCheck\GroupManager::class); - $nodeVisitor = new NodeVisitor($analysisResult, $analysableFile, $metric, $groupManager); + $metricChecker = new MetricChecker($analysisResult, $analysableFile, $metric, $groupManager); $node = $this->createMock(\PhpParser\Node\Stmt\Function_::class); - $nodeVisitor->leaveNode($node); + $metricChecker->leaveNode($node); } }