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 |
||
| 25 | class Diff extends ArrayObject implements DiffOp { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool|null |
||
| 29 | */ |
||
| 30 | private $isAssociative = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Pointers to the operations of certain types for quick lookup. |
||
| 34 | * |
||
| 35 | * @var array[] |
||
| 36 | */ |
||
| 37 | private $typePointers = array( |
||
| 38 | 'add' => array(), |
||
| 39 | 'remove' => array(), |
||
| 40 | 'change' => array(), |
||
| 41 | 'list' => array(), |
||
| 42 | 'map' => array(), |
||
| 43 | 'diff' => array(), |
||
| 44 | ); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | private $indexOffset = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @since 0.1 |
||
| 53 | * |
||
| 54 | * @param DiffOp[] $operations |
||
| 55 | * @param bool|null $isAssociative |
||
| 56 | * |
||
| 57 | * @throws InvalidArgumentException |
||
| 58 | */ |
||
| 59 | 105 | public function __construct( array $operations = array(), $isAssociative = null ) { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @since 0.1 |
||
| 79 | * |
||
| 80 | * @return DiffOp[] |
||
| 81 | */ |
||
| 82 | 122 | public function getOperations(): array { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @since 0.1 |
||
| 88 | * |
||
| 89 | * @param string $type |
||
| 90 | * |
||
| 91 | * @return DiffOp[] |
||
| 92 | */ |
||
| 93 | 21 | public function getTypeOperations( string $type ): array { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @since 0.1 |
||
| 102 | * |
||
| 103 | * @param DiffOp[] $operations |
||
| 104 | */ |
||
| 105 | 7 | public function addOperations( array $operations ) { |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Gets called before a new element is added to the ArrayObject. |
||
| 113 | * |
||
| 114 | * At this point the index is always set (ie not null) and the |
||
| 115 | * value is always of the type returned by @see getObjectType. |
||
| 116 | * |
||
| 117 | * Should return a boolean. When false is returned the element |
||
| 118 | * does not get added to the ArrayObject. |
||
| 119 | * |
||
| 120 | * @param int|string $index |
||
| 121 | * @param DiffOp $value |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | * @throws InvalidArgumentException |
||
| 125 | */ |
||
| 126 | 84 | private function preSetElement( $index, DiffOp $value ): bool { |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @see Serializable::unserialize |
||
| 143 | * |
||
| 144 | * @since 0.1 |
||
| 145 | * |
||
| 146 | * @param string $serialization |
||
| 147 | */ |
||
| 148 | 43 | public function unserialize( $serialization ) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @since 0.1 |
||
| 168 | * |
||
| 169 | * @return DiffOpAdd[] |
||
| 170 | */ |
||
| 171 | 7 | public function getAdditions(): array { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @since 0.1 |
||
| 177 | * |
||
| 178 | * @return DiffOpRemove[] |
||
| 179 | */ |
||
| 180 | 7 | public function getRemovals(): array { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @since 0.1 |
||
| 186 | * |
||
| 187 | * @return DiffOpChange[] |
||
| 188 | */ |
||
| 189 | public function getChanges(): array { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the added values. |
||
| 195 | * |
||
| 196 | * @since 0.1 |
||
| 197 | * |
||
| 198 | * @return array of mixed |
||
| 199 | */ |
||
| 200 | 1 | public function getAddedValues(): array { |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the removed values. |
||
| 211 | * |
||
| 212 | * @since 0.1 |
||
| 213 | * |
||
| 214 | * @return array of mixed |
||
| 215 | */ |
||
| 216 | 1 | public function getRemovedValues(): array { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @see DiffOp::isAtomic |
||
| 227 | * |
||
| 228 | * @since 0.1 |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 72 | public function isAtomic(): bool { |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @see DiffOp::getType |
||
| 238 | * |
||
| 239 | * @since 0.1 |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | 152 | public function getType(): string { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Counts the number of atomic operations in the diff. |
||
| 249 | * This means the size of a diff with as elements only empty diffs will be 0. |
||
| 250 | * Or that the size of a diff with one atomic operation and one diff that itself |
||
| 251 | * holds two atomic operations will be 3. |
||
| 252 | * |
||
| 253 | * @see Countable::count |
||
| 254 | * |
||
| 255 | * @since 0.1 |
||
| 256 | * |
||
| 257 | * @return int |
||
| 258 | */ |
||
| 259 | 84 | public function count(): int { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @since 0.3 |
||
| 274 | */ |
||
| 275 | 1 | public function removeEmptyOperations() { |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Returns the value of the isAssociative flag. |
||
| 285 | * |
||
| 286 | * @since 0.4 |
||
| 287 | * |
||
| 288 | * @return bool|null |
||
| 289 | */ |
||
| 290 | 9 | public function isAssociative() { |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Returns if the diff looks associative or not. |
||
| 296 | * This first checks the isAssociative flag and in case its null checks |
||
| 297 | * if there are any non-add-non-remove operations. |
||
| 298 | * |
||
| 299 | * @since 0.4 |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | 9 | public function looksAssociative(): bool { |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Returns if the diff can be non-associative. |
||
| 309 | * This means it does not contain any non-add-non-remove operations. |
||
| 310 | * |
||
| 311 | * @since 0.4 |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | 16 | public function hasAssociativeOperations(): bool { |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the Diff in array form where nested DiffOps are also turned into their array form. |
||
| 324 | * |
||
| 325 | * @see DiffOp::toArray |
||
| 326 | * |
||
| 327 | * @since 0.5 |
||
| 328 | * |
||
| 329 | * @param callable|null $valueConverter optional callback used to convert any |
||
| 330 | * complex values to arrays. |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | 108 | public function toArray( callable $valueConverter = null ): array { |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @since 2.0 |
||
| 350 | * |
||
| 351 | * @param mixed $target |
||
| 352 | * |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | 12 | public function equals( $target ) { |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Finds a new offset for when appending an element. |
||
| 370 | * The base class does this, so it would be better to integrate, |
||
| 371 | * but there does not appear to be any way to do this... |
||
| 372 | * |
||
| 373 | * @return int |
||
| 374 | */ |
||
| 375 | 23 | private function getNewOffset(): int { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @see ArrayObject::append |
||
| 385 | * |
||
| 386 | * @since 0.1 |
||
| 387 | * |
||
| 388 | * @param mixed $value |
||
| 389 | */ |
||
| 390 | 19 | public function append( $value ) { |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @see ArrayObject::offsetSet() |
||
| 396 | * |
||
| 397 | * @since 0.1 |
||
| 398 | * |
||
| 399 | * @param int|string $index |
||
| 400 | * @param mixed $value |
||
| 401 | */ |
||
| 402 | 72 | public function offsetSet( $index, $value ) { |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Method that actually sets the element and holds |
||
| 408 | * all common code needed for set operations, including |
||
| 409 | * type checking and offset resolving. |
||
| 410 | * |
||
| 411 | * If you want to do additional indexing or have code that |
||
| 412 | * otherwise needs to be executed whenever an element is added, |
||
| 413 | * you can overload @see preSetElement. |
||
| 414 | * |
||
| 415 | * @param int|string|null $index |
||
| 416 | * @param mixed $value |
||
| 417 | * |
||
| 418 | * @throws InvalidArgumentException |
||
| 419 | */ |
||
| 420 | 86 | private function setElement( $index, $value ) { |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @see Serializable::serialize |
||
| 438 | * |
||
| 439 | * @since 0.1 |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | 43 | public function serialize() { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Returns if the ArrayObject has no elements. |
||
| 458 | * |
||
| 459 | * @since 0.1 |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | 15 | public function isEmpty(): bool { |
|
| 466 | |||
| 467 | } |
||
| 468 |
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.