Skip to content

Commit

Permalink
Fixes #32, Adds function length metric (#53)
Browse files Browse the repository at this point in the history
* Fixes #32, Adds function length metric

* Adds documentation
  • Loading branch information
NielsdeBlaauw authored May 12, 2019
1 parent eda475d commit b60fcbf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Options:
-m, --metric <arg> Metric to use for determining
complexity [cognitive, cyclomatic,
metrics.deprecated.category,
metrics.deprecated.subpackage]
metrics.deprecated.subpackage,
metrics.complexity.length]
[default: cognitive]
-w, --complexity-warning-threshold <arg> Cyclomatic complexity score which
is the lower bound for a warning
Expand Down
1 change: 1 addition & 0 deletions bin/php-doc-check
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $lexer = new \PhpParser\Lexer\Emulative(
array(
'usedAttributes' => array(
'startLine',
'endLine',
'comments',
),
)
Expand Down
1 change: 1 addition & 0 deletions src/AnalysableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AnalysableFile implements \JsonSerializable
'cognitive' => '\NdB\PhpDocCheck\Metrics\CognitiveComplexity',
'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'
);

Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationArgumentsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct()
\GetOpt\Option::create('m', 'metric', \GetOpt\GetOpt::REQUIRED_ARGUMENT)
->setDescription(
'Metric to use for determining complexity [cognitive, cyclomatic, '.
'metrics.deprecated.category, metrics.deprecated.subpackage] '.
'metrics.deprecated.category, metrics.deprecated.subpackage, metrics.complexity.length] '.
'[default: cognitive]'
)
->setDefaultValue('cognitive'),
Expand Down
36 changes: 36 additions & 0 deletions src/Metrics/FunctionLength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace NdB\PhpDocCheck\Metrics;

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

const LINES_PER_POINT = 5;

public function getMessage():string
{
return '%1$s has a length score of %2$d. It is recommended to document long functions.';
}

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

public function getValue(\PhpParser\Node $node):int
{
if (!empty($node->getDocComment())) {
return 0;
}
return ( $node->getEndLine() - $node->getStartLine() ) / self::LINES_PER_POINT;
}

public function jsonSerialize() : array
{
return array(
'name'=>$this->getName(),
'value'=>$this->value
);
}
}

0 comments on commit b60fcbf

Please sign in to comment.