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 |
||
20 | class Icon |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $options = []; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $prefix = 'fas'; |
||
31 | |||
32 | /** |
||
33 | * @param string $name |
||
34 | * @param array $options |
||
35 | * @param string $prefix |
||
36 | * @throws InvalidConfigException |
||
37 | */ |
||
38 | public function __construct($name, $options = [], $prefix = 'fas') |
||
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | public function __toString() |
||
69 | |||
70 | /** |
||
71 | * @return static |
||
72 | * @throws InvalidConfigException |
||
73 | */ |
||
74 | public function inverse() |
||
78 | |||
79 | /** |
||
80 | * @return static |
||
81 | * @throws InvalidConfigException |
||
82 | */ |
||
83 | public function spin() |
||
87 | |||
88 | /** |
||
89 | * @return static |
||
90 | * @throws InvalidConfigException |
||
91 | */ |
||
92 | public function pulse() |
||
96 | |||
97 | /** |
||
98 | * @return static |
||
99 | * @throws InvalidConfigException |
||
100 | */ |
||
101 | public function fixedWidth() |
||
105 | |||
106 | /** |
||
107 | * @return static |
||
108 | * @throws InvalidConfigException |
||
109 | */ |
||
110 | public function li() |
||
114 | |||
115 | /** |
||
116 | * @return static |
||
117 | * @throws InvalidConfigException |
||
118 | */ |
||
119 | public function border() |
||
123 | |||
124 | /** |
||
125 | * @return static |
||
126 | * @throws InvalidConfigException |
||
127 | */ |
||
128 | public function pullLeft() |
||
132 | |||
133 | /** |
||
134 | * @return static |
||
135 | * @throws InvalidConfigException |
||
136 | */ |
||
137 | public function pullRight() |
||
141 | |||
142 | /** |
||
143 | * @param string $value |
||
144 | * @return static |
||
145 | * @throws \yii\base\InvalidConfigException |
||
146 | */ |
||
147 | public function size($value) |
||
172 | |||
173 | /** |
||
174 | * @param string $value |
||
175 | * @return static |
||
176 | * @throws \yii\base\InvalidConfigException |
||
177 | */ |
||
178 | public function rotate($value) |
||
190 | |||
191 | /** |
||
192 | * @param string $value |
||
193 | * @return static |
||
194 | * @throws \yii\base\InvalidConfigException |
||
195 | */ |
||
196 | public function flip($value) |
||
208 | |||
209 | /** |
||
210 | * @param integer $value |
||
211 | * @return static |
||
212 | * @throws \yii\base\InvalidConfigException |
||
213 | */ |
||
214 | public function shrink($value) |
||
225 | |||
226 | /** |
||
227 | * @param integer $value |
||
228 | * @return static |
||
229 | * @throws \yii\base\InvalidConfigException |
||
230 | */ |
||
231 | public function grow($value) |
||
242 | |||
243 | /** |
||
244 | * @param integer $value |
||
245 | * @return static |
||
246 | * @throws \yii\base\InvalidConfigException |
||
247 | */ |
||
248 | public function up($value) |
||
259 | |||
260 | /** |
||
261 | * @param integer $value |
||
262 | * @return static |
||
263 | * @throws \yii\base\InvalidConfigException |
||
264 | */ |
||
265 | public function right($value) |
||
276 | |||
277 | /** |
||
278 | * @param integer $value |
||
279 | * @return static |
||
280 | * @throws \yii\base\InvalidConfigException |
||
281 | */ |
||
282 | public function down($value) |
||
293 | |||
294 | /** |
||
295 | * @param integer $value |
||
296 | * @return static |
||
297 | * @throws \yii\base\InvalidConfigException |
||
298 | */ |
||
299 | public function left($value) |
||
310 | |||
311 | /** |
||
312 | * @param $value |
||
313 | * @return static |
||
314 | * @throws InvalidConfigException |
||
315 | */ |
||
316 | public function mask($value) |
||
320 | |||
321 | /** |
||
322 | * @param string $class |
||
323 | * @param bool $condition |
||
324 | * @param string|bool $throw |
||
325 | * @return static |
||
326 | * @throws \yii\base\InvalidConfigException |
||
327 | * @codeCoverageIgnore |
||
328 | */ |
||
329 | public function addCssClass($class, $condition = true, $throw = false) |
||
345 | |||
346 | /** |
||
347 | * @param string $transformation |
||
348 | * @param bool $condition |
||
349 | * @param string|bool $throw |
||
350 | * @return static |
||
351 | * @throws \yii\base\InvalidConfigException |
||
352 | * @codeCoverageIgnore |
||
353 | */ |
||
354 | public function addTransformation($transformation, $condition = true, $throw = false) |
||
375 | |||
376 | /** |
||
377 | * @param string $maskIcon |
||
378 | * @param bool $condition |
||
379 | * @param string|bool $throw |
||
380 | * @return static |
||
381 | * @throws \yii\base\InvalidConfigException |
||
382 | * @codeCoverageIgnore |
||
383 | */ |
||
384 | public function addMask($maskIcon, $condition = true, $throw = false) |
||
401 | } |
||
402 |
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.