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 |
||
8 | View Code Duplication | abstract class Bitwise implements Operand |
|
|
|||
9 | { |
||
10 | const B_AND = '&'; |
||
11 | |||
12 | const B_OR = '|'; |
||
13 | |||
14 | const B_XOR = '^'; |
||
15 | |||
16 | const B_LS = '<<'; |
||
17 | |||
18 | const B_RS = '>>'; |
||
19 | |||
20 | /** |
||
21 | * @var string[] |
||
22 | */ |
||
23 | private static $operations = array( |
||
24 | self::B_AND, |
||
25 | self::B_OR, |
||
26 | self::B_XOR, |
||
27 | self::B_LS, |
||
28 | self::B_RS, |
||
29 | ); |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $operation = ''; |
||
35 | |||
36 | /** |
||
37 | * @var Operand|string |
||
38 | */ |
||
39 | private $field; |
||
40 | |||
41 | /** |
||
42 | * @var Operand|string |
||
43 | */ |
||
44 | private $value; |
||
45 | |||
46 | /** |
||
47 | * @param string $operation |
||
48 | * @param Operand|string $field |
||
49 | * @param Operand|string $value |
||
50 | */ |
||
51 | public function __construct($operation, $field, $value) |
||
65 | |||
66 | /** |
||
67 | * @param QueryBuilder $qb |
||
68 | * @param string $dqlAlias |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function transform(QueryBuilder $qb, $dqlAlias) |
||
82 | } |
||
83 |
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.