Skip to content

Commit

Permalink
Fixes #39, better names in messages and output (#50)
Browse files Browse the repository at this point in the history
* Fixes #39, better names in messages and output

* Uses correct name in json otput
  • Loading branch information
NielsdeBlaauw authored Feb 10, 2019
1 parent ee37a81 commit c0262fb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
11 changes: 10 additions & 1 deletion src/AnalysableFile.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace NdB\PhpDocCheck;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AnalysableFile implements \JsonSerializable
{
public $file;
Expand Down Expand Up @@ -47,7 +50,13 @@ public function analyse() : AnalysisResult
$metricSlug = $this->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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Findings/Finding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
);
Expand Down
20 changes: 14 additions & 6 deletions src/NodeVisitor.php → src/NodeVisitors/MetricChecker.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace NdB\PhpDocCheck;
namespace NdB\PhpDocCheck\NodeVisitors;

use \PhpParser\Node;
use \PhpParser\Node\Stmt;

class NodeVisitor extends \PhpParser\NodeVisitorAbstract
class MetricChecker extends \PhpParser\NodeVisitorAbstract
{
public $analysisResult;
protected $arguments;
Expand All @@ -14,10 +14,10 @@ class NodeVisitor extends \PhpParser\NodeVisitorAbstract
protected $groupManager;

public function __construct(
AnalysisResult &$analysisResult,
AnalysableFile $file,
\NdB\PhpDocCheck\AnalysisResult &$analysisResult,
\NdB\PhpDocCheck\AnalysableFile $file,
\NdB\PhpDocCheck\Metrics\Metric $metric,
GroupManager $groupManager
\NdB\PhpDocCheck\GroupManager $groupManager
) {
$this->analysisResult =& $analysisResult;
$this->sourceFile = $file;
Expand All @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions src/NodeVisitors/ParentConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace NdB\PhpDocCheck\NodeVisitors;

class ParentConnector extends \PhpParser\NodeVisitorAbstract
{
private $stack;

/**
* @inheritdoc
*/
public function beforeTraverse(array $nodes)
{
$this->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);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace NdB\PhpDocCheck;
namespace NdB\PhpDocCheck\NodeVisitors;

final class NodeVisitorTest extends \PHPUnit\Framework\TestCase
final class MetricCheckerTest extends \PHPUnit\Framework\TestCase
{
public function testCanAnalyseNodesForWarningFindings()
{
$analysisResult = $this->createMock(AnalysisResult::class);
$analysisResult = $this->createMock(\NdB\PhpDocCheck\AnalysisResult::class);
$analysisResult->expects($this->once())
->method('addProgress')
->with($this->isInstanceOf(\NdB\PhpDocCheck\Findings\Warning::class));
Expand All @@ -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));
Expand All @@ -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);
}
}

0 comments on commit c0262fb

Please sign in to comment.