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 |
||
18 | class Twig |
||
19 | { |
||
20 | /** |
||
21 | * @var array Paths to Twig templates |
||
22 | */ |
||
23 | private $paths = []; |
||
24 | |||
25 | /** |
||
26 | * @var array Twig Environment Options |
||
27 | * @see http://twig.sensiolabs.org/doc/api.html#environment-options |
||
28 | */ |
||
29 | private $config = []; |
||
30 | |||
31 | /** |
||
32 | * @var array Functions to add to Twig |
||
33 | */ |
||
34 | private $functions_asis = [ |
||
35 | 'base_url', 'site_url', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * @var array Functions with `is_safe` option |
||
40 | * @see http://twig.sensiolabs.org/doc/advanced.html#automatic-escaping |
||
41 | */ |
||
42 | private $functions_safe = [ |
||
43 | 'form_open', 'form_close', 'form_error', 'form_hidden', 'set_value', |
||
44 | // 'form_open_multipart', 'form_upload', 'form_submit', 'form_dropdown', |
||
45 | // 'set_radio', 'set_select', 'set_checkbox', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var bool Whether functions are added or not |
||
50 | */ |
||
51 | private $functions_added = FALSE; |
||
52 | |||
53 | /** |
||
54 | * @var Twig_Environment |
||
55 | */ |
||
56 | private $twig; |
||
57 | |||
58 | /** |
||
59 | * @var Twig_Loader_Filesystem |
||
60 | */ |
||
61 | private $loader; |
||
62 | |||
63 | public function __construct($params = []) |
||
101 | |||
102 | protected function resetTwig() |
||
107 | |||
108 | protected function createTwig() |
||
130 | |||
131 | protected function setLoader($loader) |
||
135 | |||
136 | /** |
||
137 | * Registers a Global |
||
138 | * |
||
139 | * @param string $name The global name |
||
140 | * @param mixed $value The global value |
||
141 | */ |
||
142 | public function addGlobal($name, $value) |
||
147 | |||
148 | /** |
||
149 | * Renders Twig Template and Set Output |
||
150 | * |
||
151 | * @param string $view Template filename without `.twig` |
||
152 | * @param array $params Array of parameters to pass to the template |
||
153 | */ |
||
154 | public function display($view, $params = []) |
||
159 | |||
160 | /** |
||
161 | * Renders Twig Template and Returns as String |
||
162 | * |
||
163 | * @param string $view Template filename without `.twig` |
||
164 | * @param array $params Array of parameters to pass to the template |
||
165 | * @return string |
||
166 | */ |
||
167 | public function render($view, $params = []) |
||
177 | |||
178 | protected function addFunctions() |
||
229 | |||
230 | /** |
||
231 | * @param string $uri |
||
232 | * @param string $title |
||
233 | * @param array $attributes [changed] only array is acceptable |
||
234 | * @return string |
||
235 | */ |
||
236 | public function safe_anchor($uri = '', $title = '', $attributes = []) |
||
249 | |||
250 | /** |
||
251 | * @return \Twig_Environment |
||
252 | */ |
||
253 | public function getTwig() |
||
258 | } |
||
259 |
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.