예제 #1
0
 /**
  * @param AbstractNode|MethodNode $node
  */
 public function apply(AbstractNode $node)
 {
     $switchStatements = $node->findChildrenOfType('SwitchStatement');
     foreach ($switchStatements as $switchStatement) {
         $this->addViolation($switchStatement);
     }
 }
예제 #2
0
 /**
  * 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)
 {
     $cbo = $node->getMetric('cbo');
     if ($cbo >= ($threshold = $this->getIntProperty('minimum'))) {
         $this->addViolation($node, array($node->getName(), $cbo, $threshold));
     }
 }
예제 #3
0
 /**
  * @param AbstractNode|MethodNode $node
  */
 public function apply(AbstractNode $node)
 {
     $conditionalExpressions = $node->findChildrenOfType('ConditionalExpression');
     foreach ($conditionalExpressions as $conditionalExpression) {
         $this->addViolation($conditionalExpression);
     }
 }
예제 #4
0
 /**
  * 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()));
         }
     }
 }
 private function removeParameters(AbstractNode $node, array &$localVariables)
 {
     $parameters = $node->getFirstChildOfType('FormalParameters');
     $declarators = $parameters->findChildrenOfType('VariableDeclarator');
     foreach ($declarators as $declarator) {
         unset($localVariables[$declarator->getImage()]);
     }
 }
 /**
  * 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()));
         }
     }
 }
예제 #7
0
 /**
  * This method checks if a parameter is not named in camelCase
  * and emits a rule violation.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     foreach ($node->getParameters() as $parameter) {
         if (!preg_match('/^\\$[a-z][a-zA-Z0-9]*$/', $parameter->getName())) {
             $this->addViolation($node, array($parameter->getName()));
         }
     }
 }
 public function apply(AbstractNode $node)
 {
     $filename = $node->getFileName();
     $base = basename($filename, '.php');
     if (!preg_match('/^(SS_)?' . $base . '(_[A-Z][a-zA-Z0-9]+)?$/', $node->getName())) {
         $this->addViolation($node, array($node->getName(), basename($filename)));
     }
 }
예제 #9
0
 /**
  * Extracts all variable and variable declarator nodes from the given node
  * and checks the variable name length against the configured minimum
  * length.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $threshold = $this->getIntProperty('minimum');
     if ($threshold <= strlen($node->getName())) {
         return;
     }
     $this->addViolation($node, array($node->getParentName(), $node->getName(), $threshold));
 }
예제 #10
0
 /**
  * This method checks the weighted method count for the given class against
  * a configured threshold.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $threshold = $this->getIntProperty('maximum');
     $actual = $node->getMetric('wmc');
     if ($actual >= $threshold) {
         $this->addViolation($node, array($node->getName(), $actual, $threshold));
     }
 }
 public function apply(AbstractNode $node)
 {
     foreach ($node->getProperties() as $property) {
         if (!$property->isStatic() && !preg_match('/^\\$[a-zA-Z][a-zA-Z0-9]*$/', $property->getName())) {
             $this->addViolation($node, array($property->getName()));
         }
     }
 }
예제 #12
0
 /**
  * This method checks the number of classes derived from the given class
  * node.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $nocc = $node->getMetric('nocc');
     $threshold = $this->getIntProperty('minimum');
     if ($nocc >= $threshold) {
         $this->addViolation($node, array($node->getType(), $node->getName(), $nocc, $threshold));
     }
 }
예제 #13
0
 /**
  * This method checks the number of arguments for the given function or method
  * node against a configured threshold.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $threshold = $this->getIntProperty('minimum');
     $count = $node->getParameterCount();
     if ($count < $threshold) {
         return;
     }
     $this->addViolation($node, array($node->getType(), $node->getName(), $count, $threshold));
 }
예제 #14
0
 /**
  * Constructs a new collection instance.
  *
  * @param \PHPMD\AbstractNode $node
  */
 public function __construct(\PHPMD\AbstractNode $node)
 {
     preg_match_all($this->regexp, $node->getDocComment(), $matches);
     foreach (array_keys($matches[0]) as $i) {
         $name = $matches[1][$i];
         $value = trim($matches[2][$i], '" ');
         $this->annotations[] = new Annotation($name, $value);
     }
 }
