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 |
||
33 | class DocumentHelper extends AppHelper |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Init vars. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | public $charset; |
||
42 | public $dir; |
||
43 | public $eol; |
||
44 | public $locale; |
||
45 | public $tab; |
||
46 | |||
47 | /** |
||
48 | * Uses helpers. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | public $helpers = [ |
||
53 | 'Core.Html', |
||
54 | 'Core.Assets' |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Is called after layout rendering is complete. Receives the layout filename as an argument. |
||
59 | * |
||
60 | * @param Event $event |
||
61 | * @param string $layoutFile |
||
62 | * @return void |
||
63 | * |
||
64 | * @throws \JBZoo\Utils\Exception |
||
65 | */ |
||
66 | public function afterLayout(Event $event, $layoutFile) |
||
73 | |||
74 | /** |
||
75 | * Is called after the view has been rendered but before layout rendering has started. |
||
76 | * |
||
77 | * @param Event $event |
||
78 | * @param string $viewFile |
||
79 | * @return void |
||
80 | * |
||
81 | * @throws \JBZoo\Utils\Exception |
||
82 | */ |
||
83 | View Code Duplication | public function afterRender(Event $event, $viewFile) |
|
92 | |||
93 | /** |
||
94 | * Is called after each view file is rendered. This includes elements, views, parent views and layouts. |
||
95 | * A callback can modify and return $content to change how the rendered content will be displayed in the browser. |
||
96 | * |
||
97 | * @param Event $event |
||
98 | * @param string $viewFile |
||
99 | * @param string $content |
||
100 | * @return void |
||
101 | * |
||
102 | * @throws \JBZoo\Utils\Exception |
||
103 | */ |
||
104 | View Code Duplication | public function afterRenderFile(Event $event, $viewFile, $content) |
|
111 | |||
112 | /** |
||
113 | * Get assets fot layout render. |
||
114 | * |
||
115 | * @param string $type |
||
116 | * @return string |
||
117 | */ |
||
118 | public function assets($type = 'css') |
||
128 | |||
129 | /** |
||
130 | * Is called before layout rendering starts. Receives the layout filename as an argument. |
||
131 | * |
||
132 | * @param Event $event |
||
133 | * @param string $layoutFile |
||
134 | * @return void |
||
135 | * |
||
136 | * @throws \JBZoo\Utils\Exception |
||
137 | */ |
||
138 | public function beforeLayout(Event $event, $layoutFile) |
||
145 | |||
146 | /** |
||
147 | * Is called after the controller’s beforeRender method but before the controller renders view and layout. |
||
148 | * Receives the file being rendered as an argument. |
||
149 | * |
||
150 | * @param Event $event |
||
151 | * @param string $viewFile |
||
152 | * @return void |
||
153 | * |
||
154 | * @throws \JBZoo\Less\Exception |
||
155 | * @throws \JBZoo\Utils\Exception |
||
156 | */ |
||
157 | View Code Duplication | public function beforeRender(Event $event, $viewFile) |
|
166 | |||
167 | /** |
||
168 | * Is called before each view file is rendered. This includes elements, views, parent views and layouts. |
||
169 | * |
||
170 | * @param Event $event |
||
171 | * @param string $viewFile |
||
172 | * @return void |
||
173 | * |
||
174 | * @throws \JBZoo\Utils\Exception |
||
175 | */ |
||
176 | public function beforeRenderFile(Event $event, $viewFile) |
||
183 | |||
184 | /** |
||
185 | * Get body classes by view data. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getBodyClasses() |
||
209 | |||
210 | /** |
||
211 | * Create head for layout. |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public function head() |
||
226 | |||
227 | /** |
||
228 | * Constructor hook method. |
||
229 | * |
||
230 | * @param array $config |
||
231 | */ |
||
232 | public function initialize(array $config) |
||
242 | |||
243 | /** |
||
244 | * Site language. |
||
245 | * |
||
246 | * @param bool|true $isLang |
||
247 | * @return string |
||
248 | * |
||
249 | * @throws \Exception |
||
250 | */ |
||
251 | public function lang($isLang = true) |
||
256 | |||
257 | /** |
||
258 | * Creates a link to an external resource and handles basic meta tags. |
||
259 | * |
||
260 | * @param array $rows |
||
261 | * @param null $block |
||
262 | * @return null|string |
||
263 | */ |
||
264 | public function meta(array $rows, $block = null) |
||
280 | |||
281 | /** |
||
282 | * Create html 5 document type. |
||
283 | * |
||
284 | * @return string |
||
285 | * |
||
286 | * @throws \Exception |
||
287 | */ |
||
288 | public function type() |
||
306 | |||
307 | /** |
||
308 | * Assign data from view vars. |
||
309 | * |
||
310 | * @param string $key |
||
311 | * @return $this |
||
312 | */ |
||
313 | protected function _assignMeta($key) |
||
321 | |||
322 | /** |
||
323 | * Setup view meta data. |
||
324 | * |
||
325 | * @return void |
||
326 | */ |
||
327 | protected function _setupMetaData() |
||
334 | } |
||
335 |
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.