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 ActionDescriptor 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 ActionDescriptor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ActionDescriptor extends AbstractDescriptor |
||
21 | implements |
||
22 | Traits\NameInterface, |
||
23 | ValidateDescriptorInterface, |
||
24 | WriteXmlInterface |
||
25 | { |
||
26 | use Traits\NameTrait, Traits\IdTrait; |
||
27 | |||
28 | /** |
||
29 | * @var ConditionalResultDescriptor[]|SplObjectStorage |
||
30 | */ |
||
31 | protected $conditionalResults; |
||
32 | |||
33 | /** |
||
34 | * @var FunctionDescriptor[]|SplObjectStorage |
||
35 | */ |
||
36 | protected $postFunctions; |
||
37 | |||
38 | /** |
||
39 | * @var FunctionDescriptor[]|SplObjectStorage |
||
40 | */ |
||
41 | protected $preFunctions; |
||
42 | |||
43 | /** |
||
44 | * @var ValidatorDescriptor[]|SplObjectStorage |
||
45 | */ |
||
46 | protected $validators; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $metaAttributes = []; |
||
52 | |||
53 | /** |
||
54 | * @var RestrictionDescriptor |
||
55 | */ |
||
56 | protected $restriction; |
||
57 | |||
58 | /** |
||
59 | * @var ResultDescriptor |
||
60 | */ |
||
61 | protected $unconditionalResult; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $view; |
||
67 | |||
68 | /** |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $autoExecute = false; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $common = false; |
||
77 | |||
78 | /** |
||
79 | * @var bool |
||
80 | */ |
||
81 | protected $finish = false; |
||
82 | |||
83 | /** |
||
84 | * @param $element |
||
85 | * @throws InvalidWorkflowDescriptorException |
||
86 | */ |
||
87 | 78 | public function __construct(DOMElement $element = null) |
|
100 | |||
101 | /** |
||
102 | * @param DOMElement $action |
||
103 | * |
||
104 | * @return void |
||
105 | * @throws InvalidWorkflowDescriptorException |
||
106 | */ |
||
107 | 75 | protected function init(DOMElement $action) |
|
206 | |||
207 | /** |
||
208 | * @return boolean |
||
209 | */ |
||
210 | 15 | public function getAutoExecute() |
|
214 | |||
215 | /** |
||
216 | * @param boolean $autoExecute |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | 2 | public function setAutoExecute($autoExecute) |
|
226 | |||
227 | /** |
||
228 | * @return boolean |
||
229 | */ |
||
230 | 17 | public function isCommon() |
|
234 | |||
235 | /** |
||
236 | * @param boolean $common |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | 11 | public function setCommon($common) |
|
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | 15 | public function getView() |
|
254 | |||
255 | /** |
||
256 | * @param string $view |
||
257 | * |
||
258 | * @return $this |
||
259 | */ |
||
260 | 2 | public function setView($view) |
|
266 | |||
267 | /** |
||
268 | * @return ValidatorDescriptor[]|SplObjectStorage |
||
269 | */ |
||
270 | 40 | public function getValidators() |
|
274 | |||
275 | /** |
||
276 | * @return ResultDescriptor |
||
277 | */ |
||
278 | 40 | public function getUnconditionalResult() |
|
282 | |||
283 | /** |
||
284 | * @param ResultDescriptor $unconditionalResult |
||
285 | * |
||
286 | * @return $this |
||
287 | */ |
||
288 | 65 | public function setUnconditionalResult(ResultDescriptor $unconditionalResult) |
|
294 | |||
295 | /** |
||
296 | * @return RestrictionDescriptor |
||
297 | */ |
||
298 | 40 | public function getRestriction() |
|
302 | |||
303 | /** |
||
304 | * @param RestrictionDescriptor $restriction |
||
305 | * |
||
306 | * @return $this |
||
307 | */ |
||
308 | 20 | public function setRestriction(RestrictionDescriptor $restriction) |
|
314 | |||
315 | /** |
||
316 | * @return FunctionDescriptor[]|SplObjectStorage |
||
317 | */ |
||
318 | 22 | public function getPostFunctions() |
|
322 | |||
323 | /** |
||
324 | * @return FunctionDescriptor[]|SplObjectStorage |
||
325 | */ |
||
326 | 39 | public function getPreFunctions() |
|
330 | |||
331 | /** |
||
332 | * @return array |
||
333 | */ |
||
334 | 15 | public function getMetaAttributes() |
|
338 | |||
339 | /** |
||
340 | * @param array $metaAttributes |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | 75 | public function setMetaAttributes(array $metaAttributes = []) |
|
350 | |||
351 | /** |
||
352 | * @return boolean |
||
353 | */ |
||
354 | 39 | public function isFinish() |
|
358 | |||
359 | /** |
||
360 | * @param boolean $finish |
||
361 | * |
||
362 | * @return $this |
||
363 | */ |
||
364 | 2 | public function setFinish($finish) |
|
370 | |||
371 | /** |
||
372 | * @return SplObjectStorage|ConditionalResultDescriptor[] |
||
373 | */ |
||
374 | 49 | public function getConditionalResults() |
|
378 | |||
379 | |||
380 | /** |
||
381 | * Валидация дескриптора |
||
382 | * |
||
383 | * @return void |
||
384 | * @throws InvalidWorkflowDescriptorException |
||
385 | */ |
||
386 | 19 | public function validate() |
|
414 | |||
415 | /** |
||
416 | * Создает DOMElement - эквивалентный состоянию дескриптора |
||
417 | * |
||
418 | * @param DOMDocument $dom |
||
419 | * |
||
420 | * @return DOMElement |
||
421 | * @throws InvalidDescriptorException |
||
422 | * @throws InvalidWriteWorkflowException |
||
423 | */ |
||
424 | 17 | public function writeXml(DOMDocument $dom = null) |
|
534 | } |
||
535 |
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.