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 |
||
| 19 | class Alert |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $title; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Localized |
||
| 28 | */ |
||
| 29 | private $titleLocalized; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $body; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Localized |
||
| 38 | */ |
||
| 39 | private $bodyLocalized; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Localized |
||
| 43 | */ |
||
| 44 | private $actionLocalized; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $launchImage = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor. |
||
| 53 | * |
||
| 54 | * @param string $body |
||
| 55 | * @param string $title |
||
| 56 | */ |
||
| 57 | public function __construct(string $body = '', string $title = '') |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set title |
||
| 68 | * |
||
| 69 | * @param string $title |
||
| 70 | * |
||
| 71 | * @return Alert |
||
| 72 | */ |
||
| 73 | public function withTitle(string $title): Alert |
||
| 82 | |||
| 83 | /** |
||
| 84 | * With localized title |
||
| 85 | * |
||
| 86 | * @param Localized $localized |
||
| 87 | * |
||
| 88 | * @return Alert |
||
| 89 | */ |
||
| 90 | public function withLocalizedTitle(Localized $localized): Alert |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get title |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getTitle(): string |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get localized title |
||
| 112 | * |
||
| 113 | * @return Localized |
||
| 114 | */ |
||
| 115 | public function getTitleLocalized(): Localized |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Set body |
||
| 122 | * |
||
| 123 | * @param string $body |
||
| 124 | * |
||
| 125 | * @return Alert |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function withBody(string $body): Alert |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Set localized body |
||
| 139 | * |
||
| 140 | * @param Localized $localized |
||
| 141 | * |
||
| 142 | * @return Alert |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function withBodyLocalized(Localized $localized): Alert |
|
| 145 | { |
||
| 146 | $cloned = clone $this; |
||
| 147 | |||
| 148 | $cloned->body = ''; |
||
| 149 | $cloned->bodyLocalized = $localized; |
||
| 150 | |||
| 151 | return $cloned; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get localized body |
||
| 156 | * |
||
| 157 | * @return Localized |
||
| 158 | */ |
||
| 159 | public function getBodyLocalized(): Localized |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get body |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getBody(): string |
||
| 173 | |||
| 174 | /** |
||
| 175 | * With localized action |
||
| 176 | * |
||
| 177 | * @param Localized $localized |
||
| 178 | * |
||
| 179 | * @return Alert |
||
| 180 | */ |
||
| 181 | public function withActionLocalized(Localized $localized): Alert |
||
| 182 | { |
||
| 183 | $cloned = clone $this; |
||
| 184 | |||
| 185 | $cloned->actionLocalized = $localized; |
||
| 186 | |||
| 187 | return $cloned; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get localized action |
||
| 192 | * |
||
| 193 | * @return Localized |
||
| 194 | */ |
||
| 195 | public function getActionLocalized(): Localized |
||
| 196 | { |
||
| 197 | return $this->actionLocalized; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Add launch image |
||
| 202 | * |
||
| 203 | * @param string $launchImage |
||
| 204 | * |
||
| 205 | * @return Alert |
||
| 206 | */ |
||
| 207 | public function withLaunchImage(string $launchImage): Alert |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get launch image |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getLaunchImage(): string |
||
| 225 | } |
||
| 226 |