Skip to content

Commit

Permalink
Fixes #40, category is deprecated warning now added (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsdeBlaauw committed Feb 13, 2019
1 parent c0262fb commit 65c19f6
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 32 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"require": {
"nikic/php-parser": "^4.2",
"wp-cli/php-cli-tools": "^0.11.11",
"ulrichsg/getopt-php": "^3.2"
"ulrichsg/getopt-php": "^3.2",
"phpdocumentor/reflection-docblock": "^4.3"
},
"bin": [
"bin/php-doc-check"
Expand Down
5 changes: 4 additions & 1 deletion src/AnalysableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AnalysableFile implements \JsonSerializable
protected $parser;
protected $metrics = array(
'cognitive' => '\NdB\PhpDocCheck\Metrics\CognitiveComplexity',
'metrics.deprecated.category' => '\NdB\PhpDocCheck\Metrics\CategoryDeprecated',
'cyclomatic' => '\NdB\PhpDocCheck\Metrics\CyclomaticComplexity'
);

Expand All @@ -34,7 +35,6 @@ public function analyse() : AnalysisResult
$statements = $this->parser->parse(file_get_contents($this->file->getRealPath()));
} catch (\PhpParser\Error $e) {
$finding = new \NdB\PhpDocCheck\Findings\Error(
sprintf('Failed parsing: %s', $e->getRawMessage()),
new InvalidFileNode,
$this,
new \NdB\PhpDocCheck\Metrics\InvalidFile(),
Expand All @@ -54,6 +54,9 @@ public function analyse() : AnalysisResult
$traverser->addVisitor(
new \NdB\PhpDocCheck\NodeVisitors\ParentConnector()
);
$traverser->addVisitor(
new \NdB\PhpDocCheck\NodeVisitors\DocParser()
);
$traverser->addVisitor(
new \NdB\PhpDocCheck\NodeVisitors\MetricChecker($analysisResult, $this, $metric, $this->groupManager)
);
Expand Down
5 changes: 1 addition & 4 deletions src/Findings/Finding.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

abstract class Finding implements \JsonSerializable, Groupable, \NdB\PhpDocCheck\Sortable
{
public $message;
public $node;
public $sourceFile;
public $metric;
public $value;

public function __construct(
string $message,
\PhpParser\Node $node,
\NdB\PhpDocCheck\AnalysableFile $sourceFile,
\NdB\PhpDocCheck\Metrics\Metric $metric,
int $value
) {
$this->message = $message;
$this->node = $node;
$this->sourceFile = $sourceFile;
$this->metric = $metric;
Expand Down Expand Up @@ -56,7 +53,7 @@ public function getLine():int

public function getMessage():string
{
return $this->message;
return sprintf($this->metric->getMessage(), $this->node->getAttribute('FQSEN'), $this->value);
}

abstract public function getType():string;
Expand Down
37 changes: 37 additions & 0 deletions src/Metrics/CategoryDeprecated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace NdB\PhpDocCheck\Metrics;

final class CategoryDeprecated implements Metric
{
public $value = 0;

public function getMessage():string
{
return '%1$s has a @category tag, which is deprecated. '.
'It is recommended to use the @package tag\'s ability '.
'to provide multiple levels.';
}

public function getName():string
{
return 'metrics.deprecated.category';
}

public function getValue(\PhpParser\Node $node):int
{
$docBlock = $node->getAttribute('DocBlock');
if (!empty($docBlock) && $docBlock->hasTag('category')) {
return 4;
}
return 0;
}

public function jsonSerialize() : array
{
return array(
'name'=>$this->getName(),
'value'=>$this->value
);
}
}
8 changes: 8 additions & 0 deletions src/Metrics/CognitiveComplexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ final class CognitiveComplexity implements Metric
'Expr_Ternary',
);

public function getMessage():string
{
return '%1$s has no documentation and a cognitive complexity of %2$d';
}

public function getName():string
{
return 'metrics.complexity.cognitive';
}

public function getValue(\PhpParser\Node $node):int
{
if (!empty($node->getDocComment())) {
return 0;
}
return $this->calculateNodeValue($node, 0);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Metrics/CyclomaticComplexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ final class CyclomaticComplexity implements Metric
'Expr_Ternary',
);

public function getMessage():string
{
return '%1$s has no documentation and a cyclomatic complexity of %2$d';
}

public function getName():string
{
Expand All @@ -29,6 +33,9 @@ public function getName():string

public function getValue(\PhpParser\Node $node):int
{
if (!empty($node->getDocComment())) {
return 0;
}
return $this->calculateNodeValue($node) + 1;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Metrics/InvalidFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ final class InvalidFile implements Metric
{
public $value = 0;

public function getMessage():string
{
return 'Failed parsing file';
}

public function getName():string
{
return 'metrics.files.invalid';
Expand Down
1 change: 1 addition & 0 deletions src/Metrics/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ interface Metric extends \JsonSerializable
{
public function getName() : string;
public function getValue(\PhpParser\Node $node) : int;
public function getMessage():string;
}
20 changes: 20 additions & 0 deletions src/NodeVisitors/DocParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace NdB\PhpDocCheck\NodeVisitors;

class DocParser extends \PhpParser\NodeVisitorAbstract
{
/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function leaveNode(\PhpParser\Node $node)
{
try {
if (!empty($node->getDocComment())) {
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
$node->setAttribute('DocBlock', $factory->create($node->getDocComment()->__toString()));
}
} catch (\Exception $e) {
}
}
}
41 changes: 19 additions & 22 deletions src/NodeVisitors/MetricChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,25 @@ public function leaveNode(\PhpParser\Node $node)
}
}
$node->setAttribute('FQSEN', $name);
if (empty($node->getDocComment())) {
if ($metricValue >= $this->arguments->getOption('complexity-error-threshold')) {
$finding = new \NdB\PhpDocCheck\Findings\Error(
sprintf("%s has no documentation and a complexity of %d", $name, $metricValue),
$node,
$this->sourceFile,
$this->metric,
$metricValue
);
$this->analysisResult->addProgress($finding);
$this->groupManager->addFinding($finding);
} elseif ($metricValue >= $this->arguments->getOption('complexity-warning-threshold')) {
$finding = new \NdB\PhpDocCheck\Findings\Warning(
sprintf("%s has no documentation and a complexity of %d", $name, $metricValue),
$node,
$this->sourceFile,
$this->metric,
$metricValue
);
$this->analysisResult->addProgress($finding);
$this->groupManager->addFinding($finding);
}

if ($metricValue >= $this->arguments->getOption('complexity-error-threshold')) {
$finding = new \NdB\PhpDocCheck\Findings\Error(
$node,
$this->sourceFile,
$this->metric,
$metricValue
);
$this->analysisResult->addProgress($finding);
$this->groupManager->addFinding($finding);
} elseif ($metricValue >= $this->arguments->getOption('complexity-warning-threshold')) {
$finding = new \NdB\PhpDocCheck\Findings\Warning(
$node,
$this->sourceFile,
$this->metric,
$metricValue
);
$this->analysisResult->addProgress($finding);
$this->groupManager->addFinding($finding);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions tests/AnalysisResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function testAlwaysStartsClean()
public function testChangesStateWhenAnWarningIsAdded($analysisResult)
{
$finding = new \NdB\PhpDocCheck\Findings\Warning(
"Basic warning",
$this->node,
$this->analysableFile,
$this->metric,
Expand All @@ -42,7 +41,6 @@ public function testChangesStateWhenAnWarningIsAdded($analysisResult)
public function testChangesStateWhenAnErrorIsAdded($analysisResult)
{
$finding = new \NdB\PhpDocCheck\Findings\Error(
"Basic error",
$this->node,
$this->analysableFile,
$this->metric,
Expand Down
4 changes: 2 additions & 2 deletions tests/Output/Formats/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public function testCanOutputSimpleResults()
$channel = $this->createMock(\NdB\PhpDocCheck\Output\Channels\Channel::class);
$channel->expects($this->once())
->method('out')
->with($this->stringContains('Basic warning'));
->with($this->stringContains('Severity'));
$formatter = new Text(array($channel));
$results = $this->createMock(\NdB\PhpDocCheck\ResultGroup::class);
$metric = $this->createMock('\NdB\PhpDocCheck\Metrics\Metric');
$analysableFile = $this->createMock('\NdB\PhpDocCheck\AnalysableFile');
$node = $this->createMock('\PhpParser\Node');
$results->method('getFindings')->willReturn(array(
new \NdB\PhpDocCheck\Findings\Warning("Basic warning", $node, $analysableFile, $metric, 0)
new \NdB\PhpDocCheck\Findings\Warning($node, $analysableFile, $metric, 0)
));
$results->sourceFile = $this->createMock(\NdB\PhpDocCheck\AnalysableFile::class);
$results->sourceFile->file = $this->createMock(\SplFileInfo::class);
Expand Down

0 comments on commit 65c19f6

Please sign in to comment.