Complex classes like Table 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 Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Table implements ArrayAccess, Countable |
||
| 27 | { |
||
| 28 | private $name; |
||
| 29 | private $db; |
||
| 30 | private $cache = []; |
||
| 31 | private $fields = []; |
||
| 32 | private $defaults = []; |
||
| 33 | private $eventDispatcher; |
||
| 34 | private $fieldFactories; |
||
| 35 | |||
| 36 | protected const ROW_CLASS = Row::class; |
||
| 37 | protected const ROWCOLLECTION_CLASS = RowCollection::class; |
||
| 38 | |||
| 39 | final public function __construct(Database $db, string $name) |
||
| 54 | |||
| 55 | public function __debugInfo(): array |
||
| 62 | |||
| 63 | public function __toString() |
||
| 67 | |||
| 68 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): self |
||
| 74 | |||
| 75 | public function getEventDispatcher(): ?EventDispatcherInterface |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get the devault values used in new rows |
||
| 82 | */ |
||
| 83 | public function getDefaults(array $overrides = null): array |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Format selected data from the database |
||
| 102 | */ |
||
| 103 | public function format(array $values): array |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Store a row in the cache. |
||
| 116 | */ |
||
| 117 | public function cache(Row $row): Row |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Clear the current cache. |
||
| 128 | */ |
||
| 129 | public function clearCache(): self |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns whether the id is cached or not |
||
| 138 | * @param mixed $id |
||
| 139 | */ |
||
| 140 | public function isCached($id): bool |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns a row from the cache. |
||
| 147 | * @param mixed $id |
||
| 148 | */ |
||
| 149 | public function getCached($id): ?Row |
||
| 163 | |||
| 164 | public function delete(): Delete |
||
| 174 | |||
| 175 | public function insert(array $data = []): Insert |
||
| 185 | |||
| 186 | public function select(): Select |
||
| 196 | |||
| 197 | public function selectAggregate(string $function, string $field = 'id'): SelectAggregate |
||
| 207 | |||
| 208 | public function update(array $data = []): Update |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Magic method to get the Field instance of a table field |
||
| 221 | */ |
||
| 222 | public function __get(string $name): Field |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Magic method to check if a field exists or not. |
||
| 235 | */ |
||
| 236 | public function __isset(string $name): bool |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @see Countable |
||
| 243 | */ |
||
| 244 | public function count(): int |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Check if a row with a specific id exists. |
||
| 251 | * |
||
| 252 | * @see ArrayAccess |
||
| 253 | * @param mixed $offset |
||
| 254 | */ |
||
| 255 | public function offsetExists($offset): bool |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns a row with a specific id. |
||
| 269 | * |
||
| 270 | * @see ArrayAccess |
||
| 271 | * |
||
| 272 | * @param mixed $offset |
||
| 273 | */ |
||
| 274 | public function offsetGet($offset): ?Row |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Store a row with a specific id. |
||
| 288 | * |
||
| 289 | * @see ArrayAccess |
||
| 290 | * @param mixed $offset |
||
| 291 | * @param mixed $value |
||
| 292 | */ |
||
| 293 | public function offsetSet($offset, $value): Row |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Remove a row with a specific id. |
||
| 320 | * |
||
| 321 | * @see ArrayAccess |
||
| 322 | * @param mixed $offset |
||
| 323 | */ |
||
| 324 | public function offsetUnset($offset) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Returns the table name. |
||
| 335 | */ |
||
| 336 | public function getName(): string |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the foreign key name. |
||
| 343 | */ |
||
| 344 | public function getForeignKey(): string |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Returns the foreign key. |
||
| 351 | */ |
||
| 352 | public function getJoinField(Table $table): ?Field |
||
| 358 | |||
| 359 | public function getJoinTable(Table $table): ?Table |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Returns the Database instance associated with this table. |
||
| 376 | */ |
||
| 377 | public function getDatabase(): Database |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns all fields. |
||
| 384 | * |
||
| 385 | * @return Field[] |
||
| 386 | */ |
||
| 387 | public function getFields() |
||
| 391 | |||
| 392 | public function create(array $data = []): Row |
||
| 409 | |||
| 410 | public function createCollection(array $rows = []): RowCollection |
||
| 415 | |||
| 416 | public function createField(array $info): Field |
||
| 426 | } |
||
| 427 |
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: