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 |
||
17 | class UnorderedList |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | public static $defaultTag = 'ul'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $tag; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $options = []; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $items = []; |
||
39 | |||
40 | /** |
||
41 | * @param array $options |
||
42 | */ |
||
43 | 2 | public function __construct($options = []) |
|
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | 2 | public function __toString() |
|
57 | |||
58 | /** |
||
59 | * @param string|Icon $icon |
||
60 | * @param string|null $label |
||
61 | * @param array $options |
||
62 | * @return static |
||
63 | */ |
||
64 | 2 | public function item($icon, $label = null, $options = []) |
|
76 | |||
77 | /** |
||
78 | * Change html tag. |
||
79 | * @param string $tag |
||
80 | * @return static |
||
81 | * @throws \yii\base\InvalidParamException |
||
82 | */ |
||
83 | 1 | public function tag($tag) |
|
89 | |||
90 | /** |
||
91 | * @param string|null $tag |
||
92 | * @param array $options |
||
93 | * @return string |
||
94 | * @throws \yii\base\InvalidConfigException |
||
95 | */ |
||
96 | 2 | View Code Duplication | public function render($tag = null, $options = []) |
108 | } |
||
109 |
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.