Complex classes like History 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 History, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class History |
||
| 25 | { |
||
| 26 | public $lastTag = 'Under development'; |
||
| 27 | |||
| 28 | public $initTag = 'Development started'; |
||
| 29 | |||
| 30 | protected $_project; |
||
| 31 | protected $_headers = []; |
||
| 32 | protected $_hashes = []; |
||
| 33 | protected $_links = []; |
||
| 34 | protected $_tags = []; |
||
| 35 | |||
| 36 | 1 | public function setProject($value) |
|
| 40 | |||
| 41 | 1 | public function getProject() |
|
| 48 | |||
| 49 | public function detectProject() |
||
| 57 | |||
| 58 | public function addHeader($str) |
||
| 62 | 2 | ||
| 63 | public function addHeaders(array $headers) |
||
| 69 | 1 | ||
| 70 | public function setHeaders(array $headers) |
||
| 75 | 1 | ||
| 76 | public function getHeaders() |
||
| 80 | |||
| 81 | public function hasLink($link) |
||
| 85 | |||
| 86 | public function removeLink($link) |
||
| 90 | |||
| 91 | public function addLink($link, $href) |
||
| 95 | 3 | ||
| 96 | public function addLinks(array $links) |
||
| 102 | |||
| 103 | public function setLinks(array $links) |
||
| 107 | 1 | ||
| 108 | public function getLinks() |
||
| 112 | |||
| 113 | public function hasHash($hash) |
||
| 117 | |||
| 118 | public function addHash($hash) |
||
| 122 | 3 | ||
| 123 | public function addHashes(array $hashes) |
||
| 129 | |||
| 130 | public function setHashes(array $hashes) |
||
| 135 | |||
| 136 | public function getHashes() |
||
| 140 | |||
| 141 | public function getFirstTag() |
||
| 145 | |||
| 146 | public function setFirstTag($name) |
||
| 150 | |||
| 151 | public function countTags() |
||
| 155 | |||
| 156 | public function initTags() |
||
| 162 | 3 | ||
| 163 | public function getTags() |
||
| 167 | |||
| 168 | public function addTags(array $tags) |
||
| 174 | 1 | ||
| 175 | public function setTags(array $tags) |
||
| 180 | 1 | ||
| 181 | /** |
||
| 182 | * Returns tag by name. |
||
| 183 | * Creates if not exists. |
||
| 184 | * Returns first tag when given empty name. |
||
| 185 | * @param string|Tag $tag tag or tag name |
||
| 186 | * @return Tag |
||
| 187 | */ |
||
| 188 | public function findTag($tag) |
||
| 200 | |||
| 201 | public function hasTag($tag) |
||
| 205 | |||
| 206 | public function removeTag($name) |
||
| 215 | |||
| 216 | public function addTag(Tag $tag) |
||
| 220 | 3 | ||
| 221 | /** |
||
| 222 | * Merges given history into the current. |
||
| 223 | * @param History $history |
||
| 224 | */ |
||
| 225 | public function merge(History $history) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Merge given tags into the current history. |
||
| 234 | * @param Tag[] $tags |
||
| 235 | */ |
||
| 236 | public function mergeTags(array $tags) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Normalizes the history. |
||
| 252 | */ |
||
| 253 | public function normalize() |
||
| 257 | 2 | ||
| 258 | 2 | /** |
|
| 259 | * Removes first tag if it is empty: has no notes and no commits. |
||
| 260 | */ |
||
| 261 | public function removeEmptyFirstTag() |
||
| 276 | } |
||
| 277 |