Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DocumentList 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 DocumentList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class DocumentList implements \ArrayAccess, \Countable, \Iterator, \TYPO3\CMS\Core\SingletonInterface { |
||
| 24 | /** |
||
| 25 | * This holds the number of documents in the list |
||
| 26 | * @see \Countable |
||
| 27 | * |
||
| 28 | * @var integer |
||
| 29 | * @access protected |
||
| 30 | */ |
||
| 31 | protected $count = 0; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * This holds the list entries in sorted order |
||
| 35 | * @see \ArrayAccess |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | * @access protected |
||
| 39 | */ |
||
| 40 | protected $elements = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * This holds the list's metadata |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | * @access protected |
||
| 47 | */ |
||
| 48 | protected $metadata = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * This holds the current list position |
||
| 52 | * @see \Iterator |
||
| 53 | * |
||
| 54 | * @var integer |
||
| 55 | * @access protected |
||
| 56 | */ |
||
| 57 | protected $position = 0; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * This holds the full records of already processed list elements |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | * @access protected |
||
| 64 | */ |
||
| 65 | protected $records = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Instance of \Kitodo\Dlf\Common\Solr class |
||
| 69 | * |
||
| 70 | * @var \Kitodo\Dlf\Common\Solr |
||
| 71 | * @access protected |
||
| 72 | */ |
||
| 73 | protected $solr; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * This holds the Solr metadata configuration |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | * @access protected |
||
| 80 | */ |
||
| 81 | protected $solrConfig = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * This adds an array of elements at the given position to the list |
||
| 85 | * |
||
| 86 | * @access public |
||
| 87 | * |
||
| 88 | * @param array $elements: Array of elements to add to list |
||
|
|
|||
| 89 | * @param integer $position: Numeric position for including |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public function add(array $elements, $position = -1) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * This counts the elements |
||
| 103 | * @see \Countable::count() |
||
| 104 | * |
||
| 105 | * @access public |
||
| 106 | * |
||
| 107 | * @return integer The number of elements in the list |
||
| 108 | */ |
||
| 109 | public function count() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * This returns the current element |
||
| 115 | * @see \Iterator::current() |
||
| 116 | * |
||
| 117 | * @access public |
||
| 118 | * |
||
| 119 | * @return array The current element |
||
| 120 | */ |
||
| 121 | public function current() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * This returns the full record of any list element |
||
| 132 | * |
||
| 133 | * @access protected |
||
| 134 | * |
||
| 135 | * @param mixed $element: The list element |
||
| 136 | * |
||
| 137 | * @return mixed The element's full record |
||
| 138 | */ |
||
| 139 | protected function getRecord($element) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * This returns the current position |
||
| 282 | * @see \Iterator::key() |
||
| 283 | * |
||
| 284 | * @access public |
||
| 285 | * |
||
| 286 | * @return integer The current position |
||
| 287 | */ |
||
| 288 | public function key() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * This moves the element at the given position up or down |
||
| 294 | * |
||
| 295 | * @access public |
||
| 296 | * |
||
| 297 | * @param integer $position: Numeric position for moving |
||
| 298 | * @param integer $steps: Amount of steps to move up or down |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | public function move($position, $steps) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * This moves the element at the given position up |
||
| 323 | * |
||
| 324 | * @access public |
||
| 325 | * |
||
| 326 | * @param integer $position: Numeric position for moving |
||
| 327 | * |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | public function moveUp($position) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * This moves the element at the given position down |
||
| 336 | * |
||
| 337 | * @access public |
||
| 338 | * |
||
| 339 | * @param integer $position: Numeric position for moving |
||
| 340 | * |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | public function moveDown($position) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * This increments the current list position |
||
| 349 | * @see \Iterator::next() |
||
| 350 | * |
||
| 351 | * @access public |
||
| 352 | * |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | public function next() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * This checks if an offset exists |
||
| 361 | * @see \ArrayAccess::offsetExists() |
||
| 362 | * |
||
| 363 | * @access public |
||
| 364 | * |
||
| 365 | * @param mixed $offset: The offset to check |
||
| 366 | * |
||
| 367 | * @return boolean Does the given offset exist? |
||
| 368 | */ |
||
| 369 | public function offsetExists($offset) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * This returns the element at the given offset |
||
| 375 | * @see \ArrayAccess::offsetGet() |
||
| 376 | * |
||
| 377 | * @access public |
||
| 378 | * |
||
| 379 | * @param mixed $offset: The offset to return |
||
| 380 | * |
||
| 381 | * @return array The element at the given offset |
||
| 382 | */ |
||
| 383 | public function offsetGet($offset) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * This sets the element at the given offset |
||
| 394 | * @see \ArrayAccess::offsetSet() |
||
| 395 | * |
||
| 396 | * @access public |
||
| 397 | * |
||
| 398 | * @param mixed $offset: The offset to set (non-integer offsets will be appended) |
||
| 399 | * @param mixed $value: The value to set |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function offsetSet($offset, $value) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * This removes the element at the given position from the list |
||
| 416 | * |
||
| 417 | * @access public |
||
| 418 | * |
||
| 419 | * @param integer $position: Numeric position for removing |
||
| 420 | * |
||
| 421 | * @return array The removed element |
||
| 422 | */ |
||
| 423 | View Code Duplication | public function remove($position) { |
|
| 434 | |||
| 435 | /** |
||
| 436 | * This removes elements at the given range from the list |
||
| 437 | * |
||
| 438 | * @access public |
||
| 439 | * |
||
| 440 | * @param integer $position: Numeric position for start of range |
||
| 441 | * @param integer $length: Numeric position for length of range |
||
| 442 | * |
||
| 443 | * @return array The indizes of the removed elements |
||
| 444 | */ |
||
| 445 | View Code Duplication | public function removeRange($position, $length) { |
|
| 456 | |||
| 457 | /** |
||
| 458 | * This clears the current list |
||
| 459 | * |
||
| 460 | * @access public |
||
| 461 | * |
||
| 462 | * @return void |
||
| 463 | */ |
||
| 464 | public function reset() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * This resets the list position |
||
| 474 | * @see \Iterator::rewind() |
||
| 475 | * |
||
| 476 | * @access public |
||
| 477 | * |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | public function rewind() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * This saves the current list |
||
| 486 | * |
||
| 487 | * @access public |
||
| 488 | * |
||
| 489 | * @param integer $pid: PID for saving in database |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | public function save($pid = 0) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Connects to Solr server. |
||
| 505 | * |
||
| 506 | * @access protected |
||
| 507 | * |
||
| 508 | * @return boolean TRUE on success or FALSE on failure |
||
| 509 | */ |
||
| 510 | protected function solrConnect() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * This sorts the current list by the given field |
||
| 540 | * |
||
| 541 | * @access public |
||
| 542 | * |
||
| 543 | * @param string $by: Sort the list by this field |
||
| 544 | * @param boolean $asc: Sort ascending? |
||
| 545 | * |
||
| 546 | * @return void |
||
| 547 | */ |
||
| 548 | public function sort($by, $asc = TRUE) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * This unsets the element at the given offset |
||
| 577 | * @see \ArrayAccess::offsetUnset() |
||
| 578 | * |
||
| 579 | * @access public |
||
| 580 | * |
||
| 581 | * @param mixed $offset: The offset to unset |
||
| 582 | * |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | public function offsetUnset($offset) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * This checks if the current list position is valid |
||
| 594 | * @see \Iterator::valid() |
||
| 595 | * |
||
| 596 | * @access public |
||
| 597 | * |
||
| 598 | * @return boolean Is the current list position valid? |
||
| 599 | */ |
||
| 600 | public function valid() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * This returns $this->metadata via __get() |
||
| 606 | * |
||
| 607 | * @access protected |
||
| 608 | * |
||
| 609 | * @return array The list's metadata |
||
| 610 | */ |
||
| 611 | protected function _getMetadata() { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * This sets $this->metadata via __set() |
||
| 617 | * |
||
| 618 | * @access protected |
||
| 619 | * |
||
| 620 | * @param array $metadata: Array of new metadata |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | protected function _setMetadata(array $metadata = []) { |
||
| 627 | |||
| 628 | /** |
||
| 629 | * This is the constructor |
||
| 630 | * |
||
| 631 | * @access public |
||
| 632 | * |
||
| 633 | * @param array $elements: Array of documents initially setting up the list |
||
| 634 | * @param array $metadata: Array of initial metadata |
||
| 635 | * |
||
| 636 | * @return void |
||
| 637 | */ |
||
| 638 | public function __construct(array $elements = [], array $metadata = []) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * This magic method is invoked each time a clone is called on the object variable |
||
| 663 | * (This method is defined as private/protected because singleton objects should not be cloned) |
||
| 664 | * |
||
| 665 | * @access protected |
||
| 666 | * |
||
| 667 | * @return void |
||
| 668 | */ |
||
| 669 | protected function __clone() {} |
||
| 670 | |||
| 671 | /** |
||
| 672 | * This magic method is called each time an invisible property is referenced from the object |
||
| 673 | * |
||
| 674 | * @access public |
||
| 675 | * |
||
| 676 | * @param string $var: Name of variable to get |
||
| 677 | * |
||
| 678 | * @return mixed Value of $this->$var |
||
| 679 | */ |
||
| 680 | View Code Duplication | public function __get($var) { |
|
| 690 | |||
| 691 | /** |
||
| 692 | * This magic method is called each time an invisible property is referenced from the object |
||
| 693 | * |
||
| 694 | * @access public |
||
| 695 | * |
||
| 696 | * @param string $var: Name of variable to set |
||
| 697 | * @param mixed $value: New value of variable |
||
| 698 | * |
||
| 699 | * @return void |
||
| 700 | */ |
||
| 701 | View Code Duplication | public function __set($var, $value) { |
|
| 710 | |||
| 711 | /** |
||
| 712 | * This magic method is executed prior to any serialization of the object |
||
| 713 | * @see __wakeup() |
||
| 714 | * |
||
| 715 | * @access public |
||
| 716 | * |
||
| 717 | * @return array Properties to be serialized |
||
| 718 | */ |
||
| 719 | public function __sleep() { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * This magic method is executed after the object is deserialized |
||
| 725 | * @see __sleep() |
||
| 726 | * |
||
| 727 | * @access public |
||
| 728 | * |
||
| 729 | * @return void |
||
| 730 | */ |
||
| 731 | public function __wakeup() { |
||
| 734 | } |
||
| 735 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.