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 |
||
21 | View Code Duplication | class PropertyMetadata implements \Serializable |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $className; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $propertyName; |
||
32 | |||
33 | /** |
||
34 | * @var \ReflectionProperty |
||
35 | */ |
||
36 | protected $reflection; |
||
37 | |||
38 | /** |
||
39 | * @var ArrayHashMapInterface |
||
40 | */ |
||
41 | protected $metadata; |
||
42 | |||
43 | public function __construct($className, $propertyName) |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | */ |
||
56 | public function className() |
||
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | */ |
||
64 | public function propertyName() |
||
68 | |||
69 | /** |
||
70 | * @return \ReflectionProperty |
||
71 | */ |
||
72 | public function reflection() |
||
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | public function metadata() |
||
84 | |||
85 | /** |
||
86 | * @param string $key |
||
87 | * @param mixed $value |
||
88 | */ |
||
89 | public function addMetadata($key, $value) |
||
93 | |||
94 | /** |
||
95 | * @param string $key |
||
96 | * |
||
97 | * @return mixed|null |
||
98 | */ |
||
99 | public function getMetadata($key) |
||
103 | |||
104 | /** |
||
105 | * @param object $obj |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function getValue($obj) |
||
113 | |||
114 | /** |
||
115 | * @param object $obj |
||
116 | * @param string $value |
||
117 | */ |
||
118 | public function setValue($obj, $value) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function serialize() |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function unserialize($str) |
||
150 | } |
||
151 |
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.