Complex classes like XrefCollection 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 XrefCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class XrefCollection implements \IteratorAggregate, \ArrayAccess |
||
| 9 | { |
||
| 10 | /** @var Xref[] */ |
||
| 11 | protected $collection; |
||
| 12 | |||
| 13 | 7 | public function __construct() |
|
| 17 | |||
| 18 | /** |
||
| 19 | * (PHP 5 >= 5.0.0)<br/> |
||
| 20 | * Retrieve an external iterator |
||
| 21 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 22 | * @return \Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b> |
||
| 23 | */ |
||
| 24 | public function getIterator() |
||
| 28 | |||
| 29 | /** |
||
| 30 | * (PHP 5 >= 5.0.0)<br/> |
||
| 31 | * Whether a offset exists |
||
| 32 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 33 | * @param mixed $offset <p> |
||
| 34 | * An offset to check for. |
||
| 35 | * </p> |
||
| 36 | * @return boolean true on success or false on failure. |
||
| 37 | * </p> |
||
| 38 | * <p> |
||
| 39 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 40 | */ |
||
| 41 | public function offsetExists($offset) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * (PHP 5 >= 5.0.0)<br/> |
||
| 48 | * Offset to retrieve |
||
| 49 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 50 | * @param mixed $offset <p> |
||
| 51 | * The offset to retrieve. |
||
| 52 | * </p> |
||
| 53 | * @return mixed Can return all value types. |
||
| 54 | */ |
||
| 55 | 7 | public function offsetGet($offset) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * (PHP 5 >= 5.0.0)<br/> |
||
| 62 | * Offset to set |
||
| 63 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 64 | * @param mixed $offset <p> |
||
| 65 | * The offset to assign the value to. |
||
| 66 | * </p> |
||
| 67 | * @param mixed $value <p> |
||
| 68 | * The value to set. |
||
| 69 | * </p> |
||
| 70 | * @return void |
||
| 71 | * @throws \Exception |
||
| 72 | */ |
||
| 73 | public function offsetSet($offset, $value) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * (PHP 5 >= 5.0.0)<br/> |
||
| 87 | * Offset to unset |
||
| 88 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 89 | * @param mixed $offset <p> |
||
| 90 | * The offset to unset. |
||
| 91 | * </p> |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function offsetUnset($offset) |
||
| 105 | |||
| 106 | public function isEmpty() |
||
| 110 | |||
| 111 | 7 | public function clear() |
|
| 117 | |||
| 118 | 7 | public function add(Xref $xref) |
|
| 127 | |||
| 128 | public function removeById($xrefId) |
||
| 147 | |||
| 148 | public function remove(Xref $xref) |
||
| 165 | |||
| 166 | 7 | public function hasById($xrefId) |
|
| 175 | |||
| 176 | public function has(Xref $xref) |
||
| 180 | |||
| 181 | 7 | public function getById($xrefId) |
|
| 190 | |||
| 191 | public function parse($xrefs, $typeDelimiter, $overwrite = False, $ignore = True) |
||
| 219 | |||
| 220 | public function hasUnresolved() |
||
| 229 | |||
| 230 | public function resolve($force = false) |
||
| 236 | } |