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 |
||
10 | class ApplicationConfiguration |
||
11 | { |
||
12 | /** |
||
13 | * Application title. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $title; |
||
18 | |||
19 | /** |
||
20 | * Application description. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $description; |
||
25 | |||
26 | /** |
||
27 | * Application locale. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $locale; |
||
32 | |||
33 | /** |
||
34 | * Admin main twig layout. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $layout; |
||
39 | |||
40 | /** |
||
41 | * Twig template use for rendering block in forms. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $blockTemplate; |
||
46 | |||
47 | /** |
||
48 | * Use bootstrap integration. |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $bootstrap = false; |
||
53 | |||
54 | /** |
||
55 | * Application main date format. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $dateFormat; |
||
60 | |||
61 | /** |
||
62 | * String length before truncate it (if null, no truncation). |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $stringLength; |
||
67 | |||
68 | /** |
||
69 | * Replace string in truncation. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $stringLengthTruncate; |
||
74 | |||
75 | /** |
||
76 | * Url routing pattern. |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $routingUrlPattern; |
||
81 | |||
82 | /** |
||
83 | * Generated route name pattern. |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $routingNamePattern; |
||
88 | |||
89 | /** |
||
90 | * Default number of displayed records in list. |
||
91 | * |
||
92 | * @var int |
||
93 | */ |
||
94 | protected $maxPerPage; |
||
95 | |||
96 | /** |
||
97 | * Define wether if translator should be used or not. |
||
98 | * |
||
99 | * @var bool |
||
100 | */ |
||
101 | protected $useTranslation = true; |
||
102 | |||
103 | /** |
||
104 | * Pattern use for translation key (ie: lag.admin.{key}, admin will. |
||
105 | * |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $translationPattern; |
||
109 | |||
110 | /** |
||
111 | * Contains a array of fqcn field classes indexed by field name. |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | protected $fieldsMapping = []; |
||
116 | |||
117 | /** |
||
118 | * ApplicationConfiguration constructor. |
||
119 | * |
||
120 | * @param array $applicationConfiguration |
||
121 | * @param $locale |
||
122 | */ |
||
123 | 1 | public function __construct(array $applicationConfiguration = [], $locale) |
|
124 | { |
||
125 | 1 | $resolver = new OptionsResolver(); |
|
126 | 1 | $resolver->setDefaults([ |
|
127 | 1 | 'enable_extra_configuration' => true, |
|
128 | 1 | 'title' => '', |
|
129 | 1 | 'description' => '', |
|
130 | 1 | 'locale' => $locale, |
|
131 | 1 | 'layout' => 'LAGAdminBundle::admin.layout.html.twig', |
|
132 | 1 | 'block_template' => 'LAGAdminBundle:Form:fields.html.twig', |
|
133 | 'bootstrap' => false, |
||
134 | 1 | 'date_format' => 'd/m/Y', |
|
135 | 1 | 'string_length' => 0, |
|
136 | 1 | 'string_length_truncate' => '...', |
|
137 | 'routing' => [ |
||
138 | 'url_pattern' => '/{admin}/{action}', |
||
139 | 'name_pattern' => 'lag.admin.{admin}', |
||
140 | ], |
||
141 | 'translation' => [ |
||
142 | 'enabled' => true, |
||
143 | 'pattern' => 'lag.admin.{key}', |
||
144 | ], |
||
145 | 1 | 'max_per_page' => 25, |
|
146 | 'fields_mapping' => [ |
||
147 | ], |
||
148 | ]); |
||
149 | 1 | $resolver->setAllowedValues('enable_extra_configuration', [true, false]); |
|
150 | 1 | $applicationConfiguration = $resolver->resolve($applicationConfiguration); |
|
151 | // merge default field configuration |
||
152 | 1 | $applicationConfiguration['fields_mapping'] = array_merge([ |
|
153 | 1 | Field::TYPE_STRING => 'LAG\AdminBundle\Field\Field\StringField', |
|
154 | 1 | Field::TYPE_ARRAY => 'LAG\AdminBundle\Field\Field\ArrayField', |
|
155 | 1 | Field::TYPE_LINK => 'LAG\AdminBundle\Field\Field\Link', |
|
156 | 1 | Field::TYPE_DATE => 'LAG\AdminBundle\Field\Field\Date', |
|
157 | 1 | Field::TYPE_COUNT => 'LAG\AdminBundle\Field\Field\Count', |
|
158 | 1 | Field::TYPE_ACTION => 'LAG\AdminBundle\Field\Field\Action', |
|
159 | 1 | Field::TYPE_COLLECTION => 'LAG\AdminBundle\Field\Field\Collection', |
|
160 | 1 | Field::TYPE_BOOLEAN => 'LAG\AdminBundle\Field\Field\Boolean', |
|
161 | 1 | ], $applicationConfiguration['fields_mapping']); |
|
162 | |||
163 | // resolving routing options |
||
164 | 1 | $routingConfiguration = $applicationConfiguration['routing']; |
|
165 | 1 | $resolver->clear(); |
|
166 | 1 | $resolver->setRequired([ |
|
167 | 1 | 'url_pattern', |
|
168 | 'name_pattern', |
||
169 | ]); |
||
170 | $resolver->setNormalizer('url_pattern', function (Options $options, $value) { |
||
171 | 1 | if (strstr($value, '{admin}') === false) { |
|
172 | throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder'); |
||
173 | } |
||
174 | 1 | if (strstr($value, '{action}') === false) { |
|
175 | throw new InvalidOptionsException('Admin routing configuration url pattern should contains {action} placeholder'); |
||
176 | } |
||
177 | |||
178 | 1 | return $value; |
|
179 | 1 | }); |
|
180 | View Code Duplication | $resolver->setNormalizer('name_pattern', function (Options $options, $value) { |
|
181 | 1 | if (strstr($value, '{admin}') === false) { |
|
182 | throw new InvalidOptionsException('Admin routing configuration pattern name should contains {admin} placeholder'); |
||
183 | } |
||
184 | |||
185 | 1 | return $value; |
|
186 | 1 | }); |
|
187 | 1 | $routingConfiguration = $resolver->resolve($routingConfiguration); |
|
188 | // routing configuration |
||
189 | 1 | $this->routingUrlPattern = $routingConfiguration['url_pattern']; |
|
190 | 1 | $this->routingNamePattern = $routingConfiguration['name_pattern']; |
|
191 | |||
192 | // resolving translation configuration |
||
193 | 1 | $translationConfiguration = $applicationConfiguration['translation']; |
|
194 | $resolver |
||
195 | 1 | ->clear() |
|
196 | 1 | ->setDefault('enabled', true) |
|
197 | 1 | ->setDefault('pattern', 'lag.admin.{key}'); |
|
198 | 1 | View Code Duplication | $resolver->setNormalizer('pattern', function (Options $options, $value) { |
199 | 1 | if (strstr($value, 'key') === false) { |
|
200 | throw new InvalidOptionsException('Admin translation configuration pattern should contains {key} placeholder'); |
||
201 | } |
||
202 | |||
203 | 1 | return $value; |
|
204 | 1 | }); |
|
205 | 1 | $translationConfiguration = $resolver->resolve($translationConfiguration); |
|
206 | // translation configuration |
||
207 | 1 | $this->useTranslation = $translationConfiguration['enabled']; |
|
208 | 1 | $this->translationPattern = $translationConfiguration['pattern']; |
|
209 | |||
210 | // application main configuration |
||
211 | 1 | $this->title = $applicationConfiguration['title']; |
|
212 | 1 | $this->description = $applicationConfiguration['description']; |
|
213 | 1 | $this->locale = $applicationConfiguration['locale']; |
|
214 | 1 | $this->title = $applicationConfiguration['title']; |
|
215 | 1 | $this->layout = $applicationConfiguration['layout']; |
|
216 | 1 | $this->blockTemplate = $applicationConfiguration['block_template']; |
|
217 | 1 | $this->bootstrap = $applicationConfiguration['bootstrap']; |
|
218 | 1 | $this->dateFormat = $applicationConfiguration['date_format']; |
|
219 | 1 | $this->stringLength = $applicationConfiguration['string_length']; |
|
220 | 1 | $this->stringLengthTruncate = $applicationConfiguration['string_length_truncate']; |
|
221 | 1 | $this->maxPerPage = $applicationConfiguration['max_per_page']; |
|
222 | 1 | $this->fieldsMapping = $applicationConfiguration['fields_mapping']; |
|
223 | 1 | } |
|
224 | |||
225 | /** |
||
226 | * @return mixed |
||
227 | */ |
||
228 | public function getTitle() |
||
232 | |||
233 | /** |
||
234 | * @param mixed $title |
||
235 | */ |
||
236 | public function setTitle($title) |
||
240 | |||
241 | /** |
||
242 | * @return mixed |
||
243 | */ |
||
244 | public function getLayout() |
||
248 | |||
249 | /** |
||
250 | * @param mixed $layout |
||
251 | */ |
||
252 | public function setLayout($layout) |
||
256 | |||
257 | /** |
||
258 | * @return mixed |
||
259 | */ |
||
260 | public function getBlockTemplate() |
||
264 | |||
265 | /** |
||
266 | * @param mixed $blockTemplate |
||
267 | */ |
||
268 | public function setBlockTemplate($blockTemplate) |
||
272 | |||
273 | /** |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function getDescription() |
||
280 | |||
281 | /** |
||
282 | * @param mixed $description |
||
283 | */ |
||
284 | public function setDescription($description) |
||
288 | |||
289 | /** |
||
290 | * @return bool |
||
291 | */ |
||
292 | public function useBootstrap() |
||
296 | |||
297 | /** |
||
298 | * @param bool $bootstrap |
||
299 | */ |
||
300 | public function setBootstrap($bootstrap) |
||
304 | |||
305 | /** |
||
306 | * @return mixed |
||
307 | */ |
||
308 | public function getDateFormat() |
||
312 | |||
313 | /** |
||
314 | * @return string |
||
315 | */ |
||
316 | public function getJavascriptDateFormat() |
||
326 | |||
327 | /** |
||
328 | * @param mixed $dateFormat |
||
329 | */ |
||
330 | public function setDateFormat($dateFormat) |
||
334 | |||
335 | /** |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getRoutingUrlPattern() |
||
342 | |||
343 | /** |
||
344 | * @param string $routingUrlPattern |
||
345 | */ |
||
346 | public function setRoutingUrlPattern($routingUrlPattern) |
||
350 | |||
351 | /** |
||
352 | * @return string |
||
353 | */ |
||
354 | public function getRoutingNamePattern() |
||
358 | |||
359 | /** |
||
360 | * @param string $routingNamePattern |
||
361 | */ |
||
362 | public function setRoutingNamePattern($routingNamePattern) |
||
366 | |||
367 | /** |
||
368 | * @return string |
||
369 | */ |
||
370 | public function getLocale() |
||
374 | |||
375 | /** |
||
376 | * @param string $locale |
||
377 | */ |
||
378 | public function setLocale($locale) |
||
382 | |||
383 | /** |
||
384 | * @return int |
||
385 | */ |
||
386 | public function getStringLength() |
||
390 | |||
391 | /** |
||
392 | * @param int $stringLength |
||
393 | */ |
||
394 | public function setStringLength($stringLength) |
||
398 | |||
399 | /** |
||
400 | * @return mixed |
||
401 | */ |
||
402 | public function getStringLengthTruncate() |
||
406 | |||
407 | /** |
||
408 | * @param mixed $stringLengthTruncate |
||
409 | */ |
||
410 | public function setStringLengthTruncate($stringLengthTruncate) |
||
414 | |||
415 | /** |
||
416 | * @return bool |
||
417 | */ |
||
418 | public function isBootstrap() |
||
422 | |||
423 | /** |
||
424 | * @return int |
||
425 | */ |
||
426 | public function getMaxPerPage() |
||
430 | |||
431 | /** |
||
432 | * @param int $maxPerPage |
||
433 | */ |
||
434 | public function setMaxPerPage($maxPerPage) |
||
438 | |||
439 | /** |
||
440 | * @return string |
||
441 | */ |
||
442 | public function getTranslationPattern() |
||
446 | |||
447 | /** |
||
448 | * @param string $translationPattern |
||
449 | */ |
||
450 | public function setTranslationPattern($translationPattern) |
||
454 | |||
455 | /** |
||
456 | * @param $key |
||
457 | * @param string $adminName |
||
458 | * @return string |
||
459 | */ |
||
460 | public function getTranslationKey($key, $adminName = null) |
||
461 | { |
||
462 | $translationKey = $this->translationPattern; |
||
463 | |||
464 | if (strstr($this->translationPattern, '{admin}') && $adminName != null) { |
||
465 | $translationKey = str_replace('{admin}', $adminName, $translationKey); |
||
466 | } |
||
467 | $translationKey = str_replace('{key}', $key, $translationKey); |
||
468 | |||
469 | return $translationKey; |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * @return bool |
||
474 | */ |
||
475 | public function useTranslation() |
||
479 | |||
480 | /** |
||
481 | * @param bool $useTranslation |
||
482 | */ |
||
483 | public function setUseTranslation($useTranslation) |
||
487 | |||
488 | /** |
||
489 | * Return array field mapping |
||
490 | * |
||
491 | * @return array |
||
492 | */ |
||
493 | public function getFieldsMapping() |
||
497 | } |
||
498 |