Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 13 | class ReferenceMatcher extends AbstractMatcher |
||
| 14 | { |
||
| 15 | //const MATCH_REFERENCE = 'reference'; |
||
|
|
|||
| 16 | |||
| 17 | protected $allowedConditions = array( |
||
| 18 | self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
||
| 19 | //self::MATCH_REFERENCE |
||
| 20 | ); |
||
| 21 | |||
| 22 | // since this is used for recursive calls as well, we have to unlock it for now |
||
| 23 | /// @todo allow this to be set in a more flexible way |
||
| 24 | protected $maxConditions = 0; |
||
| 25 | |||
| 26 | protected $validator; |
||
| 27 | /** @var ReferenceResolverInterface $referenceResolver */ |
||
| 28 | protected $referenceResolver; |
||
| 29 | |||
| 30 | /// @todo add more operators |
||
| 31 | static protected $operatorsMap = array( |
||
| 32 | 'eq' => '\Symfony\Component\Validator\Constraints\EqualTo', |
||
| 33 | 'gt' => '\Symfony\Component\Validator\Constraints\GreaterThan', |
||
| 34 | 'gte' => '\Symfony\Component\Validator\Constraints\GreaterThanOrEqual', |
||
| 35 | 'lt' => '\Symfony\Component\Validator\Constraints\LessThan', |
||
| 36 | 'lte' => '\Symfony\Component\Validator\Constraints\LessThanOrEqual', |
||
| 37 | 'ne' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
||
| 38 | |||
| 39 | 'count' => '\Symfony\Component\Validator\Constraints\Count', |
||
| 40 | 'length' => '\Symfony\Component\Validator\Constraints\Length', |
||
| 41 | 'regex' => '\Symfony\Component\Validator\Constraints\Regex', |
||
| 42 | //'in' => Operator::IN, |
||
| 43 | //'between' => Operator::BETWEEN, => use count/length with min & max sub-members |
||
| 44 | //'like' => Operator::LIKE, => use regex |
||
| 45 | //'contains' => Operator::CONTAINS, |
||
| 46 | Operator::EQ => '\Symfony\Component\Validator\Constraints\EqualTo', |
||
| 47 | Operator::GT => '\Symfony\Component\Validator\Constraints\GreaterThan', |
||
| 48 | Operator::GTE => '\Symfony\Component\Validator\Constraints\GreaterThanOrEqual', |
||
| 49 | Operator::LT => '\Symfony\Component\Validator\Constraints\LessThan', |
||
| 50 | Operator::LTE => '\Symfony\Component\Validator\Constraints\LessThanOrEqual', |
||
| 51 | '!=' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
||
| 52 | '<>' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
||
| 53 | ); |
||
| 54 | |||
| 55 | public function __construct(ReferenceResolverInterface $referenceResolver, ValidatorInterface $validator) |
||
| 60 | |||
| 61 | protected function validateConditions(array $conditions) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
| 74 | * @return array 1 element with the value true/false |
||
| 75 | */ |
||
| 76 | public function match(array $conditions) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $constraint |
||
| 120 | * @param $targetValue |
||
| 121 | * @return mixed |
||
| 122 | * @throws \Exception for unsupported keys |
||
| 123 | */ |
||
| 124 | protected function getConstraint($constraint, $targetValue) |
||
| 139 | } |
||
| 140 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.