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:
1 | <?php |
||
23 | final class Version |
||
24 | { |
||
25 | /** @var string */ |
||
26 | private $build; |
||
27 | /** @var int */ |
||
28 | private $major; |
||
29 | /** @var int */ |
||
30 | private $minor; |
||
31 | /** @var int */ |
||
32 | private $patch; |
||
33 | /** @var string */ |
||
34 | private $preRelease; |
||
35 | |||
36 | /** |
||
37 | * @param int $major |
||
38 | * @param int $minor |
||
39 | * @param int $patch |
||
40 | * @param string $preRelease |
||
41 | * @param string $build |
||
42 | * |
||
43 | * @throws \SemVer\SemVer\Exception\InvalidArgumentException |
||
44 | */ |
||
45 | public function __construct(int $major, int $minor, int $patch, string $preRelease = '', string $build = '') |
||
65 | |||
66 | /** @return string */ |
||
67 | public function __toString() : string |
||
79 | |||
80 | /** |
||
81 | * @param string $version |
||
82 | * |
||
83 | * @throws InvalidArgumentException |
||
84 | * |
||
85 | * @return Version |
||
86 | */ |
||
87 | public static function fromString(string $version) : Version |
||
108 | |||
109 | /** |
||
110 | * @return int |
||
111 | */ |
||
112 | public function getMajor() : int |
||
116 | |||
117 | /** |
||
118 | * @return int |
||
119 | */ |
||
120 | public function getMinor() : int |
||
124 | |||
125 | /** |
||
126 | * @return int |
||
127 | */ |
||
128 | public function getPatch() : int |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getPreRelease() : string |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getBuild() : string |
||
148 | |||
149 | /** |
||
150 | * @throws \SemVer\SemVer\Exception\InvalidArgumentException |
||
151 | * |
||
152 | * @return Version |
||
153 | */ |
||
154 | public function major() : Version |
||
162 | |||
163 | /** |
||
164 | * @throws \SemVer\SemVer\Exception\InvalidArgumentException |
||
165 | * |
||
166 | * @return Version |
||
167 | */ |
||
168 | public function minor() : Version |
||
176 | |||
177 | /** |
||
178 | * @throws \SemVer\SemVer\Exception\InvalidArgumentException |
||
179 | * |
||
180 | * @return Version |
||
181 | */ |
||
182 | public function patch() : Version |
||
190 | |||
191 | /** |
||
192 | * @param Version $other |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function isEquals(Version $other) : bool |
||
200 | |||
201 | /** |
||
202 | * @param Version $other |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function isGreaterThan(Version $other) : bool |
||
210 | |||
211 | /** |
||
212 | * @param Version $other |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | public function isGreaterThanOrEqual(Version $other) : bool |
||
220 | |||
221 | /** |
||
222 | * @param Version $other |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function isLessThan(Version $other) : bool |
||
230 | |||
231 | /** |
||
232 | * @param Version $other |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | public function isLessThanOrEqual(Version $other) : bool |
||
240 | } |
||
241 |
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.