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 NamedFormElement 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 NamedFormElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class NamedFormElement extends FormElement |
||
17 | { |
||
18 | use HtmlAttributes; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $path; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $name; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $modelAttributeKey; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $label; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $helpText; |
||
43 | |||
44 | /** |
||
45 | * @var mixed |
||
46 | */ |
||
47 | protected $defaultValue; |
||
48 | |||
49 | /** |
||
50 | * @var \Closure |
||
51 | */ |
||
52 | protected $mutator; |
||
53 | |||
54 | /** |
||
55 | * @param string $path |
||
56 | * @param string|null $label |
||
57 | * |
||
58 | * @throws FormElementException |
||
59 | */ |
||
60 | 27 | public function __construct($path, $label = null) |
|
75 | |||
76 | /** |
||
77 | * Compose html name from array like this: 'first[second][third]'. |
||
78 | * |
||
79 | * @param array $parts |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | 26 | private function composeName(array $parts) |
|
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | 13 | public function getPath() |
|
102 | |||
103 | /** |
||
104 | * @param string $path |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | 26 | public function setPath($path) |
|
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | 5 | public function getName() |
|
122 | |||
123 | /** |
||
124 | * @param string $name |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | 26 | public function setName($name) |
|
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | 4 | public function getLabel() |
|
142 | |||
143 | /** |
||
144 | * @param string $label |
||
145 | * |
||
146 | * @return $this |
||
147 | */ |
||
148 | 26 | public function setLabel($label) |
|
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 6 | public function getModelAttributeKey() |
|
162 | |||
163 | /** |
||
164 | * @param string $key |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | 26 | public function setModelAttributeKey($key) |
|
174 | |||
175 | /** |
||
176 | * @return mixed |
||
177 | */ |
||
178 | 3 | public function getDefaultValue() |
|
182 | |||
183 | /** |
||
184 | * @param mixed $defaultValue |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | 1 | public function setDefaultValue($defaultValue) |
|
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | 2 | public function getHelpText() |
|
206 | |||
207 | /** |
||
208 | * @param string|Htmlable $helpText |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | 1 | public function setHelpText($helpText) |
|
218 | |||
219 | /** |
||
220 | * @param string|null $message |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | 2 | public function required($message = null) |
|
230 | |||
231 | /** |
||
232 | * @param string|null $message |
||
233 | * |
||
234 | * @return $this |
||
235 | */ |
||
236 | 2 | public function unique($message = null) |
|
246 | |||
247 | /** |
||
248 | * @return array |
||
249 | */ |
||
250 | 3 | public function getValidationMessages() |
|
261 | |||
262 | /** |
||
263 | * @return array |
||
264 | */ |
||
265 | 1 | public function getValidationLabels() |
|
269 | |||
270 | /** |
||
271 | * If FormElement has `_unique` rule, it will get all appropriate |
||
272 | * validation rules based on underlying model. |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | 2 | public function getValidationRules() |
|
297 | |||
298 | /** |
||
299 | * Get model related to form element. |
||
300 | * |
||
301 | * @return mixed |
||
302 | */ |
||
303 | 2 | public function resolvePath() |
|
340 | |||
341 | /** |
||
342 | * @param \Illuminate\Http\Request $request |
||
343 | * |
||
344 | * @return array|string |
||
345 | */ |
||
346 | 6 | public function getValueFromRequest(\Illuminate\Http\Request $request) |
|
354 | |||
355 | /** |
||
356 | * @return mixed |
||
357 | */ |
||
358 | 3 | public function getValueFromModel() |
|
404 | |||
405 | /** |
||
406 | * @param \Illuminate\Http\Request $request |
||
407 | * |
||
408 | * @return void |
||
409 | */ |
||
410 | 1 | public function save(\Illuminate\Http\Request $request) |
|
418 | |||
419 | /** |
||
420 | * @param mixed $value |
||
421 | * |
||
422 | * @return void |
||
423 | */ |
||
424 | 3 | public function setModelAttribute($value) |
|
439 | |||
440 | /** |
||
441 | * @param string $path |
||
442 | * |
||
443 | * @return Model|null |
||
444 | */ |
||
445 | 5 | protected function getModelByPath($path) |
|
494 | |||
495 | protected function getForeignKeyNameFromRelation($relation) |
||
501 | |||
502 | /** |
||
503 | * Field->mutateValue(function($value) { |
||
504 | * return bcrypt($value); |
||
505 | * }). |
||
506 | * |
||
507 | * @param \Closure $mutator |
||
508 | * |
||
509 | * @return $this |
||
510 | */ |
||
511 | 2 | public function mutateValue(\Closure $mutator) |
|
517 | |||
518 | /** |
||
519 | * @return bool |
||
520 | */ |
||
521 | 5 | public function hasMutator() |
|
525 | |||
526 | /** |
||
527 | * @param mixed $value |
||
528 | * |
||
529 | * @return mixed |
||
530 | */ |
||
531 | 4 | public function prepareValue($value) |
|
539 | |||
540 | /** |
||
541 | * @return array |
||
542 | */ |
||
543 | 1 | public function toArray() |
|
561 | } |
||
562 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.