Skip to content

Commit

Permalink
Fixes #32, Adds function length metric
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsdeBlaauw committed May 12, 2019
1 parent eda475d commit 725cd87
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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
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 725cd87

Please sign in to comment.