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 | * @see ArrayObject::__construct |
||
| 57 | * |
||
| 58 | * @param PropertyIdProvider[]|Traversable|null $input |
||
| 59 | * |
||
| 60 | * @throws InvalidArgumentException |
||
| 61 | */ |
||
| 62 | public function __construct( $input = null ) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Builds the index for doing look-ups by property id. |
||
| 72 | * |
||
| 73 | * @since 0.2 |
||
| 74 | */ |
||
| 75 | 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 | /** |
||
| 102 | * Returns the property ids used for indexing. |
||
| 103 | * |
||
| 104 | * @since 0.2 |
||
| 105 | * |
||
| 106 | * @return PropertyId[] |
||
| 107 | * @throws RuntimeException |
||
| 108 | */ |
||
| 109 | public function getPropertyIds() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the objects featuring the provided property id in the index. |
||
| 122 | * |
||
| 123 | * @since 0.2 |
||
| 124 | * |
||
| 125 | * @param PropertyId $propertyId |
||
| 126 | * |
||
| 127 | * @throws OutOfBoundsException |
||
| 128 | * @throws RuntimeException |
||
| 129 | * @return PropertyIdProvider[] |
||
| 130 | */ |
||
| 131 | public function getByPropertyId( PropertyId $propertyId ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the absolute index of an object or false if the object could not be found. |
||
| 143 | * @since 0.5 |
||
| 144 | * |
||
| 145 | * @param PropertyIdProvider $object |
||
| 146 | * |
||
| 147 | * @return bool|int |
||
| 148 | * @throws RuntimeException |
||
| 149 | */ |
||
| 150 | public function getFlatArrayIndexOfObject( $object ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the objects in a flat array (using the indexed form for generating the array). |
||
| 165 | * @since 0.5 |
||
| 166 | * |
||
| 167 | * @return PropertyIdProvider[] |
||
| 168 | * @throws RuntimeException |
||
| 169 | */ |
||
| 170 | public function toFlatArray() { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns the absolute numeric indices of objects featuring the same property id. |
||
| 182 | * |
||
| 183 | * @param PropertyId $propertyId |
||
| 184 | * |
||
| 185 | * @throws RuntimeException |
||
| 186 | * @return int[] |
||
| 187 | */ |
||
| 188 | private function getFlatArrayIndices( PropertyId $propertyId ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Moves an object within its "property group". |
||
| 208 | * |
||
| 209 | * @param PropertyIdProvider $object |
||
| 210 | * @param int $toIndex Absolute index within a "property group". |
||
| 211 | * |
||
| 212 | * @throws OutOfBoundsException |
||
| 213 | */ |
||
| 214 | private function moveObjectInPropertyGroup( $object, $toIndex ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Moves an object to the end of its "property group". |
||
| 247 | * |
||
| 248 | * @param PropertyIdProvider $object |
||
| 249 | */ |
||
| 250 | private function moveObjectToEndOfPropertyGroup( $object ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Removes an object from the array structures. |
||
| 268 | * |
||
| 269 | * @param PropertyIdProvider $object |
||
| 270 | */ |
||
| 271 | private function removeObject( $object ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Inserts an object at a specific index. |
||
| 280 | * |
||
| 281 | * @param PropertyIdProvider $object |
||
| 282 | * @param int $index Absolute index within the flat list of objects. |
||
| 283 | */ |
||
| 284 | private function insertObjectAtIndex( $object, $index ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param PropertyId $propertyId |
||
| 298 | * @param int $toIndex |
||
| 299 | */ |
||
| 300 | private function movePropertyGroup( PropertyId $propertyId, $toIndex ) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns the index of a "property group" (the first object in the flat array that features |
||
| 350 | * the specified property). Returns false if property id could not be found. |
||
| 351 | * |
||
| 352 | * @param PropertyId $propertyId |
||
| 353 | * |
||
| 354 | * @return bool|int |
||
| 355 | */ |
||
| 356 | 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 | * to achieve the designated index for the object to move. |
||
| 374 | * @since 0.5 |
||
| 375 | * |
||
| 376 | * @param PropertyIdProvider $object |
||
| 377 | * @param int $toIndex Absolute index where to move the object to. |
||
| 378 | * |
||
| 379 | * @throws OutOfBoundsException |
||
| 380 | * @throws RuntimeException |
||
| 381 | */ |
||
| 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 | * Specifying an index outside a "property group" will place the new object at the specified |
||
| 415 | * index with the existing "property group" objects being shifted towards the new object. |
||
| 416 | * |
||
| 417 | * @since 0.5 |
||
| 418 | * |
||
| 419 | * @param PropertyIdProvider $object |
||
| 420 | * @param int|null $index Absolute index where to place the new object. |
||
| 421 | * |
||
| 422 | * @throws OutOfBoundsException |
||
| 423 | * @throws RuntimeException |
||
| 424 | */ |
||
| 425 | public function addObjectAtIndex( $object, $index = null ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Adds an object to an existing property group at the specified absolute index. |
||
| 453 | * |
||
| 454 | * @param PropertyIdProvider $object |
||
| 455 | * @param int|null $index |
||
| 456 | * |
||
| 457 | * @throws OutOfBoundsException |
||
| 458 | */ |
||
| 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.