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 XmlWorkflowFactory 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 XmlWorkflowFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class XmlWorkflowFactory extends AbstractWorkflowFactory implements Serializable |
||
25 | { |
||
26 | /** |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | const RESOURCE_PROPERTY = 'resource'; |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | const RELOAD_PROPERTY = 'reload'; |
||
37 | |||
38 | /** |
||
39 | * @var WorkflowConfig[] |
||
40 | */ |
||
41 | protected $workflows = []; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $reload = false; |
||
47 | |||
48 | /** |
||
49 | * Пути по умолчнаию до файла с workflows |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected static $defaultPathsToWorkflows = []; |
||
54 | |||
55 | /** |
||
56 | * @param Properties $p |
||
57 | */ |
||
58 | 23 | public function __construct(Properties $p = null) |
|
63 | |||
64 | |||
65 | /** |
||
66 | * @param string $workflowName |
||
67 | * @param string $layout |
||
68 | * |
||
69 | * @return $this |
||
70 | */ |
||
71 | 1 | public function setLayout($workflowName, $layout) |
|
74 | |||
75 | /** |
||
76 | * @param string $workflowName |
||
77 | * |
||
78 | * @return mixed|null |
||
79 | */ |
||
80 | 1 | public function getLayout($workflowName) |
|
84 | |||
85 | |||
86 | /** |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 1 | public function getName() |
|
94 | |||
95 | /** |
||
96 | * @param string $name |
||
97 | * |
||
98 | * @return boolean |
||
99 | */ |
||
100 | 1 | public function isModifiable($name) |
|
104 | |||
105 | /** |
||
106 | * String representation of object |
||
107 | * |
||
108 | * @link http://php.net/manual/en/serializable.serialize.php |
||
109 | * @return string the string representation of the object or null |
||
110 | */ |
||
111 | 1 | public function serialize() |
|
114 | |||
115 | /** |
||
116 | * Constructs the object |
||
117 | * |
||
118 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
119 | * |
||
120 | * @param string $serialized <p> |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | 1 | public function unserialize($serialized) |
|
127 | /** |
||
128 | * |
||
129 | * @return String[] |
||
130 | * @throws FactoryException |
||
131 | */ |
||
132 | 1 | public function getWorkflowNames() |
|
138 | |||
139 | /** |
||
140 | * @param string $name |
||
141 | * |
||
142 | * @return boolean |
||
143 | * @throws FactoryException |
||
144 | */ |
||
145 | 1 | public function removeWorkflow($name) |
|
149 | |||
150 | /** |
||
151 | * @param string $oldName |
||
152 | * @param string $newName |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 1 | public function renameWorkflow($newName, $oldName = null) |
|
159 | |||
160 | /** |
||
161 | * @return void |
||
162 | */ |
||
163 | 1 | public function save() |
|
166 | |||
167 | /** |
||
168 | * @param string $name |
||
169 | * |
||
170 | * @return void |
||
171 | * @throws FactoryException |
||
172 | */ |
||
173 | 1 | public function createWorkflow($name) |
|
176 | |||
177 | /** |
||
178 | * Иницализация путей по которым происходит поиск |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | 23 | protected function initDefaultPathsToWorkflows() |
|
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * |
||
191 | * @return void |
||
192 | * @throws FactoryException |
||
193 | * @throws InvalidParsingWorkflowException |
||
194 | */ |
||
195 | 11 | public function initDone() |
|
245 | |||
246 | /** |
||
247 | * @param $basedir |
||
248 | * @param $type |
||
249 | * @param $location |
||
250 | * |
||
251 | * @return WorkflowConfig |
||
252 | * @throws \OldTown\Workflow\Exception\RemoteException |
||
253 | */ |
||
254 | 7 | protected function buildWorkflowConfig($basedir, $type, $location) |
|
260 | |||
261 | /** |
||
262 | * @param string $name |
||
263 | * @param bool $validate |
||
264 | * |
||
265 | * @return WorkflowDescriptor |
||
266 | * @throws FactoryException |
||
267 | */ |
||
268 | 9 | public function getWorkflow($name, $validate = true) |
|
290 | |||
291 | /** |
||
292 | * @param WorkflowConfig $c |
||
293 | * @param boolean $validate |
||
294 | * |
||
295 | * @return void |
||
296 | * @throws FactoryException |
||
297 | */ |
||
298 | 8 | protected function loadWorkflow(WorkflowConfig $c, $validate = true) |
|
308 | |||
309 | /** |
||
310 | * Возвращает абсолютный путь до директории где находится workflow xml файл |
||
311 | * |
||
312 | * @param DOMElement $root |
||
313 | * |
||
314 | * @param $pathWorkflowFile |
||
315 | * |
||
316 | * @return string |
||
317 | * @throws RuntimeException |
||
318 | */ |
||
319 | 9 | protected function getBaseDir(DOMElement $root, $pathWorkflowFile) |
|
341 | |||
342 | |||
343 | |||
344 | |||
345 | |||
346 | /** |
||
347 | * @param string $name |
||
348 | * |
||
349 | * @return string |
||
350 | * @throws FactoryException |
||
351 | */ |
||
352 | 10 | protected function getPathWorkflowFile($name) |
|
375 | |||
376 | /** |
||
377 | * @return array |
||
378 | */ |
||
379 | 23 | public static function getDefaultPathsToWorkflows() |
|
383 | |||
384 | /** |
||
385 | * @param array $defaultPathsToWorkflows |
||
386 | */ |
||
387 | 23 | public static function setDefaultPathsToWorkflows(array $defaultPathsToWorkflows = []) |
|
391 | |||
392 | /** |
||
393 | * @param string $path |
||
394 | */ |
||
395 | 9 | public static function addDefaultPathToWorkflows($path) |
|
401 | |||
402 | /** |
||
403 | * Сохраняет workflow |
||
404 | * |
||
405 | * @param string $name имя workflow |
||
406 | * @param WorkflowDescriptor $descriptor descriptor workflow |
||
407 | * @param boolean $replace если true - то в случае существования одноименного workflow, оно будет |
||
408 | * заменено |
||
409 | * |
||
410 | * @return boolean true - если workflow было сохранено |
||
411 | * @throws FactoryException |
||
412 | * @throws UnsupportedOperationException |
||
413 | * @throws InvalidWriteWorkflowException |
||
414 | */ |
||
415 | 5 | public function saveWorkflow($name, WorkflowDescriptor $descriptor, $replace = false) |
|
464 | |||
465 | /** |
||
466 | * Архивирование оригинального файла workflow |
||
467 | * |
||
468 | * @param $original |
||
469 | * @param $backup |
||
470 | * |
||
471 | * @return bool |
||
472 | */ |
||
473 | 2 | protected function createBackupFile($original, $backup) |
|
479 | |||
480 | /** |
||
481 | * @param $newFileName |
||
482 | * @param $targetFile |
||
483 | * |
||
484 | * @return bool |
||
485 | */ |
||
486 | 1 | protected function createNewWorkflowFile($newFileName, $targetFile) |
|
492 | } |
||
493 |
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.