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 |
||
| 17 | class Alert |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $title = ''; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Localized |
||
| 26 | */ |
||
| 27 | private $titleLocalized; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $body = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Localized |
||
| 36 | */ |
||
| 37 | private $bodyLocalized; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Localized |
||
| 41 | */ |
||
| 42 | private $actionLocalized; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $launchImage = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor. |
||
| 51 | * |
||
| 52 | * @param string $body |
||
| 53 | * @param string $title |
||
| 54 | */ |
||
| 55 | public function __construct(string $body = '', string $title = '') |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Set title |
||
| 66 | * |
||
| 67 | * @param string $title |
||
| 68 | * |
||
| 69 | * @return Alert |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function withTitle(string $title) : Alert |
|
| 80 | |||
| 81 | /** |
||
| 82 | * With localized title |
||
| 83 | * |
||
| 84 | * @param Localized $localized |
||
| 85 | * |
||
| 86 | * @return Alert |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function withLocalizedTitle(Localized $localized) : Alert |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get title |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getTitle() : string |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get localized title |
||
| 110 | * |
||
| 111 | * @return Localized |
||
| 112 | */ |
||
| 113 | public function getTitleLocalized() : Localized |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set body |
||
| 120 | * |
||
| 121 | * @param string $body |
||
| 122 | * |
||
| 123 | * @return Alert |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function withBody(string $body) : Alert |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Set localized body |
||
| 137 | * |
||
| 138 | * @param Localized $localized |
||
| 139 | * |
||
| 140 | * @return Alert |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function withLocalizedBody(Localized $localized) : Alert |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Get localized body |
||
| 154 | * |
||
| 155 | * @return Localized |
||
| 156 | */ |
||
| 157 | public function getBodyLocalized() : Localized |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get body |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getBody() : string |
||
| 171 | |||
| 172 | /** |
||
| 173 | * With localized action |
||
| 174 | * |
||
| 175 | * @param Localized $localized |
||
| 176 | * |
||
| 177 | * @return Alert |
||
| 178 | */ |
||
| 179 | public function withLocalizedAction(Localized $localized) : Alert |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get localized action |
||
| 190 | * |
||
| 191 | * @return Localized |
||
| 192 | */ |
||
| 193 | public function getLocalizedAction() : Localized |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Add launch image |
||
| 200 | * |
||
| 201 | * @param string $launchImage |
||
| 202 | * |
||
| 203 | * @return Alert |
||
| 204 | */ |
||
| 205 | public function withLaunchImage(string $launchImage) : Alert |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get launch image |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getLaunchImage() : string |
||
| 223 | } |
||
| 224 |
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.