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 |
||
11 | class DependentSelect extends Select implements WithRoutesInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @param Router $router |
||
16 | */ |
||
17 | public static function registerRoutes(Router $router) |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $dataUrl = ''; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $dataDepends = []; |
||
85 | |||
86 | /** |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $params; |
||
90 | |||
91 | /** |
||
92 | * DependentSelect constructor. |
||
93 | * |
||
94 | * @param string $path |
||
95 | * @param null $label |
||
96 | * @param array $depends |
||
97 | */ |
||
98 | public function __construct($path, $label = null, array $depends = []) |
||
104 | |||
105 | /** |
||
106 | * @param string $key |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function hasDependKey($key) |
||
114 | |||
115 | /** |
||
116 | * @param string $key |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function getDependValue($key) |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getDataUrl() |
||
137 | |||
138 | /** |
||
139 | * @param string $dataUrl |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setDataUrl($dataUrl) |
||
149 | |||
150 | /** |
||
151 | * @return string Json |
||
152 | */ |
||
153 | public function getDataDepends() |
||
157 | |||
158 | |||
159 | /** |
||
160 | * @param array|string $depends |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function setDataDepends($depends) |
||
170 | |||
171 | /** |
||
172 | * @return array |
||
173 | */ |
||
174 | public function toArray() |
||
202 | |||
203 | /** |
||
204 | * @param array $params |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | protected function setAjaxParameters(array $params) |
||
214 | |||
215 | } |
||
216 |
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.