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 | * Store a row in the cache. |
||
102 | */ |
||
103 | public function cache(Row $row): Row |
||
111 | |||
112 | /** |
||
113 | * Clear the current cache. |
||
114 | */ |
||
115 | public function clearCache(): self |
||
121 | |||
122 | /** |
||
123 | * Returns whether the id is cached or not |
||
124 | * @param mixed $id |
||
125 | */ |
||
126 | public function isCached($id): bool |
||
130 | |||
131 | /** |
||
132 | * Returns a row from the cache. |
||
133 | * @param mixed $id |
||
134 | */ |
||
135 | public function getCached($id): ?Row |
||
149 | |||
150 | public function delete(): Delete |
||
160 | |||
161 | public function insert(array $data = []): Insert |
||
171 | |||
172 | public function select(): Select |
||
182 | |||
183 | public function selectAggregate(string $function, string $field = 'id'): SelectAggregate |
||
193 | |||
194 | public function update(array $data = []): Update |
||
204 | |||
205 | /** |
||
206 | * Magic method to get the Field instance of a table field |
||
207 | */ |
||
208 | public function __get(string $name): Field |
||
218 | |||
219 | /** |
||
220 | * Magic method to check if a field exists or not. |
||
221 | */ |
||
222 | public function __isset(string $name): bool |
||
226 | |||
227 | /** |
||
228 | * @see Countable |
||
229 | */ |
||
230 | public function count(): int |
||
234 | |||
235 | /** |
||
236 | * Check if a row with a specific id exists. |
||
237 | * |
||
238 | * @see ArrayAccess |
||
239 | * @param mixed $offset |
||
240 | */ |
||
241 | public function offsetExists($offset): bool |
||
252 | |||
253 | /** |
||
254 | * Returns a row with a specific id. |
||
255 | * |
||
256 | * @see ArrayAccess |
||
257 | * |
||
258 | * @param mixed $offset |
||
259 | */ |
||
260 | public function offsetGet($offset): ?Row |
||
271 | |||
272 | /** |
||
273 | * Store a row with a specific id. |
||
274 | * |
||
275 | * @see ArrayAccess |
||
276 | * @param mixed $offset |
||
277 | * @param mixed $value |
||
278 | */ |
||
279 | public function offsetSet($offset, $value): Row |
||
303 | |||
304 | /** |
||
305 | * Remove a row with a specific id. |
||
306 | * |
||
307 | * @see ArrayAccess |
||
308 | * @param mixed $offset |
||
309 | */ |
||
310 | public function offsetUnset($offset) |
||
318 | |||
319 | /** |
||
320 | * Returns the table name. |
||
321 | */ |
||
322 | public function getName(): string |
||
326 | |||
327 | /** |
||
328 | * Returns the foreign key name. |
||
329 | */ |
||
330 | public function getForeignKey(): string |
||
334 | |||
335 | /** |
||
336 | * Returns the foreign key. |
||
337 | */ |
||
338 | public function getJoinField(Table $table): ?Field |
||
344 | |||
345 | public function getJoinTable(Table $table): ?Table |
||
359 | |||
360 | /** |
||
361 | * Returns the Database instance associated with this table. |
||
362 | */ |
||
363 | public function getDatabase(): Database |
||
367 | |||
368 | /** |
||
369 | * Returns all fields. |
||
370 | * |
||
371 | * @return Field[] |
||
372 | */ |
||
373 | public function getFields() |
||
377 | |||
378 | public function create(array $data = []): Row |
||
395 | |||
396 | public function createCollection(array $rows = []): RowCollection |
||
401 | |||
402 | public function createField(array $info): Field |
||
412 | } |
||
413 |
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: