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 |
||
23 | class History |
||
24 | { |
||
25 | public $lastTag = 'Under development'; |
||
26 | |||
27 | public $initTag = 'Development started'; |
||
28 | |||
29 | protected $_config; |
||
30 | protected $_project; |
||
31 | protected $_headers = []; |
||
32 | protected $_hashes = []; |
||
33 | protected $_links = []; |
||
34 | protected $_tags = []; |
||
35 | |||
36 | 3 | public function __construct(ConfigInterface $config) |
|
40 | |||
41 | 2 | public function isInitTag($tag) |
|
45 | |||
46 | 2 | public function isLastTag($tag) |
|
50 | |||
51 | 1 | public function setProject($value) |
|
55 | |||
56 | 2 | public function getProject() |
|
64 | |||
65 | 1 | public function detectProject() |
|
73 | |||
74 | 2 | public function addHeader($str) |
|
78 | |||
79 | 1 | public function addHeaders(array $headers) |
|
85 | |||
86 | 1 | public function setHeaders(array $headers) |
|
91 | |||
92 | 2 | public function getHeaders() |
|
96 | |||
97 | 2 | public function hasLink($link) |
|
101 | |||
102 | public function removeLink($link) |
||
106 | |||
107 | 3 | public function addLink($link, $href) |
|
111 | |||
112 | public function addLinks(array $links) |
||
118 | |||
119 | 1 | public function setLinks(array $links) |
|
123 | |||
124 | 2 | public function getLinks() |
|
128 | |||
129 | 2 | public function hasHash($hash) |
|
133 | |||
134 | 3 | public function addHash($hash) |
|
138 | |||
139 | public function addHashes(array $hashes) |
||
145 | |||
146 | public function setHashes(array $hashes) |
||
151 | |||
152 | 2 | public function getHashes() |
|
156 | |||
157 | 2 | public function getFirstTag() |
|
161 | |||
162 | public function setFirstTag($name) |
||
166 | |||
167 | 3 | public function countTags() |
|
171 | |||
172 | 3 | public function initTags() |
|
178 | |||
179 | 2 | public function getTags() |
|
183 | |||
184 | /** |
||
185 | * Adds given tags to the history. |
||
186 | * @param Tag[] $tags |
||
187 | * @param boolean $prependNotes default is append |
||
188 | */ |
||
189 | 1 | public function addTags(array $tags, $prependNotes = false) |
|
195 | |||
196 | 1 | public function setTags(array $tags) |
|
201 | |||
202 | /** |
||
203 | * Returns tag by name. |
||
204 | * Creates if not exists. |
||
205 | * Returns first tag when given empty name. |
||
206 | * @param string|Tag $tag tag name or tag object |
||
207 | * @return Tag |
||
208 | */ |
||
209 | 3 | public function findTag($tag) |
|
221 | |||
222 | 3 | public function hasTag($tag) |
|
226 | |||
227 | public function removeTag($name) |
||
237 | |||
238 | /** |
||
239 | * Adds tag. |
||
240 | * @param Tag $tag |
||
241 | * @param boolean $prependNotes default is append |
||
242 | * @return Tag the added tag |
||
243 | */ |
||
244 | 3 | public function addTag(Tag $tag, $prependNotes = false) |
|
248 | |||
249 | /** |
||
250 | * Merges given history into the current. |
||
251 | * @param History $history |
||
252 | * @param boolean $prependNotes default is append |
||
253 | */ |
||
254 | public function merge(History $history, $prependNotes = false) |
||
260 | |||
261 | /** |
||
262 | * Merge given tags into the current history. |
||
263 | * @param Tag[] $tags |
||
264 | * @param boolean $prependNotes default is append |
||
265 | */ |
||
266 | public function mergeTags(array $tags, $prependNotes = false) |
||
278 | } |
||
279 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.