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 |
||
16 | class Symbol { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $regexp; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $binding_power; |
||
26 | |||
27 | /** |
||
28 | * This defines what a token means when appearing in the initial position |
||
29 | * of an expression. |
||
30 | * |
||
31 | * @var \Closure |
||
32 | */ |
||
33 | protected $null_denotation = null; |
||
34 | |||
35 | /** |
||
36 | * This defines what a token means when appearing inside an expression |
||
37 | * to the left of the rest. |
||
38 | * |
||
39 | * @var \Closure |
||
40 | */ |
||
41 | protected $left_denotation = null; |
||
42 | |||
43 | 51 | public function __construct($regexp, $binding_power) { |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | 43 | public function regexp() { |
|
60 | |||
61 | /** |
||
62 | * @return int |
||
63 | */ |
||
64 | 31 | public function binding_power() { |
|
67 | |||
68 | /** |
||
69 | * @param \Closure $led |
||
70 | * @return self |
||
71 | */ |
||
72 | 38 | public function null_denotation_is(\Closure $led) { |
|
77 | |||
78 | /** |
||
79 | * @param array $matches |
||
80 | * @return mixed |
||
81 | */ |
||
82 | 33 | View Code Duplication | public function null_denotation(array &$matches) { |
90 | |||
91 | /** |
||
92 | * @param \Closure $led |
||
93 | * @return self |
||
94 | */ |
||
95 | 38 | public function left_denotation_is(\Closure $led) { |
|
100 | |||
101 | /** |
||
102 | * @param mixed $left |
||
103 | * @param array $matches |
||
104 | * @return mixed |
||
105 | */ |
||
106 | 22 | View Code Duplication | public function left_denotation($left, array &$matches) { |
114 | } |
||
115 |
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.