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 |
||
| 46 | class ByPropertyIdArray extends ArrayObject { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array[]|null |
||
| 50 | */ |
||
| 51 | private $byId = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @see ArrayObject::__construct |
||
| 55 | * |
||
| 56 | * @param PropertyIdProvider[]|Traversable|null $input |
||
| 57 | */ |
||
| 58 | public function __construct( $input = null ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Builds the index for doing look-ups by property id. |
||
| 64 | * |
||
| 65 | * @since 0.2 |
||
| 66 | */ |
||
| 67 | public function buildIndex() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Checks whether id indexed array has been generated. |
||
| 84 | * |
||
| 85 | * @throws RuntimeException |
||
| 86 | */ |
||
| 87 | private function assertIndexIsBuild() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the property ids used for indexing. |
||
| 95 | * |
||
| 96 | * @since 0.2 |
||
| 97 | * |
||
| 98 | * @return PropertyId[] |
||
| 99 | * @throws RuntimeException |
||
| 100 | */ |
||
| 101 | public function getPropertyIds() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the objects featuring the provided property id in the index. |
||
| 114 | * |
||
| 115 | * @since 0.2 |
||
| 116 | * |
||
| 117 | * @param PropertyId $propertyId |
||
| 118 | * |
||
| 119 | * @throws OutOfBoundsException |
||
| 120 | * @throws RuntimeException |
||
| 121 | * @return PropertyIdProvider[] |
||
| 122 | */ |
||
| 123 | public function getByPropertyId( PropertyId $propertyId ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the absolute index of an object or false if the object could not be found. |
||
| 135 | * @since 0.5 |
||
| 136 | * |
||
| 137 | * @param PropertyIdProvider $object |
||
| 138 | * |
||
| 139 | * @return bool|int |
||
| 140 | * @throws RuntimeException |
||
| 141 | */ |
||
| 142 | public function getFlatArrayIndexOfObject( $object ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the objects in a flat array (using the indexed form for generating the array). |
||
| 157 | * @since 0.5 |
||
| 158 | * |
||
| 159 | * @return PropertyIdProvider[] |
||
| 160 | * @throws RuntimeException |
||
| 161 | */ |
||
| 162 | public function toFlatArray() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the absolute numeric indices of objects featuring the same property id. |
||
| 174 | * |
||
| 175 | * @param PropertyId $propertyId |
||
| 176 | * |
||
| 177 | * @throws RuntimeException |
||
| 178 | * @return int[] |
||
| 179 | */ |
||
| 180 | private function getFlatArrayIndices( PropertyId $propertyId ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Moves an object within its "property group". |
||
| 200 | * |
||
| 201 | * @param PropertyIdProvider $object |
||
| 202 | * @param int $toIndex Absolute index within a "property group". |
||
| 203 | * |
||
| 204 | * @throws OutOfBoundsException |
||
| 205 | */ |
||
| 206 | private function moveObjectInPropertyGroup( $object, $toIndex ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Moves an object to the end of its "property group". |
||
| 239 | * |
||
| 240 | * @param PropertyIdProvider $object |
||
| 241 | */ |
||
| 242 | private function moveObjectToEndOfPropertyGroup( $object ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Removes an object from the array structures. |
||
| 260 | * |
||
| 261 | * @param PropertyIdProvider $object |
||
| 262 | */ |
||
| 263 | private function removeObject( $object ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Inserts an object at a specific index. |
||
| 272 | * |
||
| 273 | * @param PropertyIdProvider $object |
||
| 274 | * @param int $index Absolute index within the flat list of objects. |
||
| 275 | */ |
||
| 276 | private function insertObjectAtIndex( $object, $index ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param PropertyId $propertyId |
||
| 290 | * @param int $toIndex |
||
| 291 | */ |
||
| 292 | private function movePropertyGroup( PropertyId $propertyId, $toIndex ) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns the index of a "property group" (the first object in the flat array that features |
||
| 342 | * the specified property). Returns false if property id could not be found. |
||
| 343 | * |
||
| 344 | * @param PropertyId $propertyId |
||
| 345 | * |
||
| 346 | * @return bool|int |
||
| 347 | */ |
||
| 348 | private function getPropertyGroupIndex( PropertyId $propertyId ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Moves an existing object to a new index. Specifying an index outside the object's "property |
||
| 364 | * group" will move the object to the edge of the "property group" and shift the whole group |
||
| 365 | * to achieve the designated index for the object to move. |
||
| 366 | * @since 0.5 |
||
| 367 | * |
||
| 368 | * @param PropertyIdProvider $object |
||
| 369 | * @param int $toIndex Absolute index where to move the object to. |
||
| 370 | * |
||
| 371 | * @throws OutOfBoundsException |
||
| 372 | * @throws RuntimeException |
||
| 373 | */ |
||
| 374 | public function moveObjectToIndex( $object, $toIndex ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Adds an object at a specific index. If no index is specified, the object will be append to |
||
| 404 | * the end of its "property group" or - if no objects featuring the same property exist - to the |
||
| 405 | * absolute end of the array. |
||
| 406 | * Specifying an index outside a "property group" will place the new object at the specified |
||
| 407 | * index with the existing "property group" objects being shifted towards the new object. |
||
| 408 | * |
||
| 409 | * @since 0.5 |
||
| 410 | * |
||
| 411 | * @param PropertyIdProvider $object |
||
| 412 | * @param int|null $index Absolute index where to place the new object. |
||
| 413 | * |
||
| 414 | * @throws RuntimeException |
||
| 415 | */ |
||
| 416 | public function addObjectAtIndex( $object, $index = null ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Adds an object to an existing property group at the specified absolute index. |
||
| 444 | * |
||
| 445 | * @param PropertyIdProvider $object |
||
| 446 | * @param int|null $index |
||
| 447 | * |
||
| 448 | * @throws OutOfBoundsException |
||
| 449 | */ |
||
| 450 | 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.