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 |
||
| 47 | class ByPropertyIdArray extends ArrayObject { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array[]|null |
||
| 51 | */ |
||
| 52 | private $byId = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @deprecated since 5.0, use a DataModel Service instead |
||
| 56 | 65 | * @see ArrayObject::__construct |
|
| 57 | 65 | * |
|
| 58 | 65 | * @param PropertyIdProvider[]|Traversable|null $input |
|
| 59 | * |
||
| 60 | * @throws InvalidArgumentException |
||
| 61 | */ |
||
| 62 | public function __construct( $input = null ) { |
||
| 69 | 61 | ||
| 70 | /** |
||
| 71 | 61 | * Builds the index for doing look-ups by property id. |
|
| 72 | 61 | * |
|
| 73 | 61 | * @since 0.2 |
|
| 74 | */ |
||
| 75 | 61 | public function buildIndex() { |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Checks whether id indexed array has been generated. |
||
| 92 | * |
||
| 93 | * @throws RuntimeException |
||
| 94 | */ |
||
| 95 | private function assertIndexIsBuild() { |
||
| 100 | |||
| 101 | 37 | /** |
|
| 102 | 37 | * Returns the property ids used for indexing. |
|
| 103 | 37 | * |
|
| 104 | 37 | * @since 0.2 |
|
| 105 | 37 | * |
|
| 106 | 37 | * @return PropertyId[] |
|
| 107 | * @throws RuntimeException |
||
| 108 | */ |
||
| 109 | public function getPropertyIds() { |
||
| 119 | |||
| 120 | 27 | /** |
|
| 121 | 27 | * Returns the objects featuring the provided property id in the index. |
|
| 122 | * |
||
| 123 | 26 | * @since 0.2 |
|
| 124 | 1 | * |
|
| 125 | * @param PropertyId $propertyId |
||
| 126 | * |
||
| 127 | 25 | * @throws OutOfBoundsException |
|
| 128 | * @throws RuntimeException |
||
| 129 | * @return PropertyIdProvider[] |
||
| 130 | */ |
||
| 131 | public function getByPropertyId( PropertyId $propertyId ) { |
||
| 140 | 47 | ||
| 141 | /** |
||
| 142 | 47 | * Returns the absolute index of an object or false if the object could not be found. |
|
| 143 | 47 | * @since 0.5 |
|
| 144 | 47 | * |
|
| 145 | 47 | * @param PropertyIdProvider $object |
|
| 146 | * |
||
| 147 | 40 | * @return bool|int |
|
| 148 | 40 | * @throws RuntimeException |
|
| 149 | */ |
||
| 150 | public function getFlatArrayIndexOfObject( $object ) { |
||
| 162 | |||
| 163 | 55 | /** |
|
| 164 | 55 | * Returns the objects in a flat array (using the indexed form for generating the array). |
|
| 165 | 55 | * @since 0.5 |
|
| 166 | 55 | * |
|
| 167 | 55 | * @return PropertyIdProvider[] |
|
| 168 | * @throws RuntimeException |
||
| 169 | */ |
||
| 170 | public function toFlatArray() { |
||
| 179 | 43 | ||
| 180 | /** |
||
| 181 | 43 | * Returns the absolute numeric indices of objects featuring the same property id. |
|
| 182 | 43 | * |
|
| 183 | * @param PropertyId $propertyId |
||
| 184 | 43 | * |
|
| 185 | 41 | * @throws RuntimeException |
|
| 186 | 41 | * @return int[] |
|
| 187 | 41 | */ |
|
| 188 | private function getFlatArrayIndices( PropertyId $propertyId ) { |
||
| 205 | 38 | ||
| 206 | /** |
||
| 207 | 38 | * Moves an object within its "property group". |
|
| 208 | 17 | * |
|
| 209 | * @param PropertyIdProvider $object |
||
| 210 | * @param int $toIndex Absolute index within a "property group". |
||
| 211 | 21 | * |
|
| 212 | * @throws OutOfBoundsException |
||
| 213 | 21 | */ |
|
| 214 | 21 | private function moveObjectInPropertyGroup( $object, $toIndex ) { |
|
| 244 | 12 | ||
| 245 | 12 | /** |
|
| 246 | * Moves an object to the end of its "property group". |
||
| 247 | 12 | * |
|
| 248 | 12 | * @param PropertyIdProvider $object |
|
| 249 | 12 | */ |
|
| 250 | private function moveObjectToEndOfPropertyGroup( $object ) { |
||
| 265 | 23 | ||
| 266 | 23 | /** |
|
| 267 | 23 | * Removes an object from the array structures. |
|
| 268 | * |
||
| 269 | * @param PropertyIdProvider $object |
||
| 270 | */ |
||
| 271 | private function removeObject( $object ) { |
||
| 277 | |||
| 278 | 9 | /** |
|
| 279 | 9 | * Inserts an object at a specific index. |
|
| 280 | 9 | * |
|
| 281 | 9 | * @param PropertyIdProvider $object |
|
| 282 | 9 | * @param int $index Absolute index within the flat list of objects. |
|
| 283 | */ |
||
| 284 | 9 | private function insertObjectAtIndex( $object, $index ) { |
|
| 295 | |||
| 296 | 32 | /** |
|
| 297 | * @param PropertyId $propertyId |
||
| 298 | 32 | * @param int $toIndex |
|
| 299 | 32 | */ |
|
| 300 | private function movePropertyGroup( PropertyId $propertyId, $toIndex ) { |
||
| 347 | 32 | ||
| 348 | 32 | /** |
|
| 349 | * Returns the index of a "property group" (the first object in the flat array that features |
||
| 350 | 32 | * the specified property). Returns false if property id could not be found. |
|
| 351 | 32 | * |
|
| 352 | 32 | * @param PropertyId $propertyId |
|
| 353 | 32 | * |
|
| 354 | * @return bool|int |
||
| 355 | 30 | */ |
|
| 356 | 30 | private function getPropertyGroupIndex( PropertyId $propertyId ) { |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Moves an existing object to a new index. Specifying an index outside the object's "property |
||
| 372 | * group" will move the object to the edge of the "property group" and shift the whole group |
||
| 373 | 47 | * to achieve the designated index for the object to move. |
|
| 374 | 47 | * @since 0.5 |
|
| 375 | * |
||
| 376 | 47 | * @param PropertyIdProvider $object |
|
| 377 | 1 | * @param int $toIndex Absolute index where to move the object to. |
|
| 378 | 46 | * |
|
| 379 | 1 | * @throws OutOfBoundsException |
|
| 380 | 45 | * @throws RuntimeException |
|
| 381 | 7 | */ |
|
| 382 | public function moveObjectToIndex( $object, $toIndex ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Adds an object at a specific index. If no index is specified, the object will be append to |
||
| 412 | * the end of its "property group" or - if no objects featuring the same property exist - to the |
||
| 413 | * absolute end of the array. |
||
| 414 | 7 | * Specifying an index outside a "property group" will place the new object at the specified |
|
| 415 | 7 | * index with the existing "property group" objects being shifted towards the new object. |
|
| 416 | * |
||
| 417 | 7 | * @since 0.5 |
|
| 418 | 7 | * |
|
| 419 | * @param PropertyIdProvider $object |
||
| 420 | 7 | * @param int|null $index Absolute index where to place the new object. |
|
| 421 | * |
||
| 422 | 2 | * @throws OutOfBoundsException |
|
| 423 | 7 | * @throws RuntimeException |
|
| 424 | */ |
||
| 425 | public function addObjectAtIndex( $object, $index = null ) { |
||
| 450 | 3 | ||
| 451 | 3 | /** |
|
| 452 | * Adds an object to an existing property group at the specified absolute index. |
||
| 453 | 3 | * |
|
| 454 | * @param PropertyIdProvider $object |
||
| 455 | * @param int|null $index |
||
| 456 | * |
||
| 457 | * @throws OutOfBoundsException |
||
| 458 | 3 | */ |
|
| 459 | private function addObjectToPropertyGroup( $object, $index = null ) { |
||
| 496 | |||
| 497 | } |
||
| 498 |
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.