Skip to content

Commit

Permalink
Fixes #16, Implements PHP Mess Detector (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsdeBlaauw authored Jan 26, 2019
1 parent 382bfbf commit ab0967e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ install:
script:
- composer validate
- vendor/bin/phpcs
- "vendor/bin/phpmd ./ text cleancode,codesize,controversial,design,naming,unusedcode --exclude vendor/"
- "./bin/php-doc-check -d ./ --exclude vendor/"
- ls .travis/
- vendor/bin/box build
- "php ./bin/php-doc-check.phar -d ./ --exclude vendor/"
before_deploy:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.4",
"kherge/box": "~2.0"
"kherge/box": "~2.0",
"phpmd/phpmd": "^2.6"
}
}
10 changes: 5 additions & 5 deletions src/AnalysableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

class AnalysableFile
{
public $has_errors = false;
public $has_warnings = false;
public $hasErrors = false;
public $hasWarnings = false;
public $findings = array();

public function __construct(\SplFileInfo $file, \PhpParser\Parser $parser, $arguments)
Expand All @@ -19,7 +19,7 @@ public function analyse()
try {
$statements = $this->parser->parse(file_get_contents($this->file->getRealPath()));
} catch (\PhpParser\Error $e) {
$this->has_errors = true;
$this->hasErrors = true;
$this->findings[] = new \NdB\PhpDocCheck\Findings\Error(
sprintf('Failed parsing: %s', $e->getRawMessage()),
$e->getStartLine()
Expand All @@ -37,9 +37,9 @@ public function analyse()
*/
public function getProgressIndicator() : string
{
if (!$this->has_warnings && !$this->has_errors) {
if (!$this->hasWarnings && !$this->hasErrors) {
return '.';
} elseif (!$this->has_errors) {
} elseif (!$this->hasErrors) {
return 'W';
}
return 'E';
Expand Down
42 changes: 23 additions & 19 deletions src/NodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

class NodeVisitor extends \PhpParser\NodeVisitorAbstract
{
const COMPLEX_NODES = array(
'PhpParser\Node\Stmt\If_',
'PhpParser\Node\Stmt\ElseIf_',
'PhpParser\Node\Stmt\For_',
'PhpParser\Node\Stmt\Foreach_',
'PhpParser\Node\Stmt\While_',
'PhpParser\Node\Stmt\Do_',
'PhpParser\Node\Expr\BinaryOp\LogicalAnd',
'PhpParser\Node\Expr\BinaryOp\LogicalOr',
'PhpParser\Node\Expr\BinaryOp\LogicalXor',
'PhpParser\Node\Expr\BinaryOp\BooleanAnd',
'PhpParser\Node\Expr\BinaryOp\BooleanOr',
'PhpParser\Node\Stmt\Catch_',
'PhpParser\Node\Expr\Ternary',
'PhpParser\Node\Expr\BinaryOp\Coalesce',
);

public function __construct(AnalysableFile &$file)
{
$this->file =& $file;
Expand All @@ -27,13 +44,13 @@ public function leaveNode(\PhpParser\Node $node)
}
if (empty($node->getDocComment())) {
if ($methodCcn >= $this->file->arguments['complexity-error-treshold']) {
$this->file->has_errors = true;
$this->file->hasErrors = true;
$this->file->findings[] = new \NdB\PhpDocCheck\Findings\Error(
sprintf("%s has no documentation and a complexity of %d", $name, $methodCcn),
$node->getStartLine()
);
} elseif ($methodCcn >= $this->file->arguments['complexity-warning-treshold']) {
$this->file->has_warnings = true;
$this->file->hasWarnings = true;
$this->file->findings[] = new \NdB\PhpDocCheck\Findings\Warning(
sprintf("%s has no documentation and a complexity of %d", $name, $methodCcn),
$node->getStartLine()
Expand All @@ -49,30 +66,17 @@ public function leaveNode(\PhpParser\Node $node)
protected function calculateComplexity($node)
{
$ccn = 0;
foreach (get_object_vars($node) as $name => $member) {
foreach (get_object_vars($node) as $member) {
foreach (is_array($member) ? $member : [$member] as $memberItem) {
if ($memberItem instanceof Node) {
$ccn += $this->calculateComplexity($memberItem);
}
}
}
if (in_array(get_class($node), self::COMPLEX_NODES)) {
$ccn++;
}
switch (true) {
case $node instanceof Stmt\If_:
case $node instanceof Stmt\ElseIf_:
case $node instanceof Stmt\For_:
case $node instanceof Stmt\Foreach_:
case $node instanceof Stmt\While_:
case $node instanceof Stmt\Do_:
case $node instanceof Node\Expr\BinaryOp\LogicalAnd:
case $node instanceof Node\Expr\BinaryOp\LogicalOr:
case $node instanceof Node\Expr\BinaryOp\LogicalXor:
case $node instanceof Node\Expr\BinaryOp\BooleanAnd:
case $node instanceof Node\Expr\BinaryOp\BooleanOr:
case $node instanceof Stmt\Catch_:
case $node instanceof Node\Expr\Ternary:
case $node instanceof Node\Expr\BinaryOp\Coalesce:
$ccn++;
break;
case $node instanceof Stmt\Case_: // include default
if ($node->cond !== null) { // exclude default
$ccn++;
Expand Down
2 changes: 1 addition & 1 deletion src/Output/AbstractOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function display()
public function getExitCode()
{
foreach ($this->files as $file) {
if ($file->has_errors || $file->has_warnings) {
if ($file->hasErrors || $file->hasWarnings) {
return 1;
}
}
Expand Down

0 comments on commit ab0967e

Please sign in to comment.