Skip to content

Commit

Permalink
Fixes #31, Allows for checking multiple metrics at once (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsdeBlaauw committed May 12, 2019
1 parent b60fcbf commit 4bc3fee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ Options:
[default: text]
-o, --reportFile <arg> Send report output to a file
-m, --metric <arg> Metric to use for determining
complexity [cognitive, cyclomatic,
complexity
[metrics.complexity.cognitive,
metrics.complexity.cyclomatic,
metrics.deprecated.category,
metrics.deprecated.subpackage,
metrics.complexity.length]
[default: cognitive]
[default:
metrics.complexity.cognitive]
-w, --complexity-warning-threshold <arg> Cyclomatic complexity score which
is the lower bound for a warning
[default: 4]
Expand Down
31 changes: 21 additions & 10 deletions src/AnalysableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class AnalysableFile implements \JsonSerializable
public $groupManager;
protected $parser;
protected $metrics = array(
'cognitive' => '\NdB\PhpDocCheck\Metrics\CognitiveComplexity',
'cognitive' => '\NdB\PhpDocCheck\Metrics\CognitiveComplexity', // @deprecated
'cyclomatic' => '\NdB\PhpDocCheck\Metrics\CyclomaticComplexity', // @deprecated
'metrics.deprecated.category' => '\NdB\PhpDocCheck\Metrics\CategoryDeprecated',
'metrics.deprecated.subpackage' => '\NdB\PhpDocCheck\Metrics\SubpackageDeprecated',
'metrics.complexity.length' => '\NdB\PhpDocCheck\Metrics\FunctionLength',
'cyclomatic' => '\NdB\PhpDocCheck\Metrics\CyclomaticComplexity'
'metrics.complexity.cognitive' => '\NdB\PhpDocCheck\Metrics\CognitiveComplexity',
'metrics.complexity.cyclomatic' => '\NdB\PhpDocCheck\Metrics\CognitiveComplexity',
);

public function __construct(
Expand All @@ -30,6 +32,9 @@ public function __construct(
$this->groupManager = $groupManager;
}

/**
* Analyses the file for all requested metrics.
*/
public function analyse() : AnalysisResult
{
$analysisResult = new AnalysisResult($this);
Expand All @@ -47,21 +52,27 @@ public function analyse() : AnalysisResult
return $analysisResult;
}
$traverser = new \PhpParser\NodeTraverser();
$metricSlug = 'cognitive';
if (array_key_exists($this->arguments->getOption('metric'), $this->metrics)) {
$metricSlug = $this->arguments->getOption('metric');
}
$metric = new $this->metrics[$metricSlug];

$traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver);
$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)
);
foreach ($this->arguments->getOption('metric') as $metricSlug) {
if (array_key_exists($metricSlug, $this->metrics)) {
$metric = new $this->metrics[$metricSlug];
$traverser->addVisitor(
new \NdB\PhpDocCheck\NodeVisitors\MetricChecker(
$analysisResult,
$this,
$metric,
$this->groupManager
)
);
}
}
$traverser->traverse($statements);
return $analysisResult;
}
Expand Down
10 changes: 5 additions & 5 deletions src/ApplicationArgumentsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public function __construct()
\GetOpt\Option::create('o', 'reportFile', \GetOpt\GetOpt::REQUIRED_ARGUMENT)
->setDescription('Send report output to a file')
->setDefaultValue(''),
\GetOpt\Option::create('m', 'metric', \GetOpt\GetOpt::REQUIRED_ARGUMENT)
\GetOpt\Option::create('m', 'metric', \GetOpt\GetOpt::MULTIPLE_ARGUMENT)
->setDescription(
'Metric to use for determining complexity [cognitive, cyclomatic, '.
'metrics.deprecated.category, metrics.deprecated.subpackage, metrics.complexity.length] '.
'[default: cognitive]'
'Metric to use for determining complexity [metrics.complexity.cognitive, '.
'metrics.complexity.cyclomatic, metrics.deprecated.category, metrics.deprecated.subpackage, '.
'metrics.complexity.length] [default: metrics.complexity.cognitive]'
)
->setDefaultValue('cognitive'),
->setDefaultValue('metrics.complexity.cognitive'),
\GetOpt\Option::create('w', 'complexity-warning-threshold', \GetOpt\GetOpt::REQUIRED_ARGUMENT)
->setDescription('Cyclomatic complexity score which is the lower bound for a warning [default: 4]')
->setDefaultValue(4)->setValidation('is_numeric'),
Expand Down

0 comments on commit 4bc3fee

Please sign in to comment.