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 |
||
| 27 | class Table implements ArrayAccess, Countable |
||
| 28 | { |
||
| 29 | private $name; |
||
| 30 | private $db; |
||
| 31 | private $cache = []; |
||
| 32 | private $fields = []; |
||
| 33 | private $defaults = []; |
||
| 34 | private $eventDispatcher; |
||
| 35 | private $fieldFactories; |
||
| 36 | |||
| 37 | protected const ROW_CLASS = Row::class; |
||
| 38 | protected const ROWCOLLECTION_CLASS = RowCollection::class; |
||
| 39 | |||
| 40 | final public function __construct(Database $db, string $name) |
||
| 57 | |||
| 58 | protected function init() |
||
| 62 | |||
| 63 | public function __debugInfo(): array |
||
| 70 | |||
| 71 | public function __toString() |
||
| 75 | |||
| 76 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): self |
||
| 82 | |||
| 83 | public function getEventDispatcher(): EventDispatcherInterface |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the devault values used in new rows |
||
| 94 | */ |
||
| 95 | public function getDefaults(array $overrides = null): array |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Format selected data from the database |
||
| 114 | */ |
||
| 115 | public function format(array $values): array |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Store a row in the cache. |
||
| 128 | */ |
||
| 129 | public function cache(Row $row): Row |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Clear the current cache. |
||
| 140 | */ |
||
| 141 | public function clearCache(): self |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns whether the id is cached or not |
||
| 150 | * @param mixed $id |
||
| 151 | */ |
||
| 152 | public function isCached($id): bool |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns a row from the cache. |
||
| 159 | * @param mixed $id |
||
| 160 | */ |
||
| 161 | public function getCached($id): ?Row |
||
| 175 | |||
| 176 | public function delete(): Delete |
||
| 186 | |||
| 187 | public function insert(array $data = []): Insert |
||
| 197 | |||
| 198 | public function select(): Select |
||
| 208 | |||
| 209 | public function selectAggregate(string $function, string $field = 'id'): SelectAggregate |
||
| 219 | |||
| 220 | public function update(array $data = []): Update |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Magic method to get the Field instance of a table field |
||
| 233 | */ |
||
| 234 | public function __get(string $name): Field |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Magic method to check if a field exists or not. |
||
| 247 | */ |
||
| 248 | public function __isset(string $name): bool |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @see Countable |
||
| 255 | */ |
||
| 256 | public function count(): int |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if a row with a specific id exists. |
||
| 263 | * |
||
| 264 | * @see ArrayAccess |
||
| 265 | * @param mixed $offset |
||
| 266 | */ |
||
| 267 | public function offsetExists($offset): bool |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns a row with a specific id. |
||
| 281 | * |
||
| 282 | * @see ArrayAccess |
||
| 283 | * |
||
| 284 | * @param mixed $offset |
||
| 285 | */ |
||
| 286 | public function offsetGet($offset): ?Row |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Store a row with a specific id. |
||
| 300 | * |
||
| 301 | * @see ArrayAccess |
||
| 302 | * @param mixed $offset |
||
| 303 | * @param mixed $value |
||
| 304 | */ |
||
| 305 | public function offsetSet($offset, $value): Row |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Remove a row with a specific id. |
||
| 332 | * |
||
| 333 | * @see ArrayAccess |
||
| 334 | * @param mixed $offset |
||
| 335 | */ |
||
| 336 | public function offsetUnset($offset) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the table name. |
||
| 347 | */ |
||
| 348 | public function getName(): string |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns the foreign key name. |
||
| 355 | */ |
||
| 356 | public function getForeignKey(): string |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns the foreign key. |
||
| 363 | */ |
||
| 364 | public function getJoinField(Table $table): ?Field |
||
| 370 | |||
| 371 | public function getJoinTable(Table $table): ?Table |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Returns the Database instance associated with this table. |
||
| 388 | */ |
||
| 389 | public function getDatabase(): Database |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Returns all fields. |
||
| 396 | * |
||
| 397 | * @return Field[] |
||
| 398 | */ |
||
| 399 | public function getFields() |
||
| 403 | |||
| 404 | public function create(array $data = []): Row |
||
| 421 | |||
| 422 | public function createCollection(array $rows = []): RowCollection |
||
| 427 | |||
| 428 | public function createField(array $info): Field |
||
| 438 | } |
||
| 439 |
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: