Complex classes like RowCollection 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 RowCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class RowCollection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
| 18 | { |
||
| 19 | private $table; |
||
| 20 | private $rows = []; |
||
| 21 | private $data = []; |
||
| 22 | |||
| 23 | public function __construct(Table $table, Row ...$rows) |
||
| 31 | |||
| 32 | public function __debugInfo(): array |
||
| 39 | |||
| 40 | public function __call(string $name, array $arguments): Select |
||
| 53 | |||
| 54 | public function setData(array $data): self |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @see JsonSerializable |
||
| 63 | */ |
||
| 64 | public function jsonSerialize() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Magic method to stringify the values. |
||
| 71 | */ |
||
| 72 | public function __toString() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the table associated with this row |
||
| 79 | */ |
||
| 80 | public function getTable(): Table |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Return the value of all rows |
||
| 87 | */ |
||
| 88 | public function __get(string $name) |
||
| 134 | |||
| 135 | public function link(Table $table, RowCollection $rows, RowCollection $relations = null) |
||
| 166 | |||
| 167 | private function linkThrough(RowCollection $rows, RowCollection $relations) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Change a property of all rows |
||
| 209 | * @param mixed $value |
||
| 210 | */ |
||
| 211 | public function __set(string $name, $value) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Check whether a value is set or not |
||
| 220 | * @param mixed $name |
||
| 221 | */ |
||
| 222 | public function __isset($name) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @see ArrayAccess |
||
| 229 | * @param mixed $offset |
||
| 230 | * @param mixed $value |
||
| 231 | */ |
||
| 232 | public function offsetSet($offset, $value) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @see ArrayAccess |
||
| 239 | * @param mixed $offset |
||
| 240 | */ |
||
| 241 | public function offsetExists($offset) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @see ArrayAccess |
||
| 248 | * @param mixed $offset |
||
| 249 | */ |
||
| 250 | public function offsetUnset($offset) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @see ArrayAccess |
||
| 257 | * @param mixed $offset |
||
| 258 | */ |
||
| 259 | public function offsetGet($offset) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @see Iterator |
||
| 266 | */ |
||
| 267 | public function rewind() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @see Iterator |
||
| 274 | */ |
||
| 275 | public function current() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @see Iterator |
||
| 282 | */ |
||
| 283 | public function key() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @see Iterator |
||
| 290 | */ |
||
| 291 | public function next() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @see Iterator |
||
| 298 | */ |
||
| 299 | public function valid() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @see Countable |
||
| 306 | */ |
||
| 307 | public function count() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Returns an array with all fields of all rows |
||
| 314 | */ |
||
| 315 | public function toArray(): array |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Save all rows in the database |
||
| 324 | */ |
||
| 325 | public function save(): self |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Delete all rows in the database |
||
| 336 | */ |
||
| 337 | public function delete(): self |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Creates a select query of a table related with this row collection |
||
| 354 | */ |
||
| 355 | public function select(Table $table): Select |
||
| 359 | } |
||
| 360 |
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: