diff --git a/.travis.yml b/.travis.yml index 959203f..990b973 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/composer.json b/composer.json index 0c33cc7..79da27d 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ }, "require-dev": { "squizlabs/php_codesniffer": "^3.4", - "kherge/box": "~2.0" + "kherge/box": "~2.0", + "phpmd/phpmd": "^2.6" } } diff --git a/src/AnalysableFile.php b/src/AnalysableFile.php index 78e3db0..0267e50 100644 --- a/src/AnalysableFile.php +++ b/src/AnalysableFile.php @@ -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) @@ -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() @@ -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'; diff --git a/src/NodeVisitor.php b/src/NodeVisitor.php index f92d6c8..238781d 100644 --- a/src/NodeVisitor.php +++ b/src/NodeVisitor.php @@ -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; @@ -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() @@ -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++; diff --git a/src/Output/AbstractOutput.php b/src/Output/AbstractOutput.php index bf9a303..e66671e 100644 --- a/src/Output/AbstractOutput.php +++ b/src/Output/AbstractOutput.php @@ -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; } }