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 | * Magic method to get properties from all rows. |
||
| 20 | * |
||
| 21 | * @see self::get() |
||
| 22 | */ |
||
| 23 | public function __get($name) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Magic method to set properties to all rows. |
||
| 85 | * |
||
| 86 | * @param string $name |
||
| 87 | * @param mixed $value |
||
| 88 | */ |
||
| 89 | public function __set($name, $value) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Magic method to check if a property is defined or not. |
||
| 124 | * |
||
| 125 | * @param string $name Property name |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | public function __isset($name) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @see ArrayAccess |
||
| 138 | */ |
||
| 139 | public function offsetSet($offset, $value) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @see ArrayAccess |
||
| 154 | */ |
||
| 155 | public function offsetExists($offset) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @see ArrayAccess |
||
| 162 | */ |
||
| 163 | public function offsetUnset($offset) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @see ArrayAccess |
||
| 170 | */ |
||
| 171 | public function offsetGet($offset) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @see Iterator |
||
| 178 | */ |
||
| 179 | public function rewind() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @see Iterator |
||
| 186 | */ |
||
| 187 | public function current() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @see Iterator |
||
| 194 | */ |
||
| 195 | public function key() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @see Iterator |
||
| 202 | */ |
||
| 203 | public function next() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @see Iterator |
||
| 210 | */ |
||
| 211 | public function valid() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @see Countable |
||
| 218 | */ |
||
| 219 | public function count() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function toArray(array $bannedEntities = array()) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Filter the rows by a value. |
||
| 246 | * |
||
| 247 | * @param callable $filter |
||
| 248 | * |
||
| 249 | * @return RowCollection |
||
| 250 | */ |
||
| 251 | public function filter(callable $filter) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Find a row by a value. |
||
| 258 | * |
||
| 259 | * @param callable $filter |
||
| 260 | * |
||
| 261 | * @return Row|null The rows found |
||
| 262 | */ |
||
| 263 | public function find(callable $filter) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Join two related tables. |
||
| 274 | * |
||
| 275 | * @param Table $table1 |
||
| 276 | * @param RowCollection|array $rows1 |
||
| 277 | * @param Table $table2 |
||
| 278 | * @param RowCollection|array $rows2 |
||
| 279 | * @param array $relation |
||
| 280 | */ |
||
| 281 | private static function join(Table $table1, $rows1, Table $table2, $rows2, array $relation) |
||
| 302 | } |
||
| 303 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.