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 ResultDescriptor 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 ResultDescriptor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ResultDescriptor extends AbstractDescriptor implements ValidateDescriptorInterface, WriteXmlInterface |
||
23 | { |
||
24 | use Traits\IdTrait; |
||
25 | |||
26 | /** |
||
27 | * Предыдущий статус |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $oldStatus; |
||
32 | |||
33 | /** |
||
34 | * Статус |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $status; |
||
39 | |||
40 | /** |
||
41 | * Срок |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $dueDate; |
||
46 | |||
47 | /** |
||
48 | * @var integer|null |
||
49 | */ |
||
50 | protected $join; |
||
51 | |||
52 | /** |
||
53 | * @var integer|null |
||
54 | */ |
||
55 | protected $split; |
||
56 | |||
57 | /** |
||
58 | * @var integer |
||
59 | */ |
||
60 | protected $step; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $hasStep = false; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $owner; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $displayName; |
||
76 | |||
77 | /** |
||
78 | * @var ValidatorDescriptor[]|SplObjectStorage |
||
79 | */ |
||
80 | protected $validators; |
||
81 | |||
82 | /** |
||
83 | * @var FunctionDescriptor[]|SplObjectStorage |
||
84 | */ |
||
85 | protected $preFunctions; |
||
86 | |||
87 | /** |
||
88 | * @var FunctionDescriptor[]|SplObjectStorage |
||
89 | */ |
||
90 | protected $postFunctions; |
||
91 | |||
92 | /** |
||
93 | * Если флаг установлен в true, то не запускаем инициализацию дескриптора для элемента |
||
94 | * |
||
95 | * @var bool |
||
96 | */ |
||
97 | protected $flagNotExecuteInit = false; |
||
98 | |||
99 | /** |
||
100 | * @param $element |
||
101 | */ |
||
102 | 101 | public function __construct(DOMElement $element = null) |
|
116 | |||
117 | /** |
||
118 | * @param DOMElement $result |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | 98 | protected function init(DOMElement $result) |
|
196 | |||
197 | /** |
||
198 | * Возвращает предыдущий статус |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | 47 | public function getOldStatus() |
|
206 | |||
207 | /** |
||
208 | * Устанавливает значение предыдующего статуса |
||
209 | * |
||
210 | * @param string $oldStatus |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | 98 | public function setOldStatus($oldStatus) |
|
220 | |||
221 | /** |
||
222 | * Возвращает статус |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | 52 | public function getStatus() |
|
230 | |||
231 | /** |
||
232 | * Устанавливает статус |
||
233 | * |
||
234 | * @param string $status |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | 75 | public function setStatus($status) |
|
244 | |||
245 | /** |
||
246 | * Срок |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | 47 | public function getDueDate() |
|
254 | |||
255 | /** |
||
256 | * @return int|null |
||
257 | */ |
||
258 | 70 | public function getJoin() |
|
262 | |||
263 | /** |
||
264 | * @param int $join |
||
265 | * |
||
266 | * @return $this |
||
267 | */ |
||
268 | 21 | public function setJoin($join) |
|
274 | |||
275 | /** |
||
276 | * @return int|null |
||
277 | */ |
||
278 | 67 | public function getSplit() |
|
282 | |||
283 | /** |
||
284 | * @param int|null $split |
||
285 | * |
||
286 | * @return $this |
||
287 | */ |
||
288 | 17 | public function setSplit($split) |
|
294 | |||
295 | /** |
||
296 | * @return int |
||
297 | */ |
||
298 | 41 | public function getStep() |
|
302 | |||
303 | /** |
||
304 | * @param int $step |
||
305 | * |
||
306 | * @return $this |
||
307 | */ |
||
308 | 76 | public function setStep($step) |
|
315 | |||
316 | /** |
||
317 | * @return string |
||
318 | */ |
||
319 | 35 | public function getOwner() |
|
323 | |||
324 | /** |
||
325 | * @param string $owner |
||
326 | * |
||
327 | * @return $this |
||
328 | */ |
||
329 | 20 | public function setOwner($owner) |
|
335 | |||
336 | /** |
||
337 | * @return string |
||
338 | */ |
||
339 | 21 | public function getDisplayName() |
|
343 | |||
344 | /** |
||
345 | * @param string $displayName |
||
346 | * |
||
347 | * @return $this |
||
348 | */ |
||
349 | 7 | public function setDisplayName($displayName) |
|
364 | |||
365 | /** |
||
366 | * @return ValidatorDescriptor[]|SplObjectStorage |
||
367 | */ |
||
368 | 58 | public function getValidators() |
|
372 | |||
373 | /** |
||
374 | * @param ValidatorDescriptor[]|SplObjectStorage $validators |
||
375 | * |
||
376 | * @return $this |
||
377 | */ |
||
378 | 101 | public function setValidators(SplObjectStorage $validators) |
|
384 | |||
385 | /** |
||
386 | * @return FunctionDescriptor[]|SplObjectStorage |
||
387 | */ |
||
388 | 57 | public function getPreFunctions() |
|
392 | |||
393 | /** |
||
394 | * @return FunctionDescriptor[]|SplObjectStorage |
||
395 | */ |
||
396 | 56 | public function getPostFunctions() |
|
400 | |||
401 | /** |
||
402 | * Вывод информации о функциях пост обработки |
||
403 | * |
||
404 | * @param DOMDocument $dom |
||
405 | * @return DOMElement|null |
||
406 | * @throws InvalidWriteWorkflowException |
||
407 | */ |
||
408 | 24 | View Code Duplication | protected function printPostFunctions(DOMDocument $dom) |
429 | |||
430 | /** |
||
431 | * Вывод информации о функциях пред обработки |
||
432 | * |
||
433 | * @param DOMDocument $dom |
||
434 | * @return DOMElement|null |
||
435 | * |
||
436 | * @throws InvalidWriteWorkflowException |
||
437 | */ |
||
438 | 25 | View Code Duplication | protected function printPreFunctions(DOMDocument $dom) |
458 | |||
459 | |||
460 | /** |
||
461 | * Создает DOMElement - эквивалентный состоянию дескриптора |
||
462 | * |
||
463 | * @param DOMDocument $dom |
||
464 | * |
||
465 | * @return DOMElement |
||
466 | * @throws InvalidDescriptorException |
||
467 | * @throws InvalidWriteWorkflowException |
||
468 | */ |
||
469 | 26 | public function writeXml(DOMDocument $dom = null) |
|
551 | |||
552 | |||
553 | /** |
||
554 | * Валидация дескриптора |
||
555 | * |
||
556 | * @return void |
||
557 | * @throws InvalidWorkflowDescriptorException |
||
558 | */ |
||
559 | 28 | public function validate() |
|
593 | } |
||
594 |
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.