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 |
||
19 | abstract class Arithmetic implements Operand |
||
20 | { |
||
21 | const ADD = '+'; |
||
22 | |||
23 | const SUB = '-'; |
||
24 | |||
25 | const MUL = '*'; |
||
26 | |||
27 | const DIV = '/'; |
||
28 | |||
29 | const MOD = '%'; |
||
30 | |||
31 | /** |
||
32 | * @var string[] |
||
33 | */ |
||
34 | private static $operations = [ |
||
35 | self::ADD, |
||
36 | self::SUB, |
||
37 | self::MUL, |
||
38 | self::DIV, |
||
39 | self::MOD, |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $operation = ''; |
||
46 | |||
47 | /** |
||
48 | * @var Operand|string |
||
49 | */ |
||
50 | private $field; |
||
51 | |||
52 | /** |
||
53 | * @var Operand|string |
||
54 | */ |
||
55 | private $value; |
||
56 | |||
57 | /** |
||
58 | * @param string $operation |
||
59 | * @param Operand|string $field |
||
60 | * @param Operand|string $value |
||
61 | */ |
||
62 | View Code Duplication | public function __construct($operation, $field, $value) |
|
76 | |||
77 | /** |
||
78 | * @param QueryBuilder $qb |
||
79 | * @param string $dqlAlias |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function transform(QueryBuilder $qb, $dqlAlias) |
||
93 | } |
||
94 |
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.