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 FormDiffService 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 FormDiffService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class FormDiffService |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Форма которую сравнивают |
||
26 | * |
||
27 | * @var FormInterface |
||
28 | */ |
||
29 | private $sourceForm; |
||
30 | |||
31 | /** |
||
32 | * Форма с которой сравнивают |
||
33 | * |
||
34 | * @var FormInterface |
||
35 | */ |
||
36 | private $targetForm; |
||
37 | |||
38 | /** |
||
39 | * Результаты сравнения |
||
40 | * |
||
41 | * @var AbstractDiff[] |
||
42 | */ |
||
43 | private $diff = []; |
||
44 | |||
45 | /** |
||
46 | * Определение элементов которые отличаются в формах |
||
47 | * |
||
48 | * @param FormInterface $sourceForm |
||
49 | * @param FormInterface $targetForm |
||
50 | * |
||
51 | * @return array |
||
52 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
53 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
54 | * @throws \Nnx\FormComparator\Comparator\Exception\RuntimeException |
||
55 | * @throws \Nnx\FormComparator\Comparator\Exception\IncorrectElementTypeException |
||
56 | */ |
||
57 | public function buildDiff(FormInterface $sourceForm, FormInterface $targetForm) |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Запускает сравнение двух Fieldset'ов |
||
88 | * |
||
89 | * @param FieldsetInterface $sourceForm |
||
90 | * @param FieldsetInterface $targetForm |
||
91 | * |
||
92 | * @return AbstractDiff[] |
||
93 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
94 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
95 | * @throws \Nnx\FormComparator\Comparator\Exception\IncorrectElementTypeException |
||
96 | */ |
||
97 | protected function runBuildDiffFieldset(FieldsetInterface $sourceForm, FieldsetInterface $targetForm) |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Проверка того что элементы совпадают по типам. |
||
107 | * |
||
108 | * Т.е. если сравниваемый элемент является коллекцией, то и элемент с которым сравнивают должен быть коллекцией. |
||
109 | * Если сравниваемый элемент является Fieldset'ом, то и элемент с которым сравнивают должен быть Fieldset'ом |
||
110 | * |
||
111 | * @param ElementInterface $sourceElement |
||
112 | * @param ElementInterface $targetElement |
||
113 | * |
||
114 | * @throws \Nnx\FormComparator\Comparator\Exception\IncorrectElementTypeException |
||
115 | */ |
||
116 | protected function validateElementType(ElementInterface $sourceElement, ElementInterface $targetElement) |
||
136 | |||
137 | /** |
||
138 | * Подготавливает список изменнные элементов для двух Fieldset'ов формы |
||
139 | * |
||
140 | * |
||
141 | * @param FieldsetInterface $sourceFieldset |
||
142 | * @param FieldsetInterface $targetFieldset |
||
143 | * @param $prefixPath |
||
144 | * |
||
145 | * @throws \Nnx\FormComparator\Comparator\Exception\IncorrectElementTypeException |
||
146 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
147 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
148 | */ |
||
149 | protected function buildDiffFieldset(FieldsetInterface $sourceFieldset, FieldsetInterface $targetFieldset, $prefixPath) |
||
168 | |||
169 | /** |
||
170 | * @param ElementInterface $insertedElement |
||
171 | * @param $prefixPath |
||
172 | * |
||
173 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
174 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
175 | */ |
||
176 | View Code Duplication | protected function createInsertedElementDiff(ElementInterface $insertedElement, $prefixPath) |
|
185 | |||
186 | |||
187 | /** |
||
188 | * Строит объект описывающий различия для двух изменныех элементов форм |
||
189 | * |
||
190 | * @param ElementInterface $sourceElement |
||
191 | * @param ElementInterface $targetElement |
||
192 | * @param $prefixPath |
||
193 | * |
||
194 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
195 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
196 | */ |
||
197 | protected function buildDiffElementValue(ElementInterface $sourceElement, ElementInterface $targetElement, $prefixPath) |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @param FieldsetInterface $sourceFieldset |
||
222 | * @param FieldsetInterface $targetFieldset |
||
223 | * @param $prefixPath |
||
224 | * |
||
225 | * @throws \Nnx\FormComparator\Comparator\Exception\IncorrectElementTypeException |
||
226 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
227 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
228 | */ |
||
229 | protected function addNewElementInDiff(FieldsetInterface $sourceFieldset, FieldsetInterface $targetFieldset, $prefixPath) |
||
252 | |||
253 | /** |
||
254 | * Проверяет нужно ли продолжать добавление новых элементов |
||
255 | * |
||
256 | * @param ElementInterface $sourceElement |
||
257 | * @param ElementInterface $targetElement |
||
258 | * |
||
259 | * @return bool |
||
260 | * @throws \Nnx\FormComparator\Comparator\Exception\IncorrectElementTypeException |
||
261 | */ |
||
262 | protected function isRunAddNewElementInDiff(ElementInterface $sourceElement, ElementInterface $targetElement) |
||
274 | |||
275 | /** |
||
276 | * Помечает вложенные элемента Fieldset'a как удаленные |
||
277 | * |
||
278 | * @param FieldsetInterface $deletedFieldset |
||
279 | * @param $prefixPath |
||
280 | * |
||
281 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
282 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
283 | */ |
||
284 | View Code Duplication | protected function markNestedElementsAsDeletedInFieldset(FieldsetInterface $deletedFieldset, $prefixPath) |
|
297 | |||
298 | /** |
||
299 | * Помечает вложенные элемента Fieldset'a как добавленные |
||
300 | * |
||
301 | * @param FieldsetInterface $insertedFieldset |
||
302 | * @param $prefixPath |
||
303 | * |
||
304 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
305 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
306 | */ |
||
307 | View Code Duplication | protected function markNestedElementsAsInsertedInFieldset(FieldsetInterface $insertedFieldset, $prefixPath) |
|
318 | |||
319 | |||
320 | /** |
||
321 | * Создает diff - указывающий на то что элемент был удален |
||
322 | * |
||
323 | * @param ElementInterface $deletedElement |
||
324 | * @param $pathToDeletedElement |
||
325 | * |
||
326 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
327 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
328 | */ |
||
329 | View Code Duplication | public function createDeleteElementDiff(ElementInterface $deletedElement, $pathToDeletedElement) |
|
338 | |||
339 | |||
340 | /** |
||
341 | * Путь до элемента |
||
342 | * |
||
343 | * @param $elementName |
||
344 | * @param null|string $prefixPath |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | protected function buildPathToElementOrFieldset($elementName, $prefixPath = null) |
||
357 | |||
358 | /** |
||
359 | * Создает билдер, используемый для того что бы построить объект в котором описываются различия между элементами |
||
360 | * формы |
||
361 | * |
||
362 | * @param $mode |
||
363 | * |
||
364 | * @return DiffElementBuilder |
||
365 | */ |
||
366 | protected function diffBuilderFactory($mode) |
||
374 | |||
375 | /** |
||
376 | * Определяет стратегию сравнения в зависимости от типа элемента |
||
377 | * |
||
378 | * @param ElementInterface $sourceElement |
||
379 | * @param ElementInterface $targetElement |
||
380 | * @param $prefixPath |
||
381 | * |
||
382 | * @throws \Nnx\FormComparator\Comparator\Exception\IncorrectElementTypeException |
||
383 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
384 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
385 | */ |
||
386 | protected function runDiffElementStrategy(ElementInterface $sourceElement, ElementInterface $targetElement, $prefixPath) |
||
408 | |||
409 | /** |
||
410 | * Определяет стратегию для создания объекта описывающего изменения связанные с удалением элемента из формы, в |
||
411 | * зависимости от типа элемента |
||
412 | * |
||
413 | * @param ElementInterface $deletedElement |
||
414 | * @param $prefixPath |
||
415 | * |
||
416 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
417 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
418 | */ |
||
419 | View Code Duplication | protected function runDeleteElementStrategy(ElementInterface $deletedElement, $prefixPath) |
|
433 | |||
434 | |||
435 | /** |
||
436 | * В зависимости от типа элемента определяет стратегию для создания объекта описывающего изменения связанные |
||
437 | * с добавлением элемента в форму |
||
438 | * |
||
439 | * @param ElementInterface $insertedElement |
||
440 | * @param $prefixPath |
||
441 | * |
||
442 | * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
||
443 | * @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
||
444 | */ |
||
445 | View Code Duplication | protected function runInsertedElementStrategy(ElementInterface $insertedElement, $prefixPath) |
|
459 | } |
||
460 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.