Esempio n. 1
0
 /**
  * Extracts all variable and variable declarator nodes from the given node
  * and checks the variable name length against the configured maximum
  * length.
  *
  * @param PHP_PMD_AbstractNode $node The context source code node.
  *
  * @return void
  */
 public function apply(PHP_PMD_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();
 }
 /**
  * Extracts all constant declarations from the given node and tests that
  * the image only contains upper case characters.
  *
  * @param PHP_PMD_AbstractNode $node The context source code node.
  *
  * @return void
  */
 public function apply(PHP_PMD_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 variable is not named in camelCase
  * and emits a rule violation.
  *
  * @param PHP_PMD_AbstractNode $node The context source code node.
  *
  * @return void
  */
 public function apply(PHP_PMD_AbstractNode $node)
 {
     foreach ($node->findChildrenOfType('Variable') as $variable) {
         if (!preg_match('/^\\$[a-z][a-zA-Z0-9]*$/', $variable->getImage())) {
             $this->addViolation($node, array($variable->getImage()));
         }
     }
 }
 /**
  * This method checks if a superglobal is used
  * and emits a rule violation.
  *
  * @param PHP_PMD_AbstractNode $node The context source code node.
  *
  * @return void
  */
 public function apply(PHP_PMD_AbstractNode $node)
 {
     foreach ($node->findChildrenOfType('Variable') as $variable) {
         if (in_array($variable->getImage(), $this->superglobals)) {
             $this->addViolation($node, array($node->getName(), $variable->getImage()));
         }
     }
 }
Esempio n. 5
0
 /**
  * Method checks for use of static access and warns about it.
  *
  * @param PHP_PMD_AbstractNode $node The context source code node.
  *
  * @return void
  */
 public function apply(PHP_PMD_AbstractNode $node)
 {
     $nodes = $node->findChildrenOfType('MemberPrimaryPrefix');
     foreach ($nodes as $methodCall) {
         if (!$this->isStaticMethodCall($methodCall)) {
             continue;
         }
         $this->addViolation($methodCall, array($methodCall->getImage(), $methodCall->getImage()));
     }
 }
 public function apply(PHP_PMD_AbstractNode $node)
 {
     $staticReferences = $node->findChildrenOfType('ClassOrInterfaceReference');
     foreach ($staticReferences as $reference) {
         if ($this->isReferenceInParameter($reference)) {
             continue;
         }
         $this->addViolation($reference, array($reference->getImage(), $node->getImage()));
     }
 }
 public function apply(PHP_PMD_AbstractNode $node)
 {
     foreach ($node->findChildrenOfType('FormalParameter') as $param) {
         $declarator = $param->getFirstChildOfType('VariableDeclarator');
         $value = $declarator->getValue();
         if (!$this->isBooleanValue($value)) {
             continue;
         }
         $this->addViolation($param, array($node->getImage(), $declarator->getImage()));
     }
 }
 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()));
     }
 }
 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()));
     }
 }
Esempio n. 10
0
 /**
  * This method checks if a given function or method contains an eval-expression
  * and emits a rule violation when it exists.
  *
  * @param PHP_PMD_AbstractNode $node The context source code node.
  *
  * @return void
  */
 public function apply(PHP_PMD_AbstractNode $node)
 {
     foreach ($node->findChildrenOfType('EvalExpression') as $eval) {
         $this->addViolation($eval, array($node->getType(), $node->getName()));
     }
 }
Esempio n. 11
0
 /**
  * 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 PHP_PMD_AbstractNode $node The context function or method instance.
  *
  * @return void
  */
 private function _removeUsedParameters(PHP_PMD_AbstractNode $node)
 {
     $variables = $node->findChildrenOfType('Variable');
     foreach ($variables as $variable) {
         if ($this->isLocal($variable)) {
             unset($this->_nodes[$variable->getImage()]);
         }
     }
 }
Esempio n. 12
0
 /**
  * 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 PHP_PMD_AbstractNode $node The context function or method instance.
  *
  * @return void
  */
 private function _removeUsedParameters(PHP_PMD_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 ($functionCall->getImage() == 'func_get_args') {
             $this->_nodes = array();
         }
     }
 }
 /**
  * This method should implement the violation analysis algorithm of concrete
  * rule implementations. All extending classes must implement this method.
  *
  * @param PHP_PMD_AbstractNode $node The current context for analysis.
  *
  * @return void
  */
 public function apply(PHP_PMD_AbstractNode $node)
 {
     foreach ($node->findChildrenOfType('GotoStatement') as $goto) {
         $this->addViolation($goto, array($node->getType(), $node->getName()));
     }
 }