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() |
||
35 | |||
36 | /** |
||
37 | * Add Twig extensions |
||
38 | * |
||
39 | * @see Text extensions http://twig.sensiolabs.org/doc/extensions/text.html |
||
40 | * @see Intl extensions http://twig.sensiolabs.org/doc/extensions/intl.html |
||
41 | * @see Array extension http://twig.sensiolabs.org/doc/extensions/array.html |
||
42 | * @see Time extension http://twig.sensiolabs.org/doc/extensions/date.html |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | 1 | public function registerMarkupTags() |
|
47 | { |
||
48 | 1 | $filters = []; |
|
49 | 1 | $functions = []; |
|
50 | |||
51 | // init Twig |
||
52 | 1 | $twig = App::make('twig.environment'); |
|
53 | |||
54 | // add String Loader functions |
||
55 | 1 | $functions += $this->getStringLoaderFunctions($twig); |
|
56 | |||
57 | // add Config function |
||
58 | 1 | $functions += $this->getConfigFunction(); |
|
59 | |||
60 | // add Text extensions |
||
61 | 1 | $filters += $this->getTextFilters($twig); |
|
62 | |||
63 | // add Intl extensions if php5-intl installed |
||
64 | 1 | if (class_exists('IntlDateFormatter')) { |
|
65 | 1 | $filters += $this->getLocalizedFilters($twig); |
|
66 | 1 | } |
|
67 | |||
68 | // add Array extensions |
||
69 | 1 | $filters += $this->getArrayFilters($twig); |
|
70 | |||
71 | // add Time extensions |
||
72 | 1 | $filters += $this->getTimeFilters($twig); |
|
73 | |||
74 | // add PHP functions |
||
75 | 1 | $filters += $this->getPhpFunctions(); |
|
76 | |||
77 | return [ |
||
78 | 1 | 'filters' => $filters, |
|
79 | 'functions' => $functions |
||
80 | 1 | ]; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns String Loader functions |
||
85 | * |
||
86 | * @param \Twig_Environment $twig |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | 1 | private function getStringLoaderFunctions($twig) |
|
91 | { |
||
92 | 1 | $stringLoader = new Twig_Extension_StringLoader(); |
|
93 | 1 | $stringLoaderFunc = $stringLoader->getFunctions(); |
|
94 | |||
95 | return [ |
||
96 | 'template_from_string' => function($template) use ($twig, $stringLoaderFunc) { |
||
97 | 1 | $callable = $stringLoaderFunc[0]->getCallable(); |
|
98 | 1 | return $callable($twig, $template); |
|
99 | } |
||
100 | 1 | ]; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns Text filters |
||
105 | * |
||
106 | * @param \Twig_Environment $twig |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | 3 | private function getTextFilters($twig) |
|
111 | { |
||
112 | 1 | $textExtension = new Twig_Extensions_Extension_Text(); |
|
113 | 1 | $textFilters = $textExtension->getFilters(); |
|
114 | |||
115 | return [ |
||
116 | View Code Duplication | 'truncate' => function($value, $length = 30, $preserve = false, $separator = '...') use ($twig, $textFilters) { |
|
1 ignored issue
–
show
|
|||
117 | 3 | $callable = $textFilters[0]->getCallable(); |
|
118 | 3 | return $callable($twig, $value, $length, $preserve, $separator); |
|
119 | 1 | }, |
|
120 | View Code Duplication | 'wordwrap' => function($value, $length = 80, $separator = "\n", $preserve = false) use ($twig, $textFilters) { |
|
1 ignored issue
–
show
|
|||
121 | 1 | $callable = $textFilters[1]->getCallable(); |
|
122 | 1 | return $callable($twig, $value, $length, $separator, $preserve); |
|
123 | } |
||
124 | 1 | ]; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Returns Intl filters |
||
129 | * |
||
130 | * @param \Twig_Environment $twig |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 1 | private function getLocalizedFilters($twig) |
|
135 | { |
||
136 | 1 | $intlExtension = new Twig_Extensions_Extension_Intl(); |
|
137 | 1 | $intlFilters = $intlExtension->getFilters(); |
|
138 | |||
139 | return [ |
||
140 | 'localizeddate' => function($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) use ($twig, $intlFilters) { |
||
141 | $callable = $intlFilters[0]->getCallable(); |
||
142 | return $callable($twig, $date, $dateFormat, $timeFormat, $locale, $timezone, $format); |
||
143 | 1 | }, |
|
144 | 'localizednumber' => function($number, $style = 'decimal', $type = 'default', $locale = null) use ($twig, $intlFilters) { |
||
145 | $callable = $intlFilters[1]->getCallable(); |
||
146 | return $callable($twig, $number, $style, $type, $locale); |
||
147 | 1 | }, |
|
148 | 'localizedcurrency' => function($number, $currency = null, $locale = null) use ($twig, $intlFilters) { |
||
149 | $callable = $intlFilters[2]->getCallable(); |
||
150 | return $callable($twig, $number, $currency, $locale); |
||
151 | } |
||
152 | 1 | ]; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Returns Array filters |
||
157 | * |
||
158 | * @param \Twig_Environment $twig |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 1 | private function getArrayFilters($twig) |
|
163 | { |
||
164 | 1 | $arrayExtension = new Twig_Extensions_Extension_Array(); |
|
165 | 1 | $arrayFilters = $arrayExtension->getFilters(); |
|
166 | |||
167 | return [ |
||
168 | 'shuffle' => function($array) use ($twig, $arrayFilters) { |
||
169 | $callable = $arrayFilters[0]->getCallable(); |
||
170 | return $callable($twig, $array); |
||
171 | } |
||
172 | 1 | ]; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Returns Date filters |
||
177 | * |
||
178 | * @param \Twig_Environment $twig |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | 1 | private function getTimeFilters($twig) |
|
183 | { |
||
184 | 1 | $timeExtension = new Twig_Extensions_Extension_Date(); |
|
185 | 1 | $timeFilters = $timeExtension->getFilters(); |
|
186 | |||
187 | return [ |
||
188 | 'time_diff' => function($date, $now = null) use ($twig, $timeFilters) { |
||
189 | 1 | $callable = $timeFilters[0]->getCallable(); |
|
190 | 1 | return $callable($twig, $date, $now); |
|
191 | } |
||
192 | 1 | ]; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Returns plain PHP functions |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 1 | private function getPhpFunctions() |
|
242 | |||
243 | /** |
||
244 | * Works like the config() function |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | private function getConfigFunction() |
||
256 | } |
||
257 |
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.