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 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 $_project; |
||
30 | protected $_headers = []; |
||
31 | protected $_hashes = []; |
||
32 | protected $_links = []; |
||
33 | protected $_tags = []; |
||
34 | |||
35 | 1 | public function setProject($value) |
|
36 | { |
||
37 | 1 | $this->_project = $value; |
|
38 | 1 | } |
|
39 | |||
40 | 1 | public function getProject() |
|
41 | { |
||
42 | 1 | if ($this->_project === null) { |
|
43 | $this->_project = $this->detectProject(); |
||
44 | } |
||
45 | |||
46 | 1 | return $this->_project; |
|
47 | } |
||
48 | |||
49 | public function detectProject() |
||
50 | { |
||
51 | foreach ($this->getHeaders() as $line) { |
||
52 | if (preg_match('/\b([a-z0-9._-]{2,}\/[a-z0-9._-]{2,})\b/i', $line, $m)) { |
||
53 | return $m[1]; |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | 2 | public function addHeader($str) |
|
59 | { |
||
60 | 2 | $this->_headers[$str] = $str; |
|
61 | 2 | } |
|
62 | |||
63 | 1 | public function addHeaders(array $headers) |
|
64 | { |
||
65 | 1 | foreach ($headers as $header) { |
|
66 | 1 | $this->addHeader($header); |
|
67 | 1 | } |
|
68 | 1 | } |
|
69 | |||
70 | 1 | public function setHeaders(array $headers) |
|
71 | { |
||
72 | 1 | $this->_headers = []; |
|
73 | 1 | $this->addHeaders($headers); |
|
74 | 1 | } |
|
75 | |||
76 | 2 | public function getHeaders() |
|
77 | { |
||
78 | 2 | return $this->_headers; |
|
79 | } |
||
80 | |||
81 | 2 | public function hasLink($link) |
|
82 | { |
||
83 | 2 | return isset($this->_links[$link]); |
|
84 | } |
||
85 | |||
86 | public function removeLink($link) |
||
87 | { |
||
88 | unset($this->_links[$link]); |
||
89 | } |
||
90 | |||
91 | 3 | public function addLink($link, $href) |
|
92 | { |
||
93 | 3 | $this->_links[$link] = $href; |
|
94 | 3 | } |
|
95 | |||
96 | public function addLinks(array $links) |
||
97 | { |
||
98 | foreach ($links as $link => $href) { |
||
99 | $this->addLink($link, $href); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | 1 | public function setLinks(array $links) |
|
104 | { |
||
105 | 1 | $this->_links = $links; |
|
106 | 1 | } |
|
107 | |||
108 | 2 | public function getLinks() |
|
109 | { |
||
110 | 2 | return $this->_links; |
|
111 | } |
||
112 | |||
113 | 2 | public function hasHash($hash) |
|
114 | { |
||
115 | 2 | return isset($this->_hashes[(string) $hash]); |
|
116 | } |
||
117 | |||
118 | 3 | public function addHash($hash) |
|
119 | { |
||
120 | 3 | $this->_hashes[(string) $hash] = $hash; |
|
121 | 3 | } |
|
122 | |||
123 | public function addHashes(array $hashes) |
||
124 | { |
||
125 | foreach ($hashes as $hash) { |
||
126 | $this->addHash($hash); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public function setHashes(array $hashes) |
||
131 | { |
||
132 | $this->_hashes = []; |
||
133 | $this->addHashes($hashes); |
||
134 | } |
||
135 | |||
136 | 2 | public function getHashes() |
|
137 | { |
||
138 | 2 | return $this->_hashes; |
|
139 | } |
||
140 | |||
141 | 2 | public function getFirstTag() |
|
142 | { |
||
143 | 2 | return reset($this->_tags); |
|
144 | } |
||
145 | |||
146 | public function setFirstTag($name) |
||
147 | { |
||
148 | $this->getFirstTag()->setName($name); |
||
149 | } |
||
150 | |||
151 | 3 | public function countTags() |
|
152 | { |
||
153 | 3 | return count($this->_tags); |
|
154 | } |
||
155 | |||
156 | 3 | public function initTags() |
|
157 | { |
||
158 | 3 | if (!$this->countTags()) { |
|
159 | 3 | $this->addTag(new Tag($this->lastTag)); |
|
160 | 3 | } |
|
161 | 3 | } |
|
162 | |||
163 | 2 | public function getTags() |
|
164 | { |
||
165 | 2 | return $this->_tags; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Adds given tags to the history. |
||
170 | * @param Tag[] $tags |
||
171 | * @param boolean $prependNotes default is append |
||
172 | */ |
||
173 | 1 | public function addTags(array $tags, $prependNotes = false) |
|
174 | { |
||
175 | 1 | foreach ($tags as $name => $tag) { |
|
176 | 1 | $this->addTag($tag, $prependNotes); |
|
177 | 1 | } |
|
178 | 1 | } |
|
179 | |||
180 | 1 | public function setTags(array $tags) |
|
181 | { |
||
182 | 1 | $this->_tags = []; |
|
183 | 1 | $this->addTags($tags); |
|
184 | 1 | } |
|
185 | |||
186 | /** |
||
187 | * Returns tag by name. |
||
188 | * Creates if not exists. |
||
189 | * Returns first tag when given empty name. |
||
190 | * @param string|Tag $tag tag name or tag object |
||
191 | * @return Tag |
||
192 | */ |
||
193 | 3 | public function findTag($tag) |
|
194 | { |
||
195 | 3 | if (!$tag) { |
|
196 | 1 | $tag = reset($this->_tags) ?: $this->lastTag; |
|
197 | 1 | } |
|
198 | 3 | $name = $tag instanceof Tag ? $tag->getName() : $tag; |
|
199 | 3 | if (!$this->hasTag($name)) { |
|
200 | 3 | $this->_tags[$name] = new Tag($name); |
|
201 | 3 | } |
|
202 | |||
203 | 3 | return $this->_tags[$name]; |
|
204 | } |
||
205 | |||
206 | 3 | public function hasTag($tag) |
|
207 | { |
||
208 | 3 | return isset($this->_tags[$tag]); |
|
209 | } |
||
210 | |||
211 | public function removeTag($name) |
||
212 | { |
||
213 | foreach ($this->_tags as $k => $tag) { |
||
214 | if ($tag->getName() === $name) { |
||
215 | unset($this->_tags[$k]); |
||
216 | |||
217 | return; |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Adds tag. |
||
224 | * @param Tag $tag |
||
225 | * @param boolean $prependNotes default is append |
||
226 | * @return Tag the added tag |
||
227 | */ |
||
228 | 3 | public function addTag(Tag $tag, $prependNotes = false) |
|
229 | { |
||
230 | 3 | return $this->findTag($tag->getName())->setDate($tag->getDate())->addNotes($tag->getNotes(), $prependNotes); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Merges given history into the current. |
||
235 | * @param History $history |
||
236 | * @param boolean $prependNotes default is append |
||
237 | */ |
||
238 | public function merge(History $history, $prependNotes = false) |
||
239 | { |
||
240 | $this->mergeTags($history->getTags(), $prependNotes); |
||
241 | $this->addLinks($history->getLinks()); |
||
242 | $this->addHashes($history->getHashes()); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Merge given tags into the current history. |
||
247 | * @param Tag[] $tags |
||
248 | * @param boolean $prependNotes default is append |
||
249 | */ |
||
250 | public function mergeTags(array $tags, $prependNotes = false) |
||
251 | { |
||
252 | foreach ($tags as $tag) { |
||
253 | foreach ($tag->getNotes() as $note) { |
||
254 | $note->removeCommits($this->getHashes()); |
||
255 | } |
||
256 | } |
||
257 | $this->addTags($tags, $prependNotes); |
||
258 | //$olds = $this->getTags(); |
||
|
|||
259 | //$this->_tags = $tags; |
||
260 | //$this->addTags($$olds); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Normalizes the history. |
||
265 | */ |
||
266 | 2 | public function normalize($options = []) |
|
267 | { |
||
268 | static $defaults = [ |
||
269 | 'removeEmptyFirstTag' => [], |
||
270 | 'addInitTag' => [], |
||
271 | 'setTagDates' => [], |
||
272 | 'addCommitLinks' => [], |
||
273 | 'removeCommitLinks' => [], |
||
274 | 'prettifyUserLinks' => [], |
||
275 | 2 | ]; |
|
276 | 2 | $options = array_merge($defaults, $options); |
|
277 | 2 | foreach ($options as $func => $args) { |
|
278 | 2 | if (is_array($args)) { |
|
279 | 2 | call_user_func_array([$this, $func], $args); |
|
280 | 2 | } |
|
281 | 2 | } |
|
282 | 2 | } |
|
283 | |||
284 | /** |
||
285 | * Removes first tag if it is empty: has no notes and no commits. |
||
286 | */ |
||
287 | 2 | public function removeEmptyFirstTag() |
|
302 | |||
303 | /** |
||
304 | * Adds init tag with oldest commit date. |
||
305 | */ |
||
306 | 2 | public function addInitTag() |
|
325 | |||
326 | /** |
||
327 | * Normalizes dates to all the tags. |
||
328 | * Drops date for the last tag and sets for others. |
||
329 | */ |
||
330 | 2 | public function setTagDates() |
|
340 | |||
341 | /** |
||
342 | * Adds links for commits not having ones. |
||
343 | */ |
||
344 | 2 | public function addCommitLinks() |
|
352 | |||
353 | 1 | public function generateHashHref($hash) |
|
359 | |||
360 | /** |
||
361 | * Removes commit links that are not present in the history. |
||
362 | */ |
||
363 | 2 | public function removeCommitLinks($all = false) |
|
373 | |||
374 | /** |
||
375 | * Converts user links to given links. |
||
376 | * Usage: add 2 links to `history.md` like this: |
||
377 | * |
||
378 | * [@hiqsol]: https://github.com/hiqsol |
||
379 | * [[email protected]]: https://github.com/hiqsol |
||
380 | */ |
||
381 | 2 | public function prettifyUserLinks() |
|
382 | { |
||
383 | 2 | $users = []; |
|
384 | 2 | $subs = []; |
|
385 | 2 | foreach ($this->getLinks() as $link => $href) { |
|
386 | 2 | if ($link[0] === '@') { |
|
387 | 1 | $users[$href] = $link; |
|
388 | 2 | } else if (isset($users[$href])) { |
|
389 | $subs[$link] = $users[$href]; |
||
390 | } |
||
391 | 2 | } |
|
392 | 2 | if (!$subs) { |
|
393 | 2 | return; |
|
394 | } |
||
395 | foreach ($this->getTags() as $tag) { |
||
396 | foreach ($tag->getNotes() as $note) { |
||
397 | foreach ($note->getCommits() as $commit) { |
||
398 | $author = $commit->getAuthor(); |
||
399 | if (isset($subs[$author])) { |
||
400 | $commit->setAuthor($subs[$author]); |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | } |
||
407 |
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.