/usr/share/php/PHP/PMD/Rule/CleanCode/NestedScopes.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 49 50 51 | <?php
require_once 'PHP/PMD/AbstractRule.php';
require_once 'PHP/PMD/Rule/IMethodAware.php';
require_once 'PHP/PMD/Rule/IFunctionAware.php';
/**
* Check if there are nested if or loop scopes in a method.
*
* The strict rule of this type would disallow nested scopes
* of any kind, however this is not always possible for all
* idioms in a performant way. This is why loop => if or if => loop
* are allowed, however never two loops or two if statements.
*/
class PHP_PMD_Rule_CleanCode_NestedScopes
extends PHP_PMD_AbstractRule
implements PHP_PMD_Rule_IMethodAware,
PHP_PMD_Rule_IFunctionAware
{
public function apply(PHP_PMD_AbstractNode $node)
{
return; // not good enough, if, elseif,... nest each other and get detected by this rule :(
foreach ($node->findChildrenOfType('ScopeStatement') as $scope) {
if ( ! $this->isNestedScope($scope)) {
continue;
}
$this->addViolation($scope, array($node->getImage()));
}
}
private function isNestedScope($scope)
{
$scopeParent = $scope->getParent();
$searchNode = $scopeParent;
while ($searchNode = $searchNode->getParent()) {
if ($this->isSameScopeType($scopeParent, $searchNode)) {
return true;
}
}
return false;
}
private function isSameScopeType($scopeParent, $searchNode)
{
return $scopeParent->getName() === $searchNode->getName();
}
}
|