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() |
|
| 49 | |||
| 50 | public function detectProject() |
||
| 58 | |||
| 59 | 2 | public function addHeader($str) |
|
| 63 | |||
| 64 | 1 | public function addHeaders(array $headers) |
|
| 70 | |||
| 71 | 1 | public function setHeaders(array $headers) |
|
| 76 | |||
| 77 | 2 | public function getHeaders() |
|
| 81 | |||
| 82 | 2 | public function hasLink($link) |
|
| 86 | |||
| 87 | public function removeLink($link) |
||
| 91 | |||
| 92 | 3 | public function addLink($link, $href) |
|
| 96 | |||
| 97 | public function addLinks(array $links) |
||
| 103 | |||
| 104 | 1 | public function setLinks(array $links) |
|
| 108 | |||
| 109 | 2 | public function getLinks() |
|
| 113 | |||
| 114 | 2 | public function hasHash($hash) |
|
| 118 | |||
| 119 | 3 | public function addHash($hash) |
|
| 123 | |||
| 124 | public function addHashes(array $hashes) |
||
| 130 | |||
| 131 | public function setHashes(array $hashes) |
||
| 136 | |||
| 137 | 2 | public function getHashes() |
|
| 141 | |||
| 142 | 2 | public function getFirstTag() |
|
| 146 | |||
| 147 | public function setFirstTag($name) |
||
| 151 | |||
| 152 | 3 | public function countTags() |
|
| 156 | |||
| 157 | 3 | public function initTags() |
|
| 163 | |||
| 164 | 2 | public function getTags() |
|
| 168 | |||
| 169 | 1 | public function addTags(array $tags) |
|
| 175 | |||
| 176 | 1 | public function setTags(array $tags) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Returns tag by name. |
||
| 184 | * Creates if not exists. |
||
| 185 | * Returns first tag when given empty name. |
||
| 186 | * @param string|Tag $tag tag or tag name |
||
| 187 | * @return Tag |
||
| 188 | */ |
||
| 189 | 3 | public function findTag($tag) |
|
| 201 | |||
| 202 | 3 | public function hasTag($tag) |
|
| 206 | |||
| 207 | public function removeTag($name) |
||
| 217 | |||
| 218 | 3 | public function addTag(Tag $tag) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Merges given history into the current. |
||
| 225 | * @param History $history |
||
| 226 | */ |
||
| 227 | public function merge(History $history) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Merge given tags into the current history. |
||
| 236 | * @param Tag[] $tags |
||
| 237 | */ |
||
| 238 | public function mergeTags(array $tags) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Normalizes the history. |
||
| 254 | */ |
||
| 255 | 2 | public function normalize($options = []) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Removes first tag if it is empty: has no notes and no commits. |
||
| 273 | */ |
||
| 274 | 2 | public function removeEmptyFirstTag() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Adds init tag with oldest commit date. |
||
| 292 | */ |
||
| 293 | 2 | public function addInitTag() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Adds links for commits not having ones. |
||
| 315 | */ |
||
| 316 | 2 | public function addCommitLinks() |
|
| 324 | |||
| 325 | 1 | public function generateHashHref($hash) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Removes commit links that are not present in the history. |
||
| 334 | */ |
||
| 335 | 2 | public function removeCommitLinks($all = false) |
|
| 345 | } |
||
| 346 |