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 | //All operations |
||
| 52 | const DO_ALL = self::DO_FOREIGNS | self::DO_INDEXES | self::DO_COLUMNS; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var LoggerInterface|null |
||
| 56 | */ |
||
| 57 | private $logger = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Driver |
||
| 61 | */ |
||
| 62 | protected $driver; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Driver $driver |
||
| 66 | * @param LoggerInterface|null $logger |
||
| 67 | */ |
||
| 68 | public function __construct(Driver $driver, LoggerInterface $logger = null) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Associated driver. |
||
| 76 | * |
||
| 77 | * @return Driver |
||
| 78 | */ |
||
| 79 | public function getDriver(): Driver |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Create table based on a given schema. |
||
| 86 | * |
||
| 87 | * @param AbstractTable $table |
||
| 88 | * |
||
| 89 | * @throws HandlerException |
||
| 90 | */ |
||
| 91 | public function createTable(AbstractTable $table) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Drop table from database. |
||
| 106 | * |
||
| 107 | * @param AbstractTable $table |
||
| 108 | * |
||
| 109 | * @throws HandlerException |
||
| 110 | */ |
||
| 111 | public function dropTable(AbstractTable $table) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sync given table schema. |
||
| 120 | * |
||
| 121 | * @param AbstractTable $table |
||
| 122 | * @param int $behaviour See behaviour constants. |
||
| 123 | */ |
||
| 124 | public function syncTable(AbstractTable $table, int $behaviour = self::DO_ALL) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Rename table from one name to another. |
||
| 267 | * |
||
| 268 | * @param string $table |
||
| 269 | * @param string $name |
||
| 270 | * |
||
| 271 | * @throws HandlerException |
||
| 272 | */ |
||
| 273 | public function renameTable(string $table, string $name) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Driver specific column add command. |
||
| 280 | * |
||
| 281 | * @param AbstractTable $table |
||
| 282 | * @param AbstractColumn $column |
||
| 283 | * |
||
| 284 | * @throws HandlerException |
||
| 285 | */ |
||
| 286 | public function createColumn(AbstractTable $table, AbstractColumn $column) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Driver specific column remove (drop) command. |
||
| 293 | * |
||
| 294 | * @param AbstractTable $table |
||
| 295 | * @param AbstractColumn $column |
||
| 296 | * |
||
| 297 | * @return self |
||
| 298 | */ |
||
| 299 | public function dropColumn(AbstractTable $table, AbstractColumn $column) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Driver specific column alter command. |
||
| 311 | * |
||
| 312 | * @param AbstractTable $table |
||
| 313 | * @param AbstractColumn $initial |
||
| 314 | * @param AbstractColumn $column |
||
| 315 | * |
||
| 316 | * @throws HandlerException |
||
| 317 | */ |
||
| 318 | abstract public function alterColumn( |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Driver specific index adding command. |
||
| 326 | * |
||
| 327 | * @param AbstractTable $table |
||
| 328 | * @param AbstractIndex $index |
||
| 329 | * |
||
| 330 | * @throws HandlerException |
||
| 331 | */ |
||
| 332 | public function createIndex(AbstractTable $table, AbstractIndex $index) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Driver specific index remove (drop) command. |
||
| 339 | * |
||
| 340 | * @param AbstractTable $table |
||
| 341 | * @param AbstractIndex $index |
||
| 342 | * |
||
| 343 | * @throws HandlerException |
||
| 344 | */ |
||
| 345 | public function dropIndex(AbstractTable $table, AbstractIndex $index) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Driver specific index alter command, by default it will remove and add index. |
||
| 352 | * |
||
| 353 | * @param AbstractTable $table |
||
| 354 | * @param AbstractIndex $initial |
||
| 355 | * @param AbstractIndex $index |
||
| 356 | * |
||
| 357 | * @throws HandlerException |
||
| 358 | */ |
||
| 359 | public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Driver specific foreign key adding command. |
||
| 367 | * |
||
| 368 | * @param AbstractTable $table |
||
| 369 | * @param AbstractReference $foreign |
||
| 370 | * |
||
| 371 | * @throws HandlerException |
||
| 372 | */ |
||
| 373 | public function createForeign(AbstractTable $table, AbstractReference $foreign) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Driver specific foreign key remove (drop) command. |
||
| 380 | * |
||
| 381 | * @param AbstractTable $table |
||
| 382 | * @param AbstractReference $foreign |
||
| 383 | * |
||
| 384 | * @throws HandlerException |
||
| 385 | */ |
||
| 386 | public function dropForeign(AbstractTable $table, AbstractReference $foreign) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Driver specific foreign key alter command, by default it will remove and add foreign key. |
||
| 393 | * |
||
| 394 | * @param AbstractTable $table |
||
| 395 | * @param AbstractReference $initial |
||
| 396 | * @param AbstractReference $foreign |
||
| 397 | * |
||
| 398 | * @throws HandlerException |
||
| 399 | */ |
||
| 400 | public function alterForeign( |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Drop column constraint using it's name. |
||
| 411 | * |
||
| 412 | * @param AbstractTable $table |
||
| 413 | * @param string $constraint |
||
| 414 | * |
||
| 415 | * @throws HandlerException |
||
| 416 | */ |
||
| 417 | public function dropConstrain(AbstractTable $table, $constraint) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get statement needed to create table. Indexes will be created separately. |
||
| 424 | * |
||
| 425 | * @param AbstractTable $table |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | protected function createStatement(AbstractTable $table) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Execute statement. |
||
| 459 | * |
||
| 460 | * @param string $statement |
||
| 461 | * @param array $parameters |
||
| 462 | * |
||
| 463 | * @return \PDOStatement |
||
| 464 | * |
||
| 465 | * @throws HandlerException |
||
| 466 | */ |
||
| 467 | protected function run(string $statement, array $parameters = []): \PDOStatement |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Helper function, saves log message into logger if any attached. |
||
| 478 | * |
||
| 479 | * @param string $message |
||
| 480 | * @param array $context |
||
| 481 | */ |
||
| 482 | protected function log(string $message, array $context = []) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Create element identifier. |
||
| 493 | * |
||
| 494 | * @param AbstractElement|AbstractTable|string $element |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | protected function identify($element) |
||
| 510 | } |
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.