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 |
||
| 11 | class Config extends Content |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string $defaultMetaTitle |
||
| 15 | */ |
||
| 16 | protected $defaultMetaTitle; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string $defaultMetaDescription |
||
| 20 | */ |
||
| 21 | protected $defaultMetaDescription; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string $defaultMetaImage |
||
| 25 | */ |
||
| 26 | protected $defaultMetaImage; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string $defaultMetaUrl |
||
| 30 | */ |
||
| 31 | protected $defaultMetaUrl; |
||
| 32 | |||
| 33 | // ========================================================================== |
||
| 34 | // INIT |
||
| 35 | // ========================================================================== |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Section constructor. |
||
| 39 | * @param array $data The data. |
||
| 40 | */ |
||
| 41 | View Code Duplication | public function __construct(array $data = null) |
|
| 49 | |||
| 50 | // ========================================================================== |
||
| 51 | // SETTERS |
||
| 52 | // ========================================================================== |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param mixed $defaultMetaTitle The default meta title. |
||
| 56 | * @return self |
||
| 57 | */ |
||
| 58 | public function setDefaultMetaTitle($defaultMetaTitle) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param mixed $defaultMetaDescription The default meta description. |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | public function setDefaultMetaDescription($defaultMetaDescription) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param mixed $defaultMetaImage The default meta image. |
||
| 78 | * @return self |
||
| 79 | */ |
||
| 80 | public function setDefaultMetaImage($defaultMetaImage) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param mixed $defaultMetaUrl The default meta url. |
||
| 89 | * @return self |
||
| 90 | */ |
||
| 91 | public function setDefaultMetaUrl($defaultMetaUrl) |
||
| 97 | |||
| 98 | // ========================================================================== |
||
| 99 | // GETTERS |
||
| 100 | // ========================================================================== |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function defaultMetaTitle() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | public function defaultMetaDescription() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | public function defaultMetaImage() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | public function defaultMetaUrl() |
||
| 133 | } |
||
| 134 |
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.