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 | 'satisfies' => '\Symfony\Component\Validator\Constraints\Expression', |
||
43 | //'in' => Operator::IN, |
||
44 | //'between' => Operator::BETWEEN, => use count/length with min & max sub-members |
||
45 | //'like' => Operator::LIKE, => use regex |
||
46 | //'contains' => Operator::CONTAINS, |
||
47 | |||
48 | Operator::EQ => '\Symfony\Component\Validator\Constraints\EqualTo', |
||
49 | Operator::GT => '\Symfony\Component\Validator\Constraints\GreaterThan', |
||
50 | Operator::GTE => '\Symfony\Component\Validator\Constraints\GreaterThanOrEqual', |
||
51 | Operator::LT => '\Symfony\Component\Validator\Constraints\LessThan', |
||
52 | Operator::LTE => '\Symfony\Component\Validator\Constraints\LessThanOrEqual', |
||
53 | '!=' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
||
54 | '<>' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
||
55 | ); |
||
56 | |||
57 | 80 | public function __construct(ReferenceResolverInterface $referenceResolver, ValidatorInterface $validator) |
|
62 | |||
63 | // q: what if we receive an array of conditions? it seems that it might be validated here, even though only the 1st |
||
64 | // condition would be taken into account... |
||
65 | 2 | protected function validateConditions(array $conditions) |
|
75 | |||
76 | /** |
||
77 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
78 | * @return array 1 element with the value true/false |
||
79 | */ |
||
80 | 2 | public function match(array $conditions) |
|
122 | |||
123 | /** |
||
124 | * @param string $constraint |
||
125 | * @param $targetValue |
||
126 | * @return mixed |
||
127 | * @throws \Exception for unsupported keys |
||
128 | */ |
||
129 | 2 | protected function getConstraint($constraint, $targetValue) |
|
140 | } |
||
141 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.