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:
Complex classes like PropertyField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PropertyField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class PropertyField |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $ident; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $label; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $sqlType; |
||
27 | |||
28 | /** |
||
29 | * @var integer |
||
30 | */ |
||
31 | private $sqlPdoType; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $sqlEncoding; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $extra; |
||
42 | |||
43 | /** |
||
44 | * @var mixed |
||
45 | */ |
||
46 | private $val; |
||
47 | |||
48 | /** |
||
49 | * @var mixed |
||
50 | */ |
||
51 | private $defaultVal; |
||
52 | |||
53 | /** |
||
54 | * @var boolean |
||
55 | */ |
||
56 | private $allowNull; |
||
57 | |||
58 | /** |
||
59 | * Holds a list of all snake_case strings. |
||
60 | * |
||
61 | * @var string[] |
||
62 | */ |
||
63 | protected static $snakeCache = []; |
||
64 | |||
65 | /** |
||
66 | * @param array $data The field data. |
||
67 | * @return PropertyField Chainable |
||
68 | */ |
||
69 | public function setData(array $data) |
||
101 | |||
102 | /** |
||
103 | * @param string $ident The field identifier. |
||
104 | * @throws InvalidArgumentException If the identifier is not a string. |
||
105 | * @return PropertyField Chainable |
||
106 | */ |
||
107 | public function setIdent($ident) |
||
117 | |||
118 | /** |
||
119 | * @return string|null |
||
120 | */ |
||
121 | public function ident() |
||
125 | |||
126 | /** |
||
127 | * @param string $label The field label. |
||
128 | * @throws InvalidArgumentException If the label is not a string. |
||
129 | * @return PropertyField Chainable |
||
130 | */ |
||
131 | View Code Duplication | public function setLabel($label) |
|
141 | |||
142 | /** |
||
143 | * @return string|null |
||
144 | */ |
||
145 | public function label() |
||
149 | |||
150 | /** |
||
151 | * @param string $sqlType The field SQL column type. |
||
152 | * @throws InvalidArgumentException If the SQL type is not a string. |
||
153 | * @return PropertyField Chainable |
||
154 | */ |
||
155 | public function setSqlType($sqlType) |
||
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | public function sqlType() |
||
173 | |||
174 | /** |
||
175 | * @param integer $sqlPdoType The field PDO type. |
||
176 | * @throws InvalidArgumentException If the PDO type is not an integer. |
||
177 | * @return PropertyField Chainable |
||
178 | */ |
||
179 | public function setSqlPdoType($sqlPdoType) |
||
189 | |||
190 | /** |
||
191 | * @return integer |
||
192 | */ |
||
193 | public function sqlPdoType() |
||
201 | |||
202 | /** |
||
203 | * @param string|null $extra The extra. |
||
204 | * @throws InvalidArgumentException If the extra is not a string. |
||
205 | * @return PropertyField Chainable |
||
206 | */ |
||
207 | View Code Duplication | public function setExtra($extra) |
|
217 | |||
218 | /** |
||
219 | * @return string|null |
||
220 | */ |
||
221 | public function extra() |
||
225 | |||
226 | /** |
||
227 | * @param string $encoding The encoding and collation. |
||
228 | * @throws InvalidArgumentException If the encoding is not a string. |
||
229 | * @return PropertyField Chainable |
||
230 | */ |
||
231 | public function setSqlEncoding($encoding) |
||
241 | |||
242 | /** |
||
243 | * @return string|null |
||
244 | */ |
||
245 | public function sqlEncoding() |
||
249 | |||
250 | /** |
||
251 | * @param mixed $val The field value. |
||
252 | * @return PropertyField Chainable |
||
253 | */ |
||
254 | public function setVal($val) |
||
259 | |||
260 | /** |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function val() |
||
267 | |||
268 | /** |
||
269 | * @param mixed $defaultVal The default field value. |
||
270 | * @return PropertyField Chainable |
||
271 | */ |
||
272 | public function setDefaultVal($defaultVal) |
||
277 | |||
278 | /** |
||
279 | * @return mixed |
||
280 | */ |
||
281 | public function defaultVal() |
||
285 | |||
286 | /** |
||
287 | * @param boolean $allowNull The field allow null flag. |
||
288 | * @return PropertyField Chainable |
||
289 | */ |
||
290 | public function setAllowNull($allowNull) |
||
295 | |||
296 | /** |
||
297 | * @return boolean |
||
298 | */ |
||
299 | public function allowNull() |
||
303 | |||
304 | /** |
||
305 | * Generates the SQL table column. |
||
306 | * |
||
307 | * @return string|null |
||
308 | */ |
||
309 | public function sql() |
||
352 | |||
353 | /** |
||
354 | * Transform a string from "camelCase" to "snake_case". |
||
355 | * |
||
356 | * @param string $value The string to snakeize. |
||
357 | * @return string The snake_case string. |
||
358 | */ |
||
359 | View Code Duplication | protected function snakeize($value) |
|
373 | } |
||
374 |
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.