Complex classes like ByPropertyIdArray 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 ByPropertyIdArray, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class ByPropertyIdArray extends ArrayObject { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array[]|null |
||
| 49 | */ |
||
| 50 | private $byId = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @see ArrayObject::__construct |
||
| 54 | * |
||
| 55 | * @param array|object|null $input |
||
| 56 | 65 | */ |
|
| 57 | 65 | public function __construct( $input = null ) { |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Builds the index for doing look-ups by property id. |
||
| 63 | * |
||
| 64 | * @since 0.2 |
||
| 65 | 62 | */ |
|
| 66 | 62 | public function buildIndex() { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Checks whether id indexed array has been generated. |
||
| 82 | * |
||
| 83 | * @throws RuntimeException |
||
| 84 | 64 | */ |
|
| 85 | 64 | private function assertIndexIsBuild() { |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Returns the property ids used for indexing. |
||
| 93 | * |
||
| 94 | * @since 0.2 |
||
| 95 | * |
||
| 96 | * @return PropertyId[] |
||
| 97 | * @throws RuntimeException |
||
| 98 | 38 | */ |
|
| 99 | 38 | public function getPropertyIds() { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the objects featuring the provided property id in the index. |
||
| 112 | * |
||
| 113 | * @since 0.2 |
||
| 114 | * |
||
| 115 | * @param PropertyId $propertyId |
||
| 116 | * |
||
| 117 | * @throws OutOfBoundsException |
||
| 118 | * @throws RuntimeException |
||
| 119 | * @return object[] |
||
| 120 | 27 | */ |
|
| 121 | 27 | public function getByPropertyId( PropertyId $propertyId ) { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the absolute index of an object or false if the object could not be found. |
||
| 133 | * @since 0.5 |
||
| 134 | * |
||
| 135 | * @param object $object |
||
| 136 | * |
||
| 137 | * @return bool|int |
||
| 138 | * @throws RuntimeException |
||
| 139 | 47 | */ |
|
| 140 | 47 | public function getFlatArrayIndexOfObject( $object ) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the objects in a flat array (using the indexed form for generating the array). |
||
| 155 | * @since 0.5 |
||
| 156 | * |
||
| 157 | * @return object[] |
||
| 158 | * @throws RuntimeException |
||
| 159 | */ |
||
| 160 | 55 | public function toFlatArray() { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the absolute numeric indices of objects featuring the same property id. |
||
| 172 | * |
||
| 173 | * @param PropertyId $propertyId |
||
| 174 | * |
||
| 175 | * @throws RuntimeException |
||
| 176 | * @return int[] |
||
| 177 | */ |
||
| 178 | 43 | private function getFlatArrayIndices( PropertyId $propertyId ) { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Moves an object within its "property group". |
||
| 198 | * |
||
| 199 | * @param object $object |
||
| 200 | * @param int $toIndex Absolute index within a "property group". |
||
| 201 | * |
||
| 202 | * @throws OutOfBoundsException |
||
| 203 | */ |
||
| 204 | 38 | private function moveObjectInPropertyGroup( $object, $toIndex ) { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Moves an object to the end of its "property group". |
||
| 237 | * |
||
| 238 | * @param object $object |
||
| 239 | */ |
||
| 240 | 12 | private function moveObjectToEndOfPropertyGroup( $object ) { |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Removes an object from the array structures. |
||
| 259 | * |
||
| 260 | * @param object $object |
||
| 261 | */ |
||
| 262 | 23 | private function removeObject( $object ) { |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Inserts an object at a specific index. |
||
| 271 | * |
||
| 272 | * @param object $object |
||
| 273 | * @param int $index Absolute index within the flat list of objects. |
||
| 274 | */ |
||
| 275 | 9 | private function insertObjectAtIndex( $object, $index ) { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param PropertyId $propertyId |
||
| 289 | * @param int $toIndex |
||
| 290 | */ |
||
| 291 | 32 | private function movePropertyGroup( PropertyId $propertyId, $toIndex ) { |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the index of a "property group" (the first object in the flat array that features |
||
| 341 | * the specified property). Returns false if property id could not be found. |
||
| 342 | * |
||
| 343 | * @param PropertyId $propertyId |
||
| 344 | * |
||
| 345 | * @return bool|int |
||
| 346 | */ |
||
| 347 | 32 | private function getPropertyGroupIndex( PropertyId $propertyId ) { |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Moves an existing object to a new index. Specifying an index outside the object's "property |
||
| 363 | * group" will move the object to the edge of the "property group" and shift the whole group |
||
| 364 | * to achieve the designated index for the object to move. |
||
| 365 | * @since 0.5 |
||
| 366 | * |
||
| 367 | * @param object $object |
||
| 368 | * @param int $toIndex Absolute index where to move the object to. |
||
| 369 | * |
||
| 370 | * @throws OutOfBoundsException |
||
| 371 | * @throws RuntimeException |
||
| 372 | */ |
||
| 373 | 47 | public function moveObjectToIndex( $object, $toIndex ) { |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Adds an object at a specific index. If no index is specified, the object will be append to |
||
| 403 | * the end of its "property group" or - if no objects featuring the same property exist - to the |
||
| 404 | * absolute end of the array. |
||
| 405 | * Specifying an index outside a "property group" will place the new object at the specified |
||
| 406 | * index with the existing "property group" objects being shifted towards the new object. |
||
| 407 | * |
||
| 408 | * @since 0.5 |
||
| 409 | * |
||
| 410 | * @param object $object |
||
| 411 | * @param int|null $index Absolute index where to place the new object. |
||
| 412 | * |
||
| 413 | * @throws RuntimeException |
||
| 414 | 7 | */ |
|
| 415 | 7 | public function addObjectAtIndex( $object, $index = null ) { |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Adds an object to an existing property group at the specified absolute index. |
||
| 443 | * |
||
| 444 | * @param object $object |
||
| 445 | * @param int|null $index |
||
| 446 | * |
||
| 447 | * @throws OutOfBoundsException |
||
| 448 | 3 | */ |
|
| 449 | private function addObjectToPropertyGroup( $object, $index = null ) { |
||
| 487 | |||
| 488 | } |
||
| 489 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.