예제 #15
0
 /**
  * @param AbstractNode|AbstractASTArtifact $node
  *
  * @return float
  */
 private function calculateNameToCommentSimilarityInPercent($node)
 {
     $docComment = $node->getDocComment();
     if (empty($docComment)) {
         return 0;
     }
     similar_text($this->transformString($node->getName()), $this->getCommentDescription($docComment), $percent);
     return round($percent);
 }
 /**
  * @param AbstractNode|MethodNode $node
  */
 public function apply(AbstractNode $node)
 {
     $meaninglessNames = $this->getStringProperty('meaninglessNames');
     $delimiter = $this->getStringProperty('delimiter');
     $methodName = $node->getImage();
     if (in_array(strtolower($methodName), explode($delimiter, strtolower($meaninglessNames)))) {
         $this->addViolation($node, [$methodName, $meaninglessNames]);
     }
 }
 /**
  * @param AbstractNode $methodPostfix
  *
  * @return bool
  */
 private function isMethodPostfixContainsNames(AbstractNode $methodPostfix)
 {
     foreach ($this->names as $name) {
         if (false !== strpos($methodPostfix->getImage(), $name)) {
             return true;
         }
     }
     return false;
 }
 /**
  * This method checks if a method is not named in camelCase
  * and emits a rule violation.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $methodName = $node->getName();
     if (!in_array($methodName, $this->ignoredMethods)) {
         if (!$this->isValid($methodName)) {
             $this->addViolation($node, array($methodName));
         }
     }
 }
예제 #19
0
 /**
  * This method checks the number of methods with in a given class and checks
  * this number against a configured threshold.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $threshold = $this->getIntProperty('maxfields');
     $vars = $node->getMetric('vars');
     if ($vars <= $threshold) {
         return;
     }
     $this->addViolation($node, array($node->getType(), $node->getName(), $vars, $threshold));
 }
예제 #20
0
 /**
  * This method checks the number of public fields and methods in the given
  * class and checks that value against a configured threshold.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $threshold = $this->getIntProperty('minimum');
     $cis = $node->getMetric('cis');
     if ($cis < $threshold) {
         return;
     }
     $this->addViolation($node, array($node->getType(), $node->getName(), $cis, $threshold));
 }
예제 #21
0
 /**
  * This method checks the cyclomatic complexity for the given node against
  * a configured threshold.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $threshold = $this->getIntProperty('reportLevel');
     $ccn = $node->getMetric('ccn2');
     if ($ccn < $threshold) {
         return;
     }
     $this->addViolation($node, array($node->getType(), $node->getName(), $ccn, $threshold));
 }
예제 #22
0
 /**
  * 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()));
     }
 }
 /**
  * Is method has the same name as the enclosing class
  * (php4 style constructor).
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     if ($node->getNode()->getParent() instanceof ASTTrait) {
         return;
     }
     if (strcasecmp($node->getName(), $node->getParentName()) !== 0) {
         return;
     }
     $this->addViolation($node);
 }
 /**
  * @param AbstractNode|ClassNode $node
  */
 public function apply(AbstractNode $node)
 {
     $suffixes = $this->getStringProperty('suffixes');
     $generalSuffixes = explode($this->getStringProperty('delimiter'), $suffixes);
     foreach ($generalSuffixes as $generalSuffix) {
         if ($generalSuffix === substr($node->getImage(), strlen($generalSuffix) * -1)) {
             $this->addViolation($node, [$suffixes, $generalSuffix]);
             break;
         }
     }
 }
예제 #25
0
 /**
  * @param AbstractNode $node
  *
  * @return bool
  */
 private function isChildOfTry(AbstractNode $node)
 {
     $parent = $node->getParent();
     while (is_object($parent)) {
         if ($parent->isInstanceOf('TryStatement')) {
             return true;
         }
         $parent = $parent->getParent();
     }
     return false;
 }
예제 #26
0
 /**
  * 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()));
     }
 }
예제 #27
0
 /**
  * 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()));
     }
 }
예제 #28
0
 /**
  * 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));
     }
 }
예제 #29
0
 /**
  * This method checks the number of public methods with in a given class and checks
  * this number against a configured threshold.
  *
  * @param \PHPMD\AbstractNode $node
  * @return void
  */
 public function apply(AbstractNode $node)
 {
     $this->ignoreRegexp = $this->getStringProperty('ignorepattern');
     $threshold = $this->getIntProperty('maxmethods');
     if ($node->getMetric('npm') <= $threshold) {
         return;
     }
     $nom = $this->countMethods($node);
     if ($nom <= $threshold) {
         return;
     }
     $this->addViolation($node, array($node->getType(), $node->getName(), $nom, $threshold));
 }
 /**
  * @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);
         }
     }
 }