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 |
||
| 13 | class RowCollection extends AbstractRow implements ArrayAccess, Iterator, Countable |
||
| 14 | { |
||
| 15 | private $rows = []; |
||
| 16 | private $loadedRelations = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Debug info. |
||
| 20 | * |
||
| 21 | * @return array |
||
| 22 | */ |
||
| 23 | public function __debugInfo() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Magic method to get properties from all rows. |
||
| 33 | * |
||
| 34 | * @see self::get() |
||
| 35 | */ |
||
| 36 | public function __get($name) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Magic method to set properties to all rows. |
||
| 112 | * |
||
| 113 | * @param string $name |
||
| 114 | * @param mixed $value |
||
| 115 | */ |
||
| 116 | public function __set($name, $value) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Magic method to check if a property is defined or not. |
||
| 151 | * |
||
| 152 | * @param string $name Property name |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function __isset($name) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @see ArrayAccess |
||
| 165 | */ |
||
| 166 | public function offsetSet($offset, $value) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @see ArrayAccess |
||
| 181 | */ |
||
| 182 | public function offsetExists($offset) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @see ArrayAccess |
||
| 189 | */ |
||
| 190 | public function offsetUnset($offset) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @see ArrayAccess |
||
| 197 | */ |
||
| 198 | public function offsetGet($offset) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @see Iterator |
||
| 205 | */ |
||
| 206 | public function rewind() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @see Iterator |
||
| 213 | */ |
||
| 214 | public function current() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @see Iterator |
||
| 221 | */ |
||
| 222 | public function key() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @see Iterator |
||
| 229 | */ |
||
| 230 | public function next() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @see Iterator |
||
| 237 | */ |
||
| 238 | public function valid() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @see Countable |
||
| 245 | */ |
||
| 246 | public function count() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | public function toArray($recursive = true, array $bannedEntities = []) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Filter the rows by a value. |
||
| 277 | * |
||
| 278 | * @param callable $filter |
||
| 279 | * |
||
| 280 | * @return RowCollection |
||
| 281 | */ |
||
| 282 | public function filter(callable $filter) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Find a row by a value. |
||
| 289 | * |
||
| 290 | * @param callable $filter |
||
| 291 | * |
||
| 292 | * @return Row|null The rows found |
||
| 293 | */ |
||
| 294 | public function find(callable $filter) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Join two related tables. |
||
| 305 | * |
||
| 306 | * @param Table $table1 |
||
| 307 | * @param RowCollection|array $rows1 |
||
| 308 | * @param Table $table2 |
||
| 309 | * @param RowCollection|array $rows2 |
||
| 310 | * @param array $relation |
||
| 311 | */ |
||
| 312 | private static function join(Table $table1, $rows1, Table $table2, $rows2, array $relation) |
||
| 333 | } |
||
| 334 |