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 |
||
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() |
|
42 | { |
||
43 | 1 | if ($this->_project === null) { |
|
44 | $this->_project = $this->detectProject(); |
||
45 | } |
||
46 | |||
47 | 1 | return $this->_project; |
|
48 | } |
||
49 | |||
50 | public function detectProject() |
||
51 | { |
||
52 | foreach ($this->getHeaders() as $line) { |
||
53 | if (preg_match('/\b([a-z0-9._-]{2,}\/[a-z0-9._-]{2,})\b/i', $line, $m)) { |
||
54 | return $m[1]; |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | 2 | public function addHeader($str) |
|
63 | |||
64 | 1 | public function addHeaders(array $headers) |
|
65 | { |
||
66 | 1 | foreach ($headers as $header) { |
|
67 | 1 | $this->addHeader($header); |
|
68 | } |
||
69 | 1 | } |
|
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) |
||
98 | { |
||
99 | foreach ($links as $link => $href) { |
||
100 | $this->addLink($link, $href); |
||
101 | } |
||
102 | } |
||
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) |
||
125 | { |
||
126 | foreach ($hashes as $hash) { |
||
127 | $this->addHash($hash); |
||
128 | } |
||
129 | } |
||
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() |
|
158 | { |
||
159 | 3 | if (!$this->countTags()) { |
|
160 | 3 | $this->addTag(new Tag($this->lastTag)); |
|
161 | } |
||
162 | 3 | } |
|
163 | |||
164 | 2 | public function getTags() |
|
168 | |||
169 | 1 | public function addTags(array $tags) |
|
170 | { |
||
171 | 1 | foreach ($tags as $name => $tag) { |
|
172 | 1 | $this->addTag($tag); |
|
173 | } |
||
174 | 1 | } |
|
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) |
|
190 | { |
||
191 | 3 | if (!$tag) { |
|
192 | 1 | $tag = reset($this->_tags) ?: $this->lastTag; |
|
193 | } |
||
194 | 3 | $name = $tag instanceof Tag ? $tag->getName() : $tag; |
|
195 | 3 | if (!$this->hasTag($name)) { |
|
196 | 3 | $this->_tags[$name] = new Tag($name); |
|
197 | } |
||
198 | |||
199 | 3 | return $this->_tags[$name]; |
|
200 | } |
||
201 | |||
202 | 3 | public function hasTag($tag) |
|
206 | |||
207 | public function removeTag($name) |
||
208 | { |
||
209 | foreach ($this->_tags as $k => $tag) { |
||
210 | if ($tag->getName() == $name) { |
||
211 | unset($this->_tags[$k]); |
||
212 | |||
213 | return; |
||
214 | } |
||
215 | } |
||
216 | } |
||
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) |
||
239 | { |
||
240 | foreach ($tags as $tag) { |
||
241 | foreach ($tag->getNotes() as $note) { |
||
242 | $note->removeCommits($this->getHashes()); |
||
251 | |||
252 | /** |
||
253 | * Normalizes the history. |
||
254 | */ |
||
255 | 2 | public function normalize($options = []) |
|
271 | |||
272 | /** |
||
273 | * Removes first tag if it is empty: has no notes and no commits. |
||
274 | */ |
||
275 | 2 | public function removeEmptyFirstTag() |
|
290 | |||
291 | /** |
||
292 | * Adds init tag with oldest commit date. |
||
293 | */ |
||
294 | 2 | public function addInitTag() |
|
313 | |||
314 | /** |
||
315 | * Normalizes dates to all the tags. |
||
316 | * Drops date for the last tag and sets for others. |
||
317 | */ |
||
318 | 2 | public function setTagDates() |
|
328 | |||
329 | /** |
||
330 | * Adds links for commits not having ones. |
||
331 | */ |
||
332 | 2 | public function addCommitLinks() |
|
340 | |||
341 | 1 | public function generateHashHref($hash) |
|
347 | |||
348 | /** |
||
349 | * Removes commit links that are not present in the history. |
||
350 | */ |
||
351 | 2 | public function removeCommitLinks($all = false) |
|
361 | } |
||
362 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.