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 |
||
| 17 | class Table implements ArrayAccess |
||
| 18 | { |
||
| 19 | private $name; |
||
| 20 | private $db; |
||
| 21 | private $cache = []; |
||
| 22 | private $fields = []; |
||
| 23 | private $defaults = []; |
||
| 24 | private $eventDispatcher; |
||
| 25 | |||
| 26 | protected const ROW_CLASS = Row::class; |
||
| 27 | protected const ROWCOLLECTION_CLASS = RowCollection::class; |
||
| 28 | |||
| 29 | final public function __construct(Database $db, string $name) |
||
| 44 | |||
| 45 | public function __debugInfo(): array |
||
| 52 | |||
| 53 | public function __toString() |
||
| 57 | |||
| 58 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): self |
||
| 64 | |||
| 65 | public function getEventDispatcher(): ?EventDispatcherInterface |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the devault values used in new rows |
||
| 72 | */ |
||
| 73 | public function getDefaults(array $overrides = null): array |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Store a row in the cache. |
||
| 94 | */ |
||
| 95 | public function cache(Row $row): Row |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Clear the current cache. |
||
| 106 | */ |
||
| 107 | public function clearCache(): self |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns whether the id is cached or not |
||
| 116 | * @param mixed $id |
||
| 117 | */ |
||
| 118 | public function isCached($id): bool |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns a row from the cache. |
||
| 125 | * @param mixed $id |
||
| 126 | */ |
||
| 127 | public function getCached($id): ?Row |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Magic method to create queries related with this table. |
||
| 144 | */ |
||
| 145 | public function __call(string $name, array $arguments): QueryInterface |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Magic method to get the Field instance of a table field |
||
| 154 | */ |
||
| 155 | public function __get(string $name): FieldInterface |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Magic method to check if a field exists or not. |
||
| 168 | */ |
||
| 169 | public function __isset(string $name): bool |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Check if a row with a specific id exists. |
||
| 176 | * |
||
| 177 | * @see ArrayAccess |
||
| 178 | * @param mixed $offset |
||
| 179 | */ |
||
| 180 | public function offsetExists($offset): bool |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns a row with a specific id. |
||
| 194 | * |
||
| 195 | * @see ArrayAccess |
||
| 196 | * |
||
| 197 | * @param mixed $offset |
||
| 198 | */ |
||
| 199 | public function offsetGet($offset): ?Row |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Store a row with a specific id. |
||
| 213 | * |
||
| 214 | * @see ArrayAccess |
||
| 215 | * @param mixed $offset |
||
| 216 | * @param mixed $value |
||
| 217 | */ |
||
| 218 | public function offsetSet($offset, $value): Row |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Remove a row with a specific id. |
||
| 245 | * |
||
| 246 | * @see ArrayAccess |
||
| 247 | * @param mixed $offset |
||
| 248 | */ |
||
| 249 | public function offsetUnset($offset) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the table name. |
||
| 260 | */ |
||
| 261 | public function getName(): string |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the foreign key name. |
||
| 268 | */ |
||
| 269 | public function getForeignKey(): string |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns the foreign key. |
||
| 276 | */ |
||
| 277 | public function getJoinField(Table $table): ?FieldInterface |
||
| 283 | |||
| 284 | public function getJoinTable(Table $table): ?Table |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the Database instance associated with this table. |
||
| 301 | */ |
||
| 302 | public function getDatabase(): Database |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns all fields. |
||
| 309 | * |
||
| 310 | * @return FieldInterface[] |
||
| 311 | */ |
||
| 312 | public function getFields() |
||
| 316 | |||
| 317 | public function create(array $data = [], bool $fromDatabase = false): Row |
||
| 334 | |||
| 335 | public function createCollection(array $rows = [], bool $fromDatabase = false): RowCollection |
||
| 351 | } |
||
| 352 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: