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 |
||
12 | class Property extends AbstractConfig implements PropertyInterface |
||
13 | { |
||
14 | /** |
||
15 | * TRUE to skip property change notification, FALSE otherwise |
||
16 | * |
||
17 | * @var boolean |
||
18 | */ |
||
19 | protected $skipNotify = false; |
||
20 | /** |
||
21 | * Property value |
||
22 | * |
||
23 | * @var mixed |
||
24 | */ |
||
25 | private $value; |
||
26 | /** |
||
27 | * Cached value of "nullable" configuration option |
||
28 | * |
||
29 | * @var boolean |
||
30 | */ |
||
31 | private $nullable = true; |
||
32 | |||
33 | /** |
||
34 | * Class constructor |
||
35 | * |
||
36 | * @param mixed $value OPTIONAL Property value |
||
37 | * @param array $config OPTIONAL Configuration options for this property |
||
38 | * @throws \Flying\Struct\Exception |
||
39 | * @throws \RuntimeException |
||
40 | */ |
||
41 | 397 | View Code Duplication | public function __construct($value = null, array $config = null) |
59 | |||
60 | /** |
||
61 | * Reset property to its default state |
||
62 | * |
||
63 | * @throws Exception |
||
64 | * @return void |
||
65 | * @throws \RuntimeException |
||
66 | */ |
||
67 | 246 | public function reset() |
|
81 | |||
82 | /** |
||
83 | * Normalize given value to make it compatible with property requirements |
||
84 | * |
||
85 | * @param mixed $value Given property value (passed by reference) |
||
86 | * @return boolean TRUE if value can be accepted, FALSE otherwise |
||
87 | */ |
||
88 | 361 | protected function normalize(&$value) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | * @throws \InvalidArgumentException |
||
102 | */ |
||
103 | 338 | public function validateConfig($name, &$value) |
|
119 | |||
120 | /** |
||
121 | * Implementation of Serializable interface |
||
122 | * |
||
123 | * @return string |
||
124 | * @throws \RuntimeException |
||
125 | */ |
||
126 | 20 | public function serialize() |
|
133 | |||
134 | /** |
||
135 | * Get property value |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | 240 | public function getValue() |
|
143 | |||
144 | /** |
||
145 | * Set property value |
||
146 | * |
||
147 | * @param mixed $value |
||
148 | * @return boolean |
||
149 | * @throws \RuntimeException |
||
150 | */ |
||
151 | 165 | public function setValue($value) |
|
162 | |||
163 | /** |
||
164 | * Get property's configuration options prepared for serialization |
||
165 | * |
||
166 | * @return array |
||
167 | * @throws \RuntimeException |
||
168 | */ |
||
169 | 20 | protected function getConfigForSerialization() |
|
175 | |||
176 | /** |
||
177 | * Implementation of Serializable interface |
||
178 | * |
||
179 | * @param string $serialized Serialized object data |
||
180 | * @return void |
||
181 | * @throws \InvalidArgumentException |
||
182 | * @throws \RuntimeException |
||
183 | */ |
||
184 | 20 | public function unserialize($serialized) |
|
200 | |||
201 | /** |
||
202 | * Value change notification handler |
||
203 | * |
||
204 | * @return void |
||
205 | * @throws \RuntimeException |
||
206 | */ |
||
207 | 219 | View Code Duplication | protected function onChange() |
217 | |||
218 | /** |
||
219 | * Invalid value setting handler |
||
220 | * |
||
221 | * @param mixed $value |
||
222 | * @return void |
||
223 | */ |
||
224 | 24 | protected function onInvalidValue($value) |
|
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | * @throws \InvalidArgumentException |
||
232 | * @throws \RuntimeException |
||
233 | */ |
||
234 | 3 | protected function initConfig() |
|
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | 293 | protected function onConfigChange($name, $value) |
|
255 | } |
||
256 |
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.