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(): void |
||
| 61 | |||
| 62 | public function __debugInfo(): array |
||
| 69 | |||
| 70 | public function __toString() |
||
| 74 | |||
| 75 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): self |
||
| 81 | |||
| 82 | public function getEventDispatcher(): EventDispatcherInterface |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the devault values used in new rows |
||
| 93 | */ |
||
| 94 | public function getDefaults(array $overrides = null): array |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Format selected data from the database |
||
| 113 | */ |
||
| 114 | public function format(array $values): array |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Store a row in the cache. |
||
| 127 | */ |
||
| 128 | public function cache(Row $row): Row |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Clear the current cache. |
||
| 139 | */ |
||
| 140 | public function clearCache(): self |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns whether the id is cached or not |
||
| 149 | * @param mixed $id |
||
| 150 | */ |
||
| 151 | public function isCached($id): bool |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns a row from the cache. |
||
| 158 | * @param mixed $id |
||
| 159 | */ |
||
| 160 | public function getCached($id): ?Row |
||
| 174 | |||
| 175 | public function delete(): Delete |
||
| 185 | |||
| 186 | public function insert(array $data = []): Insert |
||
| 196 | |||
| 197 | public function select(): Select |
||
| 207 | |||
| 208 | public function selectAggregate(string $function, string $field = 'id'): SelectAggregate |
||
| 218 | |||
| 219 | public function update(array $data = []): Update |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Magic method to get the Field instance of a table field |
||
| 232 | */ |
||
| 233 | public function __get(string $name): Field |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Magic method to check if a field exists or not. |
||
| 246 | */ |
||
| 247 | public function __isset(string $name): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @see Countable |
||
| 254 | */ |
||
| 255 | public function count(): int |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Check if a row with a specific id exists. |
||
| 262 | * |
||
| 263 | * @see ArrayAccess |
||
| 264 | * @param mixed $offset |
||
| 265 | */ |
||
| 266 | public function offsetExists($offset): bool |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns a row with a specific id. |
||
| 280 | * |
||
| 281 | * @see ArrayAccess |
||
| 282 | * |
||
| 283 | * @param mixed $offset |
||
| 284 | */ |
||
| 285 | public function offsetGet($offset): ?Row |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Store a row with a specific id. |
||
| 299 | * |
||
| 300 | * @see ArrayAccess |
||
| 301 | * @param mixed $offset |
||
| 302 | * @param mixed $value |
||
| 303 | */ |
||
| 304 | public function offsetSet($offset, $value): Row |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Remove a row with a specific id. |
||
| 331 | * |
||
| 332 | * @see ArrayAccess |
||
| 333 | * @param mixed $offset |
||
| 334 | */ |
||
| 335 | public function offsetUnset($offset) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns the table name. |
||
| 346 | */ |
||
| 347 | public function getName(): string |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Returns the foreign key name. |
||
| 354 | */ |
||
| 355 | public function getForeignKey(): string |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns the foreign key. |
||
| 362 | */ |
||
| 363 | public function getJoinField(Table $table): ?Field |
||
| 369 | |||
| 370 | public function getJoinTable(Table $table): ?Table |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the Database instance associated with this table. |
||
| 387 | */ |
||
| 388 | public function getDatabase(): Database |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns all fields. |
||
| 395 | * |
||
| 396 | * @return Field[] |
||
| 397 | */ |
||
| 398 | public function getFields() |
||
| 402 | |||
| 403 | public function __call(string $name, array $args): ?Row |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Search a row with some values or create one if it does not exist |
||
| 412 | */ |
||
| 413 | public function getOrCreate(array $data): Row |
||
| 431 | |||
| 432 | public function create(array $data = []): Row |
||
| 449 | |||
| 450 | public function createCollection(array $rows = []): RowCollection |
||
| 455 | |||
| 456 | public function createField(array $info): Field |
||
| 466 | } |
||
| 467 |
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: