/usr/share/php/PHP/PMD/Rule/CleanCode/ElseExpression.php is in phpmd 1.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php
require_once 'PHP/PMD/AbstractRule.php';
require_once 'PHP/PMD/Rule/IMethodAware.php';
require_once 'PHP/PMD/Rule/IFunctionAware.php';
/**
* Check if there is an else expression somewhere in the method/function and
* warn about it.
*
* Object Calisthenics teaches us, that an else expression can always be
* avoided by simple guard clause or return statements.
*/
class PHP_PMD_Rule_CleanCode_ElseExpression
extends PHP_PMD_AbstractRule
implements PHP_PMD_Rule_IMethodAware,
PHP_PMD_Rule_IFunctionAware
{
public function apply(PHP_PMD_AbstractNode $node)
{
foreach ($node->findChildrenOfType('ScopeStatement') as $scope) {
$parent = $scope->getParent();
if ( ! $this->isIfOrElseIfStatement($parent)) {
continue;
}
if ( ! $this->isElseScope($scope, $parent)) {
continue;
}
$this->addViolation($scope, array($node->getImage()));
}
}
private function isElseScope($scope, $parent)
{
return (
count($parent->getChildren()) === 3 &&
$scope->getNode() === $parent->getChild(2)->getNode()
);
}
private function isIfOrElseIfStatement($parent)
{
return ($parent->getName() === "if" || $parent->getName() === "elseif");
}
}
|