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 |
||
7 | class Comparison implements Expression |
||
8 | { |
||
9 | const EQUALS = 'eq'; |
||
10 | const NOT_EQUALS = 'neq'; |
||
11 | const GREATER_THAN = 'gt'; |
||
12 | const GREATER_THAN_EQUAL = 'gte'; |
||
13 | const LESS_THAN = 'lt'; |
||
14 | const LESS_THAN_EQUAL = 'lte'; |
||
15 | const NULL = 'null'; |
||
16 | const NOT_NULL = 'not_null'; |
||
17 | const IN = 'in'; |
||
18 | const NOT_IN = 'nin'; |
||
19 | const CONTAINS = 'contains'; |
||
20 | const NOT_CONTAINS = 'not_contains'; |
||
21 | |||
22 | private static $validTypes = [ |
||
23 | self::EQUALS, |
||
24 | self::NOT_EQUALS, |
||
25 | self::GREATER_THAN, |
||
26 | self::GREATER_THAN_EQUAL, |
||
27 | self::LESS_THAN, |
||
28 | self::LESS_THAN_EQUAL, |
||
29 | self::NULL, |
||
30 | self::NOT_NULL, |
||
31 | self::IN, |
||
32 | self::NOT_IN, |
||
33 | self::CONTAINS, |
||
34 | self::NOT_CONTAINS, |
||
35 | ]; |
||
36 | |||
37 | private $comparator; |
||
38 | private $field; |
||
39 | private $value; |
||
40 | |||
41 | View Code Duplication | public function __construct(string $comparator, $field, $value) |
|
55 | |||
56 | public function getComparator() |
||
60 | |||
61 | public function getField() |
||
65 | |||
66 | public function getValue() |
||
70 | } |
||
71 |
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.