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 CrudFields 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 CrudFields, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class CrudFields |
||
16 | { |
||
17 | /** |
||
18 | * @var Field[] |
||
19 | */ |
||
20 | protected $fields = []; |
||
21 | |||
22 | /** |
||
23 | * @var CrudEntry |
||
24 | */ |
||
25 | protected $entry; |
||
26 | |||
27 | /** |
||
28 | * @var MessageBag |
||
29 | */ |
||
30 | protected $errors; |
||
31 | |||
32 | /** |
||
33 | * @var MessageBag |
||
34 | */ |
||
35 | protected $old_input; |
||
36 | |||
37 | /** |
||
38 | * Make staticly a new instance. |
||
39 | * |
||
40 | * @param array $fields |
||
41 | * @return CrudFields |
||
42 | */ |
||
43 | public static function make(array $fields) |
||
47 | |||
48 | /** |
||
49 | * @param CrudEntry $entry |
||
50 | * @return self |
||
51 | */ |
||
52 | public function setEntry(CrudEntry $entry) |
||
58 | |||
59 | /** |
||
60 | * @param void |
||
61 | * @return CrudEntry |
||
62 | */ |
||
63 | public function getEntry() |
||
67 | |||
68 | /** |
||
69 | * @param Field[] $fields |
||
70 | * @return self |
||
71 | * @throws DuplicateFieldIdentifierException |
||
72 | */ |
||
73 | public function add(array $fields) |
||
86 | |||
87 | /** |
||
88 | * @param Field $field |
||
89 | * @return self |
||
90 | * @throws DuplicateFieldIdentifierException |
||
91 | */ |
||
92 | public function set(Field $field) |
||
106 | |||
107 | /** |
||
108 | * @param void |
||
109 | * @return int |
||
110 | */ |
||
111 | public function count() |
||
115 | |||
116 | /** |
||
117 | * @param string $identifier |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function has($identifier) |
||
124 | |||
125 | /** |
||
126 | * @param null|string $identifier |
||
127 | * @return Field[]|Field|null |
||
128 | */ |
||
129 | public function get($identifier = null) |
||
143 | |||
144 | /** |
||
145 | * @param void |
||
146 | * @return \Generator |
||
147 | */ |
||
148 | public function columns() |
||
155 | |||
156 | /** |
||
157 | * @param void |
||
158 | * @return \Generator |
||
159 | */ |
||
160 | public function loop() |
||
167 | |||
168 | /** |
||
169 | * Returns all fields keys. |
||
170 | * |
||
171 | * @param void |
||
172 | * @return integer[] |
||
173 | */ |
||
174 | public function keys() |
||
178 | |||
179 | /** |
||
180 | * @param void |
||
181 | * @return array |
||
182 | */ |
||
183 | public function getFieldsScripts() |
||
194 | |||
195 | /** |
||
196 | * @param void |
||
197 | * @return array |
||
198 | */ |
||
199 | public function getFieldsCss() |
||
210 | |||
211 | /** |
||
212 | * Gets fields validation rules. |
||
213 | * |
||
214 | * @param void |
||
215 | * @return array |
||
216 | * @throws NoFieldsException |
||
217 | */ |
||
218 | public function validationRules() |
||
239 | |||
240 | /** |
||
241 | * Hydrate fields with new data. |
||
242 | * |
||
243 | * @param array $data |
||
244 | * @return self |
||
245 | */ |
||
246 | public function hydrateFormData(array $data) |
||
263 | |||
264 | /** |
||
265 | * @param void |
||
266 | * @return MessageBag |
||
267 | */ |
||
268 | public function getErrors() |
||
277 | |||
278 | /** |
||
279 | * @param void |
||
280 | * @return MessageBag |
||
281 | */ |
||
282 | public function getOldInput() |
||
291 | |||
292 | /** |
||
293 | * Hydrates the instance with previous validation errors. |
||
294 | * |
||
295 | * @param MessageBag $errors |
||
296 | * @return self |
||
297 | */ |
||
298 | View Code Duplication | public function hydrateErrorsFromSession(MessageBag $errors = null) |
|
315 | |||
316 | /** |
||
317 | * Hydrates the instance with previous input. |
||
318 | * |
||
319 | * @param MessageBag $old_input |
||
320 | * @return self |
||
321 | */ |
||
322 | View Code Duplication | public function hydrateFieldsFromSession(MessageBag $old_input = null) |
|
339 | } |
||
340 |
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.