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 | 2 | public function __construct(array $applicationConfiguration = [], $locale) |
|
| 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 | 1 | public function getTranslationKey($key, $adminName = null) |
|
| 461 | { |
||
| 462 | 1 | $translationKey = $this->translationPattern; |
|
| 463 | |||
| 464 | 1 | if (strstr($this->translationPattern, '{admin}') && $adminName != null) { |
|
| 465 | $translationKey = str_replace('{admin}', $adminName, $translationKey); |
||
| 466 | } |
||
| 467 | 1 | $translationKey = str_replace('{key}', $key, $translationKey); |
|
| 468 | |||
| 469 | 1 | 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 |