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 |
||
| 24 | abstract class AbstractHandler |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Behaviours. |
||
| 28 | */ |
||
| 29 | const DROP_FOREIGNS = 0b000000001; |
||
| 30 | const CREATE_FOREIGNS = 0b000000010; |
||
| 31 | const ALTER_FOREIGNS = 0b000000100; |
||
| 32 | |||
| 33 | //All foreign keys related operations |
||
| 34 | const DO_FOREIGNS = self::DROP_FOREIGNS | self::ALTER_FOREIGNS | self::CREATE_FOREIGNS; |
||
| 35 | |||
| 36 | const DROP_COLUMNS = 0b000001000; |
||
| 37 | const CREATE_COLUMNS = 0b000010000; |
||
| 38 | const ALTER_COLUMNS = 0b000100000; |
||
| 39 | |||
| 40 | //All columns related operations |
||
| 41 | const DO_COLUMNS = self::DROP_COLUMNS | self::ALTER_COLUMNS | self::CREATE_COLUMNS; |
||
| 42 | |||
| 43 | const DROP_INDEXES = 0b001000000; |
||
| 44 | const CREATE_INDEXES = 0b010000000; |
||
| 45 | const ALTER_INDEXES = 0b100000000; |
||
| 46 | |||
| 47 | //All index related operations |
||
| 48 | const DO_INDEXES = self::DROP_INDEXES | self::ALTER_INDEXES | self::CREATE_INDEXES; |
||
| 49 | |||
| 50 | //All operations |
||
| 51 | const DO_ALL = self::DO_FOREIGNS | self::DO_INDEXES | self::DO_COLUMNS; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var LoggerInterface|null |
||
| 55 | */ |
||
| 56 | private $logger = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Driver |
||
| 60 | */ |
||
| 61 | protected $driver; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param Driver $driver |
||
| 65 | * @param LoggerInterface|null $logger |
||
| 66 | */ |
||
| 67 | public function __construct(Driver $driver, LoggerInterface $logger = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Associated driver. |
||
| 75 | * |
||
| 76 | * @return Driver |
||
| 77 | */ |
||
| 78 | public function getDriver(): Driver |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Create table based on a given schema. |
||
| 85 | * |
||
| 86 | * @param AbstractTable $table |
||
| 87 | * |
||
| 88 | * @throws HandlerException |
||
| 89 | */ |
||
| 90 | public function createTable(AbstractTable $table) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Drop table from database. |
||
| 105 | * |
||
| 106 | * @param AbstractTable $table |
||
| 107 | * |
||
| 108 | * @throws HandlerException |
||
| 109 | */ |
||
| 110 | public function dropTable(AbstractTable $table) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Sync given table schema. |
||
| 119 | * |
||
| 120 | * @param AbstractTable $table |
||
| 121 | * @param int $behaviour See behaviour constants. |
||
| 122 | */ |
||
| 123 | public function syncTable(AbstractTable $table, int $behaviour = self::DO_ALL) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Rename table from one name to another. |
||
| 180 | * |
||
| 181 | * @param string $table |
||
| 182 | * @param string $name |
||
| 183 | * |
||
| 184 | * @throws HandlerException |
||
| 185 | */ |
||
| 186 | public function renameTable(string $table, string $name) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Driver specific column add command. |
||
| 193 | * |
||
| 194 | * @param AbstractTable $table |
||
| 195 | * @param AbstractColumn $column |
||
| 196 | * |
||
| 197 | * @throws HandlerException |
||
| 198 | */ |
||
| 199 | public function createColumn(AbstractTable $table, AbstractColumn $column) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Driver specific column remove (drop) command. |
||
| 206 | * |
||
| 207 | * @param AbstractTable $table |
||
| 208 | * @param AbstractColumn $column |
||
| 209 | * |
||
| 210 | * @return self |
||
| 211 | */ |
||
| 212 | public function dropColumn(AbstractTable $table, AbstractColumn $column) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Driver specific column alter command. |
||
| 224 | * |
||
| 225 | * @param AbstractTable $table |
||
| 226 | * @param AbstractColumn $initial |
||
| 227 | * @param AbstractColumn $column |
||
| 228 | * |
||
| 229 | * @throws HandlerException |
||
| 230 | */ |
||
| 231 | abstract public function alterColumn( |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Driver specific index adding command. |
||
| 239 | * |
||
| 240 | * @param AbstractTable $table |
||
| 241 | * @param AbstractIndex $index |
||
| 242 | * |
||
| 243 | * @throws HandlerException |
||
| 244 | */ |
||
| 245 | public function createIndex(AbstractTable $table, AbstractIndex $index) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Driver specific index remove (drop) command. |
||
| 252 | * |
||
| 253 | * @param AbstractTable $table |
||
| 254 | * @param AbstractIndex $index |
||
| 255 | * |
||
| 256 | * @throws HandlerException |
||
| 257 | */ |
||
| 258 | public function dropIndex(AbstractTable $table, AbstractIndex $index) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Driver specific index alter command, by default it will remove and add index. |
||
| 265 | * |
||
| 266 | * @param AbstractTable $table |
||
| 267 | * @param AbstractIndex $initial |
||
| 268 | * @param AbstractIndex $index |
||
| 269 | * |
||
| 270 | * @throws HandlerException |
||
| 271 | */ |
||
| 272 | public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Driver specific foreign key adding command. |
||
| 280 | * |
||
| 281 | * @param AbstractTable $table |
||
| 282 | * @param AbstractReference $foreign |
||
| 283 | * |
||
| 284 | * @throws HandlerException |
||
| 285 | */ |
||
| 286 | public function createForeign(AbstractTable $table, AbstractReference $foreign) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Driver specific foreign key remove (drop) command. |
||
| 293 | * |
||
| 294 | * @param AbstractTable $table |
||
| 295 | * @param AbstractReference $foreign |
||
| 296 | * |
||
| 297 | * @throws HandlerException |
||
| 298 | */ |
||
| 299 | public function dropForeign(AbstractTable $table, AbstractReference $foreign) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Driver specific foreign key alter command, by default it will remove and add foreign key. |
||
| 306 | * |
||
| 307 | * @param AbstractTable $table |
||
| 308 | * @param AbstractReference $initial |
||
| 309 | * @param AbstractReference $foreign |
||
| 310 | * |
||
| 311 | * @throws HandlerException |
||
| 312 | */ |
||
| 313 | public function alterForeign( |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Drop column constraint using it's name. |
||
| 324 | * |
||
| 325 | * @param AbstractTable $table |
||
| 326 | * @param string $constraint |
||
| 327 | * |
||
| 328 | * @throws HandlerException |
||
| 329 | */ |
||
| 330 | public function dropConstrain(AbstractTable $table, $constraint) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get statement needed to create table. Indexes will be created separately. |
||
| 337 | * |
||
| 338 | * @param AbstractTable $table |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | protected function createStatement(AbstractTable $table) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Execute statement. |
||
| 372 | * |
||
| 373 | * @param string $statement |
||
| 374 | * @param array $parameters |
||
| 375 | * |
||
| 376 | * @return \PDOStatement |
||
| 377 | * |
||
| 378 | * @throws HandlerException |
||
| 379 | */ |
||
| 380 | protected function run(string $statement, array $parameters = []): \PDOStatement |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Helper function, saves log message into logger if any attached. |
||
| 391 | * |
||
| 392 | * @param string $message |
||
| 393 | * @param array $context |
||
| 394 | */ |
||
| 395 | protected function log(string $message, array $context = []) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Create element identifier. |
||
| 404 | * |
||
| 405 | * @param AbstractElement|AbstractTable|string $element |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | protected function identify($element) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param AbstractTable $table |
||
| 424 | * @param StateComparator $comparator |
||
| 425 | */ |
||
| 426 | View Code Duplication | protected function alterForeigns(AbstractTable $table, StateComparator $comparator) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @param AbstractTable $table |
||
| 446 | * @param StateComparator $comparator |
||
| 447 | */ |
||
| 448 | View Code Duplication | protected function createForeigns(AbstractTable $table, StateComparator $comparator) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @param AbstractTable $table |
||
| 462 | * @param StateComparator $comparator |
||
| 463 | */ |
||
| 464 | View Code Duplication | protected function alterIndexes(AbstractTable $table, StateComparator $comparator) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @param AbstractTable $table |
||
| 485 | * @param StateComparator $comparator |
||
| 486 | */ |
||
| 487 | View Code Duplication | protected function createIndexes(AbstractTable $table, StateComparator $comparator) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @param AbstractTable $table |
||
| 501 | * @param StateComparator $comparator |
||
| 502 | */ |
||
| 503 | View Code Duplication | protected function alterColumns(AbstractTable $table, StateComparator $comparator) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param AbstractTable $table |
||
| 524 | * @param StateComparator $comparator |
||
| 525 | */ |
||
| 526 | View Code Duplication | protected function createColumns(AbstractTable $table, StateComparator $comparator) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @param AbstractTable $table |
||
| 540 | * @param StateComparator $comparator |
||
| 541 | */ |
||
| 542 | View Code Duplication | protected function dropColumns(AbstractTable $table, StateComparator $comparator) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * @param AbstractTable $table |
||
| 556 | * @param StateComparator $comparator |
||
| 557 | */ |
||
| 558 | View Code Duplication | protected function dropIndexes(AbstractTable $table, StateComparator $comparator) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * @param AbstractTable $table |
||
| 572 | * @param StateComparator $comparator |
||
| 573 | */ |
||
| 574 | View Code Duplication | protected function dropForeigns(AbstractTable $table, $comparator) |
|
| 585 | } |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.