Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #32, Adds function length metric #53

Merged
merged 2 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
}
}