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:
Complex classes like ApplicationConfiguration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApplicationConfiguration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ApplicationConfiguration |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Indicate wether or not the extra configuration should be enabled or not. |
||
| 14 | * |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | protected $enableExtraConfiguration = true; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Application title. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $title; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Application description. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $description; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Application locale. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $locale; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Admin main twig layout. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $layout; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Twig template use for rendering block in forms. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $blockTemplate; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Use bootstrap integration. |
||
| 56 | * |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $bootstrap = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Application main date format. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $dateFormat; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * String length before truncate it (if null, no truncation). |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $stringLength; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Replace string in truncation. |
||
| 77 | * |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | protected $stringLengthTruncate; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Url routing pattern. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $routingUrlPattern; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Generated route name pattern. |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $routingNamePattern; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Default number of displayed records in list. |
||
| 98 | * |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | protected $maxPerPage; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Define wether if translator should be used or not. |
||
| 105 | * |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | protected $useTranslation = true; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Pattern use for translation key (ie: lag.admin.{key}, admin will. |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | protected $translationPattern; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Contains a array of fqcn field classes indexed by field name. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | protected $fieldsMapping = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * ApplicationConfiguration constructor. |
||
| 126 | * |
||
| 127 | * @param array $applicationConfiguration |
||
| 128 | * @param $locale |
||
| 129 | */ |
||
| 130 | 3 | public function __construct(array $applicationConfiguration = [], $locale) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | 1 | public function getTitle() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param mixed $title |
||
| 242 | */ |
||
| 243 | public function setTitle($title) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return mixed |
||
| 250 | */ |
||
| 251 | 1 | public function getLayout() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param mixed $layout |
||
| 258 | */ |
||
| 259 | public function setLayout($layout) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | 1 | public function getBlockTemplate() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param mixed $blockTemplate |
||
| 274 | */ |
||
| 275 | public function setBlockTemplate($blockTemplate) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | 1 | public function getDescription() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param mixed $description |
||
| 290 | */ |
||
| 291 | public function setDescription($description) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 1 | public function useBootstrap() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param bool $bootstrap |
||
| 306 | */ |
||
| 307 | public function setBootstrap($bootstrap) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | 1 | public function getDateFormat() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getJavascriptDateFormat() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param mixed $dateFormat |
||
| 336 | */ |
||
| 337 | public function setDateFormat($dateFormat) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | 1 | public function getRoutingUrlPattern() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $routingUrlPattern |
||
| 352 | */ |
||
| 353 | public function setRoutingUrlPattern($routingUrlPattern) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | 1 | public function getRoutingNamePattern() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $routingNamePattern |
||
| 368 | */ |
||
| 369 | public function setRoutingNamePattern($routingNamePattern) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | 1 | public function getLocale() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $locale |
||
| 384 | */ |
||
| 385 | public function setLocale($locale) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return int |
||
| 392 | */ |
||
| 393 | 1 | public function getStringLength() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param int $stringLength |
||
| 400 | */ |
||
| 401 | public function setStringLength($stringLength) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return mixed |
||
| 408 | */ |
||
| 409 | 1 | public function getStringLengthTruncate() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param mixed $stringLengthTruncate |
||
| 416 | */ |
||
| 417 | public function setStringLengthTruncate($stringLengthTruncate) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | 1 | public function isBootstrap() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @return int |
||
| 432 | */ |
||
| 433 | 1 | public function getMaxPerPage() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @param int $maxPerPage |
||
| 440 | */ |
||
| 441 | public function setMaxPerPage($maxPerPage) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | 1 | public function getTranslationPattern() |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $translationPattern |
||
| 456 | */ |
||
| 457 | public function setTranslationPattern($translationPattern) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param $key |
||
| 464 | * @param string $adminName |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | 1 | public function getTranslationKey($key, $adminName = null) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @return bool |
||
| 481 | */ |
||
| 482 | public function useTranslation() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param bool $useTranslation |
||
| 489 | */ |
||
| 490 | public function setUseTranslation($useTranslation) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Return array field mapping |
||
| 497 | * |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | 1 | public function getFieldsMapping() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @return boolean |
||
| 507 | */ |
||
| 508 | 1 | public function isExtraConfigurationEnabled() |
|
| 512 | } |
||
| 513 |