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() { |
||
| 96 | if ( $this->byId === null ) { |
||
| 97 | throw new RuntimeException( 'Index not build, call buildIndex first' ); |
||
| 98 | } |
||
| 99 | } |
||
| 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() { |
||
| 110 | $this->assertIndexIsBuild(); |
||
| 111 | |||
| 112 | return array_map( |
||
| 113 | function( $serializedPropertyId ) { |
||
| 114 | return new PropertyId( $serializedPropertyId ); |
||
| 115 | }, |
||
| 116 | array_keys( $this->byId ) |
||
| 117 | ); |
||
| 118 | } |
||
| 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 ) { |
||
| 132 | $this->assertIndexIsBuild(); |
||
| 133 | |||
| 134 | if ( !( array_key_exists( $propertyId->getSerialization(), $this->byId ) ) ) { |
||
| 135 | throw new OutOfBoundsException( "Object with propertyId \"$propertyId\" not found" ); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $this->byId[$propertyId->getSerialization()]; |
||
| 139 | } |
||
| 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 ) { |
||
| 151 | $this->assertIndexIsBuild(); |
||
| 152 | |||
| 153 | $i = 0; |
||
| 154 | foreach ( $this as $o ) { |
||
| 155 | if ( $o === $object ) { |
||
| 156 | return $i; |
||
| 157 | } |
||
| 158 | $i++; |
||
| 159 | } |
||
| 160 | return false; |
||
| 161 | } |
||
| 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() { |
||
| 171 | $this->assertIndexIsBuild(); |
||
| 172 | |||
| 173 | $array = array(); |
||
| 174 | foreach ( $this->byId as $objects ) { |
||
|
|
|||
| 175 | $array = array_merge( $array, $objects ); |
||
| 176 | } |
||
| 177 | return $array; |
||
| 178 | } |
||
| 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 ) { |
||
| 189 | $this->assertIndexIsBuild(); |
||
| 190 | |||
| 191 | $propertyIndices = array(); |
||
| 192 | $i = 0; |
||
| 193 | |||
| 194 | foreach ( $this->byId as $serializedPropertyId => $objects ) { |
||
| 195 | if ( $serializedPropertyId === $propertyId->getSerialization() ) { |
||
| 196 | $propertyIndices = range( $i, $i + count( $objects ) - 1 ); |
||
| 197 | break; |
||
| 198 | } else { |
||
| 199 | $i += count( $objects ); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | return $propertyIndices; |
||
| 204 | } |
||
| 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 ) { |
||
| 215 | $currentIndex = $this->getFlatArrayIndexOfObject( $object ); |
||
| 216 | |||
| 217 | if ( $toIndex === $currentIndex ) { |
||
| 218 | return; |
||
| 219 | } |
||
| 220 | |||
| 221 | $propertyId = $object->getPropertyId(); |
||
| 222 | |||
| 223 | $numericIndices = $this->getFlatArrayIndices( $propertyId ); |
||
| 224 | $lastIndex = $numericIndices[count( $numericIndices ) - 1]; |
||
| 225 | |||
| 226 | if ( $toIndex > $lastIndex + 1 || $toIndex < $numericIndices[0] ) { |
||
| 227 | throw new OutOfBoundsException( 'Object cannot be moved to ' . $toIndex ); |
||
| 228 | } |
||
| 229 | |||
| 230 | if ( $toIndex >= $lastIndex ) { |
||
| 231 | $this->moveObjectToEndOfPropertyGroup( $object ); |
||
| 232 | } else { |
||
| 233 | $this->removeObject( $object ); |
||
| 234 | |||
| 235 | $propertyGroup = array_combine( |
||
| 236 | $this->getFlatArrayIndices( $propertyId ), |
||
| 237 | $this->getByPropertyId( $propertyId ) |
||
| 238 | ); |
||
| 239 | |||
| 240 | $insertBefore = $propertyGroup[$toIndex]; |
||
| 241 | $this->insertObjectAtIndex( $object, $this->getFlatArrayIndexOfObject( $insertBefore ) ); |
||
| 242 | } |
||
| 243 | } |
||
| 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 ) { |
||
| 272 | $flatArray = $this->toFlatArray(); |
||
| 273 | $this->exchangeArray( $flatArray ); |
||
| 274 | $this->offsetUnset( array_search( $object, $flatArray ) ); |
||
| 275 | $this->buildIndex(); |
||
| 276 | } |
||
| 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 ) { |
||
| 285 | $flatArray = $this->toFlatArray(); |
||
| 286 | |||
| 287 | $this->exchangeArray( array_merge( |
||
| 288 | array_slice( $flatArray, 0, $index ), |
||
| 289 | array( $object ), |
||
| 290 | array_slice( $flatArray, $index ) |
||
| 291 | ) ); |
||
| 292 | |||
| 293 | $this->buildIndex(); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param PropertyId $propertyId |
||
| 298 | * @param int $toIndex |
||
| 299 | */ |
||
| 300 | private function movePropertyGroup( PropertyId $propertyId, $toIndex ) { |
||
| 301 | if ( $this->getPropertyGroupIndex( $propertyId ) === $toIndex ) { |
||
| 302 | return; |
||
| 303 | } |
||
| 304 | |||
| 305 | $insertBefore = null; |
||
| 306 | |||
| 307 | $oldIndex = $this->getPropertyGroupIndex( $propertyId ); |
||
| 308 | $byIdClone = $this->byId; |
||
| 309 | |||
| 310 | // Remove "property group" to calculate the groups new index: |
||
| 311 | unset( $this->byId[$propertyId->getSerialization()] ); |
||
| 312 | |||
| 313 | if ( $toIndex > $oldIndex ) { |
||
| 314 | // If the group shall be moved towards the bottom, the number of objects within the |
||
| 315 | // group needs to be subtracted from the absolute toIndex: |
||
| 316 | $toIndex -= count( $byIdClone[$propertyId->getSerialization()] ); |
||
| 317 | } |
||
| 318 | |||
| 319 | foreach ( $this->getPropertyIds() as $pId ) { |
||
| 320 | // Accepting other than the exact index by using <= letting the "property group" "latch" |
||
| 321 | // in the next slot. |
||
| 322 | if ( $toIndex <= $this->getPropertyGroupIndex( $pId ) ) { |
||
| 323 | $insertBefore = $pId; |
||
| 324 | break; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | $serializedPropertyId = $propertyId->getSerialization(); |
||
| 329 | $this->byId = array(); |
||
| 330 | |||
| 331 | foreach ( $byIdClone as $serializedPId => $objects ) { |
||
| 332 | $pId = new PropertyId( $serializedPId ); |
||
| 333 | if ( $pId->equals( $propertyId ) ) { |
||
| 334 | continue; |
||
| 335 | } elseif ( $pId->equals( $insertBefore ) ) { |
||
| 336 | $this->byId[$serializedPropertyId] = $byIdClone[$serializedPropertyId]; |
||
| 337 | } |
||
| 338 | $this->byId[$serializedPId] = $objects; |
||
| 339 | } |
||
| 340 | |||
| 341 | if ( $insertBefore === null ) { |
||
| 342 | $this->byId[$serializedPropertyId] = $byIdClone[$serializedPropertyId]; |
||
| 343 | } |
||
| 344 | |||
| 345 | $this->exchangeArray( $this->toFlatArray() ); |
||
| 346 | } |
||
| 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 ) { |
||
| 357 | $i = 0; |
||
| 358 | |||
| 359 | foreach ( $this->byId as $serializedPropertyId => $objects ) { |
||
| 360 | $pId = new PropertyId( $serializedPropertyId ); |
||
| 361 | if ( $pId->equals( $propertyId ) ) { |
||
| 362 | return $i; |
||
| 363 | } |
||
| 364 | $i += count( $objects ); |
||
| 365 | } |
||
| 366 | |||
| 367 | return false; |
||
| 368 | } |
||
| 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 ) { |
||
| 383 | $this->assertIndexIsBuild(); |
||
| 384 | |||
| 385 | if ( !in_array( $object, $this->toFlatArray() ) ) { |
||
| 386 | throw new OutOfBoundsException( 'Object not present in array' ); |
||
| 387 | } elseif ( $toIndex < 0 || $toIndex > count( $this ) ) { |
||
| 388 | throw new OutOfBoundsException( 'Specified index is out of bounds' ); |
||
| 389 | } elseif ( $this->getFlatArrayIndexOfObject( $object ) === $toIndex ) { |
||
| 390 | return; |
||
| 391 | } |
||
| 392 | |||
| 393 | // Determine whether to simply reindex the object within its "property group": |
||
| 394 | $propertyIndices = $this->getFlatArrayIndices( $object->getPropertyId() ); |
||
| 395 | |||
| 396 | if ( in_array( $toIndex, $propertyIndices ) ) { |
||
| 397 | $this->moveObjectInPropertyGroup( $object, $toIndex ); |
||
| 398 | } else { |
||
| 399 | $edgeIndex = ( $toIndex <= $propertyIndices[0] ) |
||
| 400 | ? $propertyIndices[0] |
||
| 401 | : $propertyIndices[count( $propertyIndices ) - 1]; |
||
| 402 | |||
| 403 | $this->moveObjectInPropertyGroup( $object, $edgeIndex ); |
||
| 404 | $this->movePropertyGroup( $object->getPropertyId(), $toIndex ); |
||
| 405 | } |
||
| 406 | |||
| 407 | $this->exchangeArray( $this->toFlatArray() ); |
||
| 408 | } |
||
| 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 ) { |
||
| 426 | $this->assertIndexIsBuild(); |
||
| 427 | |||
| 428 | $propertyId = $object->getPropertyId(); |
||
| 429 | $validIndices = $this->getFlatArrayIndices( $propertyId ); |
||
| 430 | |||
| 431 | if ( count( $this ) === 0 ) { |
||
| 432 | // Array is empty, just append object. |
||
| 433 | $this->append( $object ); |
||
| 434 | } elseif ( empty( $validIndices ) ) { |
||
| 435 | // No objects featuring that property exist. The object may be inserted at a place |
||
| 436 | // between existing "property groups". |
||
| 437 | $this->append( $object ); |
||
| 438 | if ( $index !== null ) { |
||
| 439 | $this->buildIndex(); |
||
| 440 | $this->moveObjectToIndex( $object, $index ); |
||
| 441 | } |
||
| 442 | } else { |
||
| 443 | // Objects featuring the same property as the object which is about to be added already |
||
| 444 | // exist in the array. |
||
| 445 | $this->addObjectToPropertyGroup( $object, $index ); |
||
| 446 | } |
||
| 447 | |||
| 448 | $this->buildIndex(); |
||
| 449 | } |
||
| 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.