Complex classes like Diff 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 Diff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Diff extends ArrayObject implements DiffOp { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool|null |
||
| 27 | */ |
||
| 28 | private $isAssociative = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Pointers to the operations of certain types for quick lookup. |
||
| 32 | * |
||
| 33 | * @var array[] |
||
| 34 | */ |
||
| 35 | private $typePointers = array( |
||
| 36 | 'add' => array(), |
||
| 37 | 'remove' => array(), |
||
| 38 | 'change' => array(), |
||
| 39 | 'list' => array(), |
||
| 40 | 'map' => array(), |
||
| 41 | 'diff' => array(), |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $indexOffset = 0; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @since 0.1 |
||
| 51 | * |
||
| 52 | * @param DiffOp[] $operations |
||
| 53 | * @param bool|null $isAssociative |
||
| 54 | * |
||
| 55 | * @throws InvalidArgumentException |
||
| 56 | */ |
||
| 57 | 105 | public function __construct( array $operations = array(), $isAssociative = null ) { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @since 0.1 |
||
| 77 | * |
||
| 78 | * @return DiffOp[] |
||
| 79 | */ |
||
| 80 | 122 | public function getOperations(): array { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @since 0.1 |
||
| 86 | * |
||
| 87 | * @param string $type |
||
| 88 | * |
||
| 89 | * @return DiffOp[] |
||
| 90 | */ |
||
| 91 | 21 | public function getTypeOperations( string $type ): array { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @since 0.1 |
||
| 100 | * |
||
| 101 | * @param DiffOp[] $operations |
||
| 102 | */ |
||
| 103 | 7 | public function addOperations( array $operations ) { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Gets called before a new element is added to the ArrayObject. |
||
| 111 | * |
||
| 112 | * At this point the index is always set (ie not null) and the |
||
| 113 | * value is always of the type returned by @see getObjectType. |
||
| 114 | * |
||
| 115 | * Should return a boolean. When false is returned the element |
||
| 116 | * does not get added to the ArrayObject. |
||
| 117 | * |
||
| 118 | * @param int|string $index |
||
| 119 | * @param DiffOp $value |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | * @throws InvalidArgumentException |
||
| 123 | */ |
||
| 124 | 84 | private function preSetElement( $index, DiffOp $value ): bool { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @see Serializable::unserialize |
||
| 141 | * |
||
| 142 | * @since 0.1 |
||
| 143 | * |
||
| 144 | * @param string $serialization |
||
| 145 | */ |
||
| 146 | 43 | public function unserialize( $serialization ) { |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @since 0.1 |
||
| 166 | * |
||
| 167 | * @return DiffOpAdd[] |
||
| 168 | */ |
||
| 169 | 7 | public function getAdditions(): array { |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @since 0.1 |
||
| 175 | * |
||
| 176 | * @return DiffOpRemove[] |
||
| 177 | */ |
||
| 178 | 7 | public function getRemovals(): array { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @since 0.1 |
||
| 184 | * |
||
| 185 | * @return DiffOpChange[] |
||
| 186 | */ |
||
| 187 | public function getChanges(): array { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the added values. |
||
| 193 | * |
||
| 194 | * @since 0.1 |
||
| 195 | * |
||
| 196 | * @return array of mixed |
||
| 197 | */ |
||
| 198 | 1 | public function getAddedValues(): array { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the removed values. |
||
| 209 | * |
||
| 210 | * @since 0.1 |
||
| 211 | * |
||
| 212 | * @return array of mixed |
||
| 213 | */ |
||
| 214 | 1 | public function getRemovedValues(): array { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @see DiffOp::isAtomic |
||
| 225 | * |
||
| 226 | * @since 0.1 |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | 72 | public function isAtomic(): bool { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @see DiffOp::getType |
||
| 236 | * |
||
| 237 | * @since 0.1 |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | 152 | public function getType(): string { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Counts the number of atomic operations in the diff. |
||
| 247 | * This means the size of a diff with as elements only empty diffs will be 0. |
||
| 248 | * Or that the size of a diff with one atomic operation and one diff that itself |
||
| 249 | * holds two atomic operations will be 3. |
||
| 250 | * |
||
| 251 | * @see Countable::count |
||
| 252 | * |
||
| 253 | * @since 0.1 |
||
| 254 | * |
||
| 255 | * @return int |
||
| 256 | */ |
||
| 257 | 84 | public function count(): int { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @since 0.3 |
||
| 272 | */ |
||
| 273 | 1 | public function removeEmptyOperations() { |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the value of the isAssociative flag. |
||
| 283 | * |
||
| 284 | * @since 0.4 |
||
| 285 | * |
||
| 286 | * @return bool|null |
||
| 287 | */ |
||
| 288 | 9 | public function isAssociative() { |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns if the diff looks associative or not. |
||
| 294 | * This first checks the isAssociative flag and in case its null checks |
||
| 295 | * if there are any non-add-non-remove operations. |
||
| 296 | * |
||
| 297 | * @since 0.4 |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | 9 | public function looksAssociative(): bool { |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Returns if the diff can be non-associative. |
||
| 307 | * This means it does not contain any non-add-non-remove operations. |
||
| 308 | * |
||
| 309 | * @since 0.4 |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | 16 | public function hasAssociativeOperations(): bool { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Returns the Diff in array form where nested DiffOps are also turned into their array form. |
||
| 322 | * |
||
| 323 | * @see DiffOp::toArray |
||
| 324 | * |
||
| 325 | * @since 0.5 |
||
| 326 | * |
||
| 327 | * @param callable|null $valueConverter optional callback used to convert any |
||
| 328 | * complex values to arrays. |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | 108 | public function toArray( callable $valueConverter = null ): array { |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @since 2.0 |
||
| 348 | * |
||
| 349 | * @param mixed $target |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | 12 | public function equals( $target ) { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Finds a new offset for when appending an element. |
||
| 368 | * The base class does this, so it would be better to integrate, |
||
| 369 | * but there does not appear to be any way to do this... |
||
| 370 | * |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | 23 | private function getNewOffset(): int { |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @see ArrayObject::append |
||
| 383 | * |
||
| 384 | * @since 0.1 |
||
| 385 | * |
||
| 386 | * @param mixed $value |
||
| 387 | */ |
||
| 388 | 19 | public function append( $value ) { |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @see ArrayObject::offsetSet() |
||
| 394 | * |
||
| 395 | * @since 0.1 |
||
| 396 | * |
||
| 397 | * @param int|string $index |
||
| 398 | * @param mixed $value |
||
| 399 | */ |
||
| 400 | 72 | public function offsetSet( $index, $value ) { |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Method that actually sets the element and holds |
||
| 406 | * all common code needed for set operations, including |
||
| 407 | * type checking and offset resolving. |
||
| 408 | * |
||
| 409 | * If you want to do additional indexing or have code that |
||
| 410 | * otherwise needs to be executed whenever an element is added, |
||
| 411 | * you can overload @see preSetElement. |
||
| 412 | * |
||
| 413 | * @param int|string|null $index |
||
| 414 | * @param mixed $value |
||
| 415 | * |
||
| 416 | * @throws InvalidArgumentException |
||
| 417 | */ |
||
| 418 | 86 | private function setElement( $index, $value ) { |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @see Serializable::serialize |
||
| 436 | * |
||
| 437 | * @since 0.1 |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 43 | public function serialize() { |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Returns if the ArrayObject has no elements. |
||
| 456 | * |
||
| 457 | * @since 0.1 |
||
| 458 | * |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | 15 | public function isEmpty(): bool { |
|
| 464 | |||
| 465 | } |
||
| 466 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.