Complex classes like AbstractTable 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 AbstractTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | abstract class AbstractTable extends Component implements TableInterface, LoggerAwareInterface |
||
| 54 | { |
||
| 55 | use LoggerTrait; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Indication that table is exists and current schema is fetched from database. |
||
| 59 | * |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | private $exists = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Database specific tablePrefix. Required for table renames. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $prefix = ''; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @invisible |
||
| 73 | * |
||
| 74 | * @var Driver |
||
| 75 | */ |
||
| 76 | protected $driver = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Initial table state. |
||
| 80 | * |
||
| 81 | * @invisible |
||
| 82 | * @var TableState |
||
| 83 | */ |
||
| 84 | protected $initialState = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Currently defined table state. |
||
| 88 | * |
||
| 89 | * @invisible |
||
| 90 | * @var TableState |
||
| 91 | */ |
||
| 92 | protected $currentState = null; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param Driver $driver Parent driver. |
||
| 96 | * @param string $name Table name, must include table prefix. |
||
| 97 | * @param string $prefix Database specific table prefix. |
||
| 98 | */ |
||
| 99 | public function __construct(Driver $driver, string $name, string $prefix) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get instance of associated driver. |
||
| 120 | * |
||
| 121 | * @return Driver |
||
| 122 | */ |
||
| 123 | public function getDriver(): Driver |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return StateComparator |
||
| 130 | */ |
||
| 131 | public function getComparator(): StateComparator |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if table schema has been modified since synchronization. |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | protected function hasChanges(): bool |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | public function exists(): bool |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return database specific table prefix. |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function getPrefix(): string |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Sets table name. Use this function in combination with save to rename table. |
||
| 166 | * |
||
| 167 | * @param string $name |
||
| 168 | * |
||
| 169 | * @return string Prefixed table name. |
||
| 170 | */ |
||
| 171 | public function setName(string $name): string |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function getName(): string |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set table primary keys. Operation can only be applied for newly created tables. Now every |
||
| 188 | * database might support compound indexes. |
||
| 189 | * |
||
| 190 | * @param array $columns |
||
| 191 | * |
||
| 192 | * @return self |
||
| 193 | */ |
||
| 194 | public function setPrimaryKeys(array $columns): AbstractTable |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | public function getPrimaryKeys(): array |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function hasColumn(string $name): bool |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | * |
||
| 224 | * @return ColumnInterface[]|AbstractColumn[] |
||
| 225 | */ |
||
| 226 | public function getColumns(): array |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | public function hasIndex(array $columns = []): bool |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | * |
||
| 242 | * @return IndexInterface[]|AbstractIndex[] |
||
| 243 | */ |
||
| 244 | public function getIndexes(): array |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function hasForeign(string $column): bool |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | * |
||
| 260 | * @return ReferenceInterface[]|AbstractReference[] |
||
| 261 | */ |
||
| 262 | public function getReferences(): array |
||
| 263 | { |
||
| 264 | return $this->currentState->getForeigns(); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function getDependencies(): array |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get/create instance of AbstractColumn associated with current table. |
||
| 282 | * |
||
| 283 | * Examples: |
||
| 284 | * $table->column('name')->string(); |
||
| 285 | * |
||
| 286 | * @param string $name |
||
| 287 | * |
||
| 288 | * @return AbstractColumn |
||
| 289 | */ |
||
| 290 | public function column(string $name): AbstractColumn |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Shortcut for column() method. |
||
| 305 | * |
||
| 306 | * @param string $column |
||
| 307 | * |
||
| 308 | * @return AbstractColumn |
||
| 309 | */ |
||
| 310 | public function __get(string $column) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get/create instance of AbstractIndex associated with current table based on list of forming |
||
| 317 | * column names. |
||
| 318 | * |
||
| 319 | * Example: |
||
| 320 | * $table->index('key'); |
||
| 321 | * $table->index('key', 'key2'); |
||
| 322 | * $table->index(['key', 'key2']); |
||
| 323 | * |
||
| 324 | * @param mixed $columns Column name, or array of columns. |
||
| 325 | * |
||
| 326 | * @return AbstractIndex |
||
| 327 | * |
||
| 328 | * @throws SchemaException |
||
| 329 | */ |
||
| 330 | public function index($columns): AbstractIndex |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get/create instance of AbstractReference associated with current table based on local column |
||
| 353 | * name. |
||
| 354 | * |
||
| 355 | * @param string $column |
||
| 356 | * |
||
| 357 | * @return AbstractReference |
||
| 358 | */ |
||
| 359 | public function references(string $column): AbstractReference |
||
| 375 | |||
| 376 | //------ |
||
| 377 | //Altering operations |
||
| 378 | //------ |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * Column creation/altering shortcut, call chain is identical to: |
||
| 383 | * AbstractTable->column($name)->$type($arguments). |
||
| 384 | * |
||
| 385 | * Example: |
||
| 386 | * $table->string("name"); |
||
| 387 | * $table->text("some_column"); |
||
| 388 | * |
||
| 389 | * @param string $type |
||
| 390 | * @param array $arguments Type specific parameters. |
||
| 391 | * |
||
| 392 | * @return AbstractColumn |
||
| 393 | */ |
||
| 394 | public function __call(string $type, array $arguments) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Reset table state to new form. |
||
| 404 | * |
||
| 405 | * @param TableState $state Use null to flush table schema. |
||
| 406 | * |
||
| 407 | * @return self|$this |
||
| 408 | */ |
||
| 409 | public function setState(TableState $state = null): AbstractTable |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Reset table state to it initial form. |
||
| 423 | * |
||
| 424 | * @return self|$this |
||
| 425 | */ |
||
| 426 | public function resetState(): AbstractTable |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return AbstractColumn|string |
||
| 435 | */ |
||
| 436 | public function __toString(): string |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | public function __debugInfo() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Populate table schema with values from database. |
||
| 457 | * |
||
| 458 | * @param TableState $state |
||
| 459 | */ |
||
| 460 | protected function initSchema(TableState $state) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Fetch index declarations from database. |
||
| 481 | * |
||
| 482 | * @return ColumnInterface[] |
||
| 483 | */ |
||
| 484 | abstract protected function fetchColumns(): array; |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Fetch index declarations from database. |
||
| 488 | * |
||
| 489 | * @return IndexInterface[] |
||
| 490 | */ |
||
| 491 | abstract protected function fetchIndexes(): array; |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Fetch references declaration from database. |
||
| 495 | * |
||
| 496 | * @return ReferenceInterface[] |
||
| 497 | */ |
||
| 498 | abstract protected function fetchReferences(): array; |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Fetch names of primary keys from table. |
||
| 502 | * |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | abstract protected function fetchPrimaryKeys(): array; |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Create column with a given name. |
||
| 509 | * |
||
| 510 | * @param string $name |
||
| 511 | * |
||
| 512 | * @return AbstractColumn |
||
| 513 | */ |
||
| 514 | abstract protected function createColumn(string $name): AbstractColumn; |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Create index for a given set of columns. |
||
| 518 | * |
||
| 519 | * @param string $name |
||
| 520 | * |
||
| 521 | * @return AbstractIndex |
||
| 522 | */ |
||
| 523 | abstract protected function createIndex(string $name): AbstractIndex; |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Create reference on a given column set. |
||
| 527 | * |
||
| 528 | * @param string $name |
||
| 529 | * |
||
| 530 | * @return AbstractReference |
||
| 531 | */ |
||
| 532 | abstract protected function createReference(string $name): AbstractReference; |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Generate unique name for indexes and foreign keys. |
||
| 536 | * |
||
| 537 | * @param string $type |
||
| 538 | * @param array $columns |
||
| 539 | * |
||
| 540 | * @return string |
||
| 541 | */ |
||
| 542 | protected function createIdentifier(string $type, array $columns): string |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @return ContainerInterface |
||
| 556 | */ |
||
| 557 | protected function iocContainer() |
||
| 562 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: