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 AbstractHandler 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 AbstractHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | abstract class AbstractHandler |
||
26 | { |
||
27 | /** |
||
28 | * Behaviours. |
||
29 | */ |
||
30 | const DROP_FOREIGNS = 0b000000001; |
||
31 | const CREATE_FOREIGNS = 0b000000010; |
||
32 | const ALTER_FOREIGNS = 0b000000100; |
||
33 | |||
34 | //All foreign keys related operations |
||
35 | const DO_FOREIGNS = self::DROP_FOREIGNS | self::ALTER_FOREIGNS | self::CREATE_FOREIGNS; |
||
36 | |||
37 | const DROP_COLUMNS = 0b000001000; |
||
38 | const CREATE_COLUMNS = 0b000010000; |
||
39 | const ALTER_COLUMNS = 0b000100000; |
||
40 | |||
41 | //All columns related operations |
||
42 | const DO_COLUMNS = self::DROP_COLUMNS | self::ALTER_COLUMNS | self::CREATE_COLUMNS; |
||
43 | |||
44 | const DROP_INDEXES = 0b001000000; |
||
45 | const CREATE_INDEXES = 0b010000000; |
||
46 | const ALTER_INDEXES = 0b100000000; |
||
47 | |||
48 | //All index related operations |
||
49 | const DO_INDEXES = self::DROP_INDEXES | self::ALTER_INDEXES | self::CREATE_INDEXES; |
||
50 | |||
51 | //Schema operations |
||
52 | const DO_RENAME = 0b10000000000; |
||
53 | const DO_DROP = 0b01000000000; |
||
54 | |||
55 | //All operations |
||
56 | const DO_ALL = self::DO_FOREIGNS | self::DO_INDEXES | self::DO_COLUMNS | self::DO_DROP | self::DO_RENAME; |
||
57 | |||
58 | /** |
||
59 | * @var LoggerInterface|null |
||
60 | */ |
||
61 | private $logger = null; |
||
62 | |||
63 | /** |
||
64 | * @var Driver |
||
65 | */ |
||
66 | protected $driver; |
||
67 | |||
68 | /** |
||
69 | * @param Driver $driver |
||
70 | * @param LoggerInterface|null $logger |
||
71 | */ |
||
72 | public function __construct(Driver $driver, LoggerInterface $logger = null) |
||
77 | |||
78 | /** |
||
79 | * Associated driver. |
||
80 | * |
||
81 | * @return Driver |
||
82 | */ |
||
83 | public function getDriver(): Driver |
||
87 | |||
88 | /** |
||
89 | * Create table based on a given schema. |
||
90 | * |
||
91 | * @param AbstractTable $table |
||
92 | * |
||
93 | * @throws HandlerException |
||
94 | */ |
||
95 | public function createTable(AbstractTable $table) |
||
107 | |||
108 | /** |
||
109 | * Drop table from database. |
||
110 | * |
||
111 | * @param AbstractTable $table |
||
112 | * |
||
113 | * @throws HandlerException |
||
114 | */ |
||
115 | public function dropTable(AbstractTable $table) |
||
120 | |||
121 | /** |
||
122 | * Sync given table schema. |
||
123 | * |
||
124 | * @param AbstractTable $table |
||
125 | * @param int $behaviour See behaviour constants. |
||
126 | */ |
||
127 | public function syncTable(AbstractTable $table, int $behaviour = self::DO_ALL) |
||
147 | |||
148 | /** |
||
149 | * Rename table from one name to another. |
||
150 | * |
||
151 | * @param string $table |
||
152 | * @param string $name |
||
153 | * |
||
154 | * @throws HandlerException |
||
155 | */ |
||
156 | public function renameTable(string $table, string $name) |
||
160 | |||
161 | /** |
||
162 | * Driver specific column add command. |
||
163 | * |
||
164 | * @param AbstractTable $table |
||
165 | * @param AbstractColumn $column |
||
166 | * |
||
167 | * @throws HandlerException |
||
168 | */ |
||
169 | public function createColumn(AbstractTable $table, AbstractColumn $column) |
||
173 | |||
174 | /** |
||
175 | * Driver specific column remove (drop) command. |
||
176 | * |
||
177 | * @param AbstractTable $table |
||
178 | * @param AbstractColumn $column |
||
179 | * |
||
180 | * @return self |
||
181 | */ |
||
182 | public function dropColumn(AbstractTable $table, AbstractColumn $column) |
||
191 | |||
192 | /** |
||
193 | * Driver specific column alter command. |
||
194 | * |
||
195 | * @param AbstractTable $table |
||
196 | * @param AbstractColumn $initial |
||
197 | * @param AbstractColumn $column |
||
198 | * |
||
199 | * @throws HandlerException |
||
200 | */ |
||
201 | abstract public function alterColumn( |
||
206 | |||
207 | /** |
||
208 | * Driver specific index adding command. |
||
209 | * |
||
210 | * @param AbstractTable $table |
||
211 | * @param AbstractIndex $index |
||
212 | * |
||
213 | * @throws HandlerException |
||
214 | */ |
||
215 | public function createIndex(AbstractTable $table, AbstractIndex $index) |
||
219 | |||
220 | /** |
||
221 | * Driver specific index remove (drop) command. |
||
222 | * |
||
223 | * @param AbstractTable $table |
||
224 | * @param AbstractIndex $index |
||
225 | * |
||
226 | * @throws HandlerException |
||
227 | */ |
||
228 | public function dropIndex(AbstractTable $table, AbstractIndex $index) |
||
232 | |||
233 | /** |
||
234 | * Driver specific index alter command, by default it will remove and add index. |
||
235 | * |
||
236 | * @param AbstractTable $table |
||
237 | * @param AbstractIndex $initial |
||
238 | * @param AbstractIndex $index |
||
239 | * |
||
240 | * @throws HandlerException |
||
241 | */ |
||
242 | public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index) |
||
247 | |||
248 | /** |
||
249 | * Driver specific foreign key adding command. |
||
250 | * |
||
251 | * @param AbstractTable $table |
||
252 | * @param AbstractReference $foreign |
||
253 | * |
||
254 | * @throws HandlerException |
||
255 | */ |
||
256 | public function createForeign(AbstractTable $table, AbstractReference $foreign) |
||
260 | |||
261 | /** |
||
262 | * Driver specific foreign key remove (drop) command. |
||
263 | * |
||
264 | * @param AbstractTable $table |
||
265 | * @param AbstractReference $foreign |
||
266 | * |
||
267 | * @throws HandlerException |
||
268 | */ |
||
269 | public function dropForeign(AbstractTable $table, AbstractReference $foreign) |
||
273 | |||
274 | /** |
||
275 | * Driver specific foreign key alter command, by default it will remove and add foreign key. |
||
276 | * |
||
277 | * @param AbstractTable $table |
||
278 | * @param AbstractReference $initial |
||
279 | * @param AbstractReference $foreign |
||
280 | * |
||
281 | * @throws HandlerException |
||
282 | */ |
||
283 | public function alterForeign( |
||
291 | |||
292 | /** |
||
293 | * Drop column constraint using it's name. |
||
294 | * |
||
295 | * @param AbstractTable $table |
||
296 | * @param string $constraint |
||
297 | * |
||
298 | * @throws HandlerException |
||
299 | */ |
||
300 | public function dropConstrain(AbstractTable $table, $constraint) |
||
304 | |||
305 | /** |
||
306 | * Get statement needed to create table. Indexes will be created separately. |
||
307 | * |
||
308 | * @param AbstractTable $table |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | protected function createStatement(AbstractTable $table) |
||
340 | |||
341 | /** |
||
342 | * @param AbstractTable $table |
||
343 | * @param int $behaviour |
||
344 | * @param StateComparator $comparator |
||
345 | */ |
||
346 | protected function runChanges(AbstractTable $table, int $behaviour, StateComparator $comparator) |
||
384 | |||
385 | /** |
||
386 | * Execute statement. |
||
387 | * |
||
388 | * @param string $statement |
||
389 | * @param array $parameters |
||
390 | * |
||
391 | * @return \PDOStatement |
||
392 | * |
||
393 | * @throws HandlerException |
||
394 | */ |
||
395 | protected function run(string $statement, array $parameters = []): \PDOStatement |
||
403 | |||
404 | /** |
||
405 | * Helper function, saves log message into logger if any attached. |
||
406 | * |
||
407 | * @param string $message |
||
408 | * @param array $context |
||
409 | */ |
||
410 | protected function log(string $message, array $context = []) |
||
416 | |||
417 | /** |
||
418 | * Create element identifier. |
||
419 | * |
||
420 | * @param AbstractElement|AbstractTable|string $element |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | protected function identify($element) |
||
436 | |||
437 | /** |
||
438 | * @param AbstractTable $table |
||
439 | * @param StateComparator $comparator |
||
440 | */ |
||
441 | View Code Duplication | protected function alterForeigns(AbstractTable $table, StateComparator $comparator) |
|
458 | |||
459 | /** |
||
460 | * @param AbstractTable $table |
||
461 | * @param StateComparator $comparator |
||
462 | */ |
||
463 | View Code Duplication | protected function createForeigns(AbstractTable $table, StateComparator $comparator) |
|
474 | |||
475 | /** |
||
476 | * @param AbstractTable $table |
||
477 | * @param StateComparator $comparator |
||
478 | */ |
||
479 | View Code Duplication | protected function alterIndexes(AbstractTable $table, StateComparator $comparator) |
|
497 | |||
498 | /** |
||
499 | * @param AbstractTable $table |
||
500 | * @param StateComparator $comparator |
||
501 | */ |
||
502 | View Code Duplication | protected function createIndexes(AbstractTable $table, StateComparator $comparator) |
|
513 | |||
514 | /** |
||
515 | * @param AbstractTable $table |
||
516 | * @param StateComparator $comparator |
||
517 | */ |
||
518 | View Code Duplication | protected function alterColumns(AbstractTable $table, StateComparator $comparator) |
|
537 | |||
538 | /** |
||
539 | * @param AbstractTable $table |
||
540 | * @param StateComparator $comparator |
||
541 | */ |
||
542 | View Code Duplication | protected function createColumns(AbstractTable $table, StateComparator $comparator) |
|
554 | |||
555 | /** |
||
556 | * @param AbstractTable $table |
||
557 | * @param StateComparator $comparator |
||
558 | */ |
||
559 | View Code Duplication | protected function dropColumns(AbstractTable $table, StateComparator $comparator) |
|
570 | |||
571 | /** |
||
572 | * @param AbstractTable $table |
||
573 | * @param StateComparator $comparator |
||
574 | */ |
||
575 | View Code Duplication | protected function dropIndexes(AbstractTable $table, StateComparator $comparator) |
|
586 | |||
587 | /** |
||
588 | * @param AbstractTable $table |
||
589 | * @param StateComparator $comparator |
||
590 | */ |
||
591 | View Code Duplication | protected function dropForeigns(AbstractTable $table, $comparator) |
|
602 | |||
603 | /** |
||
604 | * Applied to every column in order to make sure that driver support it. |
||
605 | * |
||
606 | * @param AbstractColumn $column |
||
607 | * |
||
608 | * @throws DriverException |
||
609 | */ |
||
610 | protected function assertValid(AbstractColumn $column) |
||
614 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.