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 namespace VojtaSvoboda\TwigExtensions; |
||
19 | class Plugin extends PluginBase |
||
20 | { |
||
21 | /** |
||
22 | * Returns information about this plugin. |
||
23 | * |
||
24 | * @return array |
||
25 | */ |
||
26 | public function pluginDetails() |
||
36 | |||
37 | View Code Duplication | public function boot() |
|
48 | |||
49 | /** |
||
50 | * Add Twig extensions. |
||
51 | * |
||
52 | * @see Text extensions http://twig.sensiolabs.org/doc/extensions/text.html |
||
53 | * @see Intl extensions http://twig.sensiolabs.org/doc/extensions/intl.html |
||
54 | * @see Array extension http://twig.sensiolabs.org/doc/extensions/array.html |
||
55 | * @see Time extension http://twig.sensiolabs.org/doc/extensions/date.html |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | 1 | public function registerMarkupTags() |
|
60 | { |
||
61 | 1 | $filters = []; |
|
62 | 1 | $functions = []; |
|
63 | |||
64 | // init Twig |
||
65 | 1 | $twig = $this->app->make('twig.environment'); |
|
66 | |||
67 | // add String Loader functions |
||
68 | 1 | $functions += $this->getStringLoaderFunctions($twig); |
|
69 | |||
70 | // add Config function |
||
71 | 1 | $functions += $this->getConfigFunction(); |
|
72 | |||
73 | // add Session function |
||
74 | 1 | $functions += $this->getSessionFunction(); |
|
75 | |||
76 | // add Trans function |
||
77 | 1 | $functions += $this->getTransFunction(); |
|
78 | |||
79 | // add var_dump function |
||
80 | 1 | $functions += $this->getVarDumpFunction(); |
|
81 | |||
82 | // add Text extensions |
||
83 | 1 | $filters += $this->getTextFilters($twig); |
|
84 | |||
85 | // add Intl extensions if php5-intl installed |
||
86 | 1 | if (class_exists('IntlDateFormatter')) { |
|
87 | 1 | $filters += $this->getLocalizedFilters($twig); |
|
88 | 1 | } |
|
89 | |||
90 | // add Array extensions |
||
91 | 1 | $filters += $this->getArrayFilters(); |
|
92 | |||
93 | // add Time extensions |
||
94 | 1 | $filters += $this->getTimeFilters($twig); |
|
95 | |||
96 | // add Mail filters |
||
97 | 1 | $filters += $this->getMailFilters(); |
|
98 | |||
99 | // add PHP functions |
||
100 | 1 | $filters += $this->getPhpFunctions(); |
|
101 | |||
102 | // add File Version filter |
||
103 | 1 | $filters += $this->getFileRevision(); |
|
104 | 1 | ||
105 | 1 | return [ |
|
106 | 'filters' => $filters, |
||
107 | 'functions' => $functions, |
||
108 | ]; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Returns String Loader functions. |
||
113 | * |
||
114 | * @param \Twig_Environment $twig |
||
115 | 1 | * |
|
116 | * @return array |
||
117 | 1 | */ |
|
118 | 1 | private function getStringLoaderFunctions($twig) |
|
130 | |||
131 | /** |
||
132 | * Returns Text filters. |
||
133 | * |
||
134 | * @param \Twig_Environment $twig |
||
135 | 3 | * |
|
136 | * @return array |
||
137 | 1 | */ |
|
138 | 1 | private function getTextFilters($twig) |
|
154 | |||
155 | /** |
||
156 | * Returns Intl filters. |
||
157 | * |
||
158 | * @param \Twig_Environment $twig |
||
159 | 1 | * |
|
160 | * @return array |
||
161 | 1 | */ |
|
162 | 1 | private function getLocalizedFilters($twig) |
|
182 | |||
183 | /** |
||
184 | * Returns Array filters. |
||
185 | 2 | * |
|
186 | * @return array |
||
187 | 1 | */ |
|
188 | 1 | private function getArrayFilters() |
|
200 | |||
201 | /** |
||
202 | * Returns Date filters. |
||
203 | * |
||
204 | * @param \Twig_Environment $twig |
||
205 | 1 | * |
|
206 | * @return array |
||
207 | 1 | */ |
|
208 | 1 | private function getTimeFilters($twig) |
|
221 | |||
222 | /** |
||
223 | * Returns mail filters. |
||
224 | 1 | * |
|
225 | * @return array |
||
226 | */ |
||
227 | private function getMailFilters() |
||
235 | |||
236 | /** |
||
237 | * Returns plain PHP functions. |
||
238 | 1 | * |
|
239 | * @return array |
||
240 | */ |
||
241 | private function getPhpFunctions() |
||
293 | |||
294 | /** |
||
295 | * Works like the config() helper function. |
||
296 | 1 | * |
|
297 | * @return array |
||
298 | */ |
||
299 | private function getConfigFunction() |
||
307 | |||
308 | /** |
||
309 | * Works like the session() helper function. |
||
310 | 1 | * |
|
311 | * @return array |
||
312 | */ |
||
313 | private function getSessionFunction() |
||
321 | |||
322 | /** |
||
323 | * Works like the trans() helper function. |
||
324 | 1 | * |
|
325 | * @return array |
||
326 | */ |
||
327 | private function getTransFunction() |
||
335 | |||
336 | /** |
||
337 | * Dumps information about a variable. |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | 1 | private function getVarDumpFunction() |
|
353 | |||
354 | /** |
||
355 | * Create protected link with mailto: |
||
356 | * |
||
357 | * @param string $email Email to render. |
||
358 | * @param bool $link If email should be rendered as link. |
||
359 | * @param bool $protected If email should be protected. |
||
360 | * @param string $text Link text. Render email by default. |
||
361 | * |
||
362 | * @see http://www.maurits.vdschee.nl/php_hide_email/ |
||
363 | 1 | * |
|
364 | * @return string |
||
365 | */ |
||
366 | 1 | private function hideEmail($email, $link = true, $protected = true, $text = null) |
|
401 | |||
402 | /** |
||
403 | * Appends this pattern: ? . {last modified date} |
||
404 | * to an assets filename to force browser to reload |
||
405 | * cached modified file. |
||
406 | * |
||
407 | * See: https://github.com/vojtasvoboda/oc-twigextensions-plugin/issues/25 |
||
408 | * |
||
409 | * @param string $filename Filename of the asset file |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | private function getFileRevision() |
||
432 | } |
||
433 |
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.