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 | class View |
||
24 | { |
||
25 | protected $request = null; |
||
26 | |||
27 | protected $helpers = []; |
||
28 | |||
29 | protected $data = []; |
||
30 | protected $blocks = []; |
||
31 | protected $basePath = null; |
||
32 | |||
33 | /** |
||
34 | * @param $name |
||
35 | * @param $arguments |
||
36 | * @return mixed|null |
||
37 | */ |
||
38 | 2 | View Code Duplication | public function __call($name, $arguments) |
47 | |||
48 | /** |
||
49 | * @param $name |
||
50 | * @return mixed |
||
51 | */ |
||
52 | 2 | public function getHelper($name) |
|
60 | |||
61 | /** |
||
62 | * @param $name |
||
63 | */ |
||
64 | 2 | public function initHelper($name) |
|
68 | |||
69 | /** |
||
70 | * @param $name |
||
71 | * @return Helpers\View\AbstractHelper |
||
72 | */ |
||
73 | 2 | public function newHelper($name) |
|
82 | |||
83 | /** |
||
84 | * @param $name |
||
85 | * @return string |
||
86 | */ |
||
87 | 3 | public function getHelperClass($name) |
|
91 | |||
92 | /** |
||
93 | * @param $name |
||
94 | * @return mixed|null |
||
95 | */ |
||
96 | public function __get($name) |
||
100 | |||
101 | /** |
||
102 | * @param $name |
||
103 | * @param $value |
||
104 | * @return View |
||
105 | */ |
||
106 | public function __set($name, $value) |
||
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * @return mixed|null |
||
114 | */ |
||
115 | public function get($name) |
||
123 | |||
124 | /** |
||
125 | * @param string $name |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function has($name) |
||
132 | |||
133 | /** |
||
134 | * @param string $name |
||
135 | * @param mixed $value |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function set($name, $value) |
||
143 | |||
144 | /** |
||
145 | * @param $name |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function __isset($name) |
||
152 | |||
153 | /** |
||
154 | * @param $name |
||
155 | */ |
||
156 | public function __unset($name) |
||
160 | |||
161 | /** |
||
162 | * @param string $name |
||
163 | * @param string $appended |
||
164 | * @return View |
||
165 | */ |
||
166 | public function append($name, $appended) |
||
172 | |||
173 | /** |
||
174 | * @param $name |
||
175 | * @param $block |
||
176 | */ |
||
177 | public function setBlock($name, $block) |
||
181 | |||
182 | /** |
||
183 | * @param $view |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function existPath($view) |
||
190 | |||
191 | /** |
||
192 | * Builds path for including |
||
193 | * If $view starts with / the path will be relative to the root of the views folder. |
||
194 | * Otherwise to caller file location. |
||
195 | * |
||
196 | * @param string $view |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function buildPath($view) |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getBasePath() |
||
222 | |||
223 | /** |
||
224 | * @param $path |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function setBasePath($path) |
||
234 | |||
235 | protected function initBasePath() |
||
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function generateBasePath() |
||
250 | |||
251 | /** |
||
252 | * @param string $block |
||
253 | */ |
||
254 | public function render($block = 'default') |
||
262 | |||
263 | /** @noinspection PhpInconsistentReturnPointsInspection |
||
264 | * |
||
265 | * @param $view |
||
266 | * @param array $variables |
||
267 | * @param bool $return |
||
268 | * @return string|null |
||
269 | */ |
||
270 | public function load($view, $variables = [], $return = false) |
||
280 | |||
281 | /** |
||
282 | * @param $view |
||
283 | * @param array $variables |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getContents($view, $variables = []) |
||
300 | |||
301 | /** |
||
302 | * @param string $block |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function isBlock($block = 'default') |
||
309 | |||
310 | /** |
||
311 | * Assigns variables in bulk in the current scope |
||
312 | * |
||
313 | * @param array $array |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function assign($array = []) |
||
325 | |||
326 | /** |
||
327 | * @return mixed |
||
328 | */ |
||
329 | public function getRequest() |
||
333 | |||
334 | /** |
||
335 | * @param mixed $request |
||
336 | */ |
||
337 | public function setRequest($request) |
||
341 | } |
||
342 |
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.