/** * @param AbstractNode|MethodNode $node */ public function apply(AbstractNode $node) { $switchStatements = $node->findChildrenOfType('SwitchStatement'); foreach ($switchStatements as $switchStatement) { $this->addViolation($switchStatement); } }
/** * Extracts all variable and variable declarator nodes from the given node * and checks the variable name length against the configured maximum * length. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { $this->resetProcessed(); if ($node->getType() === 'class') { $fields = $node->findChildrenOfType('FieldDeclaration'); foreach ($fields as $field) { if ($field->isPrivate()) { continue; } $declarators = $field->findChildrenOfType('VariableDeclarator'); foreach ($declarators as $declarator) { $this->checkNodeImage($declarator); } } } else { $declarators = $node->findChildrenOfType('VariableDeclarator'); foreach ($declarators as $declarator) { $this->checkNodeImage($declarator); } $variables = $node->findChildrenOfType('Variable'); foreach ($variables as $variable) { $this->checkNodeImage($variable); } } $this->resetProcessed(); }
/** * @param AbstractNode|MethodNode $node */ public function apply(AbstractNode $node) { $conditionalExpressions = $node->findChildrenOfType('ConditionalExpression'); foreach ($conditionalExpressions as $conditionalExpression) { $this->addViolation($conditionalExpression); } }
/** * Extracts all constant declarations from the given node and tests that * the image only contains upper case characters. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('ConstantDeclarator') as $declarator) { if ($declarator->getImage() !== strtoupper($declarator->getImage())) { $this->addViolation($declarator, array($declarator->getImage())); } } }
/** * This method checks if a superglobal is used * and emits a rule violation. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('Variable') as $variable) { if (in_array($variable->getImage(), $this->superglobals)) { $this->addViolation($node, array($node->getName(), $variable->getImage())); } } }
/** * This method extracts all statements of a given type for the given function or method node. * * @param AbstractNode $node * @param string $statementType */ private function collectStatements(AbstractNode $node, $statementType) { $statements = $node->findChildrenOfType($statementType); foreach ($statements as $statement) { if (!$this->isAtEndOfMainScope($statement, $node) && !$this->isAtEndOfIfStatement($statement) && !$this->isAtEndOfSwitchStatement($statement) && !$this->isAtEndOfTry($statement) && !$this->isAtEndOfCatch($statement) && !$this->isAtEndOfFinally($statement) && !$this->isAtEndOfClosure($statement)) { $this->nodes[] = $statement; } } }
/** * Method checks for use of static access and warns about it. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { $nodes = $node->findChildrenOfType('MemberPrimaryPrefix'); foreach ($nodes as $methodCall) { if (!$this->isStaticMethodCall($methodCall)) { continue; } $this->addViolation($methodCall, array($methodCall->getImage(), $methodCall->getImage())); } }
private function collectLocalVariables(AbstractNode $node, array &$localVariables) { foreach ($node->findChildrenOfType(self::VARIABLE_CHILDREN_TYPE) as $variable) { if ($this->isLocal($variable)) { $localVariables[$variable->getImage()]['node'] = $variable; $localVariables[$variable->getImage()]['usage'][] = $variable->getEndLine(); } } return $localVariables; }
/** * @param AbstractNode|MethodNode $node */ public function apply(AbstractNode $node) { $countTry = count($node->findChildrenOfType('TryStatement')); if (1 < $countTry) { $this->addViolation($node); } if (1 === $countTry && true === $this->hasNotAllowedChildren($node)) { $this->addViolation($node); } }
/** * This method checks if a method/function has boolean flag arguments and warns about them. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('FormalParameter') as $param) { $declarator = $param->getFirstChildOfType('VariableDeclarator'); $value = $declarator->getValue(); if (false === $this->isBooleanValue($value)) { continue; } $this->addViolation($param, array($node->getImage(), $declarator->getImage())); } }
/** * This method checks if a variable is not named in camelCase * and emits a rule violation. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('Variable') as $variable) { $image = $variable->getImage(); if (in_array($image, $this->exceptions)) { continue; } if (preg_match('/^\\$[a-z][a-zA-Z0-9]*$/', $image)) { continue; } $this->addViolation($node, array($image)); } }
/** * This method checks if a method/function uses an else expression and add a violation for each one found. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('ScopeStatement') as $scope) { $parent = $scope->getParent(); if (false === $this->isIfOrElseIfStatement($parent)) { continue; } if (false === $this->isElseScope($scope, $parent)) { continue; } $this->addViolation($scope, array($node->getImage())); } }
/** * @param AbstractNode|MethodNode $node */ public function apply(AbstractNode $node) { $classReferences = $node->findChildrenOfType('ClassReference'); if ('__construct' !== $node->getImage() || 0 === count($classReferences)) { return; } $allowedClassNames = explode($this->getStringProperty('delimiter'), $this->getStringProperty('allowedClassNames')); foreach ($classReferences as $classReference) { if (false === $this->containsClassName($classReference->getImage(), $allowedClassNames)) { $this->addViolation($classReference); } } }
/** * This method checks if a given function or method contains an eval-expression * and emits a rule violation when it exists. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('FunctionPostfix') as $postfix) { $image = strtolower($postfix->getImage()); if (false === in_array($image, $this->getSuspectImages())) { continue; } $image = $node->getImage(); if ($node instanceof MethodNode) { $image = sprintf('%s::%s', $node->getParentName(), $node->getImage()); } $this->addViolation($postfix, array($node->getType(), $image, $postfix->getImage())); } }
/** * Method checks for use of static access and warns about it. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { $exceptions = $this->getExceptionsList(); $nodes = $node->findChildrenOfType('MemberPrimaryPrefix'); foreach ($nodes as $methodCall) { if (!$this->isStaticMethodCall($methodCall)) { continue; } $className = $methodCall->getChild(0)->getNode()->getImage(); if (in_array($className, $exceptions)) { continue; } $this->addViolation($methodCall, array($className, $node->getName())); } }
/** * This method collects all local variables in the body of the currently * analyzed method or function and removes those parameters that are * referenced by one of the collected variables. * * @param \PHPMD\AbstractNode $node * @return void */ private function removeUsedParameters(AbstractNode $node) { $variables = $node->findChildrenOfType('Variable'); foreach ($variables as $variable) { if ($this->isRegularVariable($variable)) { unset($this->nodes[$variable->getImage()]); } } /* If the method calls func_get_args() then all parameters are * automatically referenced */ $functionCalls = $node->findChildrenOfType('FunctionPostfix'); foreach ($functionCalls as $functionCall) { if ($this->isFunctionNameEqual($functionCall, 'func_get_args')) { $this->nodes = array(); } if ($this->isFunctionNameEndingWith($functionCall, 'compact')) { foreach ($functionCall->findChildrenOfType('Literal') as $literal) { unset($this->nodes['$' . trim($literal->getImage(), '"\'')]); } } } }
/** * This method checks if a given function or method contains an exit-expression * and emits a rule violation when it exists. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('ExitExpression') as $exit) { $this->addViolation($exit, array($node->getType(), $node->getName())); } }
/** * This method should implement the violation analysis algorithm of concrete * rule implementations. All extending classes must implement this method. * * @param \PHPMD\AbstractNode $node * @return void */ public function apply(AbstractNode $node) { foreach ($node->findChildrenOfType('GotoStatement') as $goto) { $this->addViolation($goto, array($node->getType(), $node->getName())); } }