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 ApnsMessage 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 ApnsMessage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ApnsMessage extends Message |
||
| 23 | { |
||
| 24 | // Default values |
||
| 25 | const DEFAULT_SOUND = 'default'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $simpleAlert; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $alertTitle; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $alertBody; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $alertTitleLocKey; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $alertTitleLocArgs; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $alertActionLocKey; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $alertLocKey; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $alertLocArgs; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $alertLaunchImage; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $badge; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $sound; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | private $contentAvailable; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $category; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | private $data; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Constructor |
||
| 99 | */ |
||
| 100 | 41 | public function __construct() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Get full message payload. |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | 5 | public function getPayload() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get the value of Simple Alert. |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 6 | public function getSimpleAlert() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Set the value of Simple Alert. |
||
| 160 | * |
||
| 161 | * @param string $simpleAlert |
||
| 162 | * |
||
| 163 | * @return self |
||
| 164 | */ |
||
| 165 | 3 | public function setSimpleAlert($simpleAlert) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Get the value of Alert Title. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | 5 | public function getAlertTitle() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Set the value of Alert Title. |
||
| 188 | * |
||
| 189 | * @param string $alertTitle |
||
| 190 | * |
||
| 191 | * @return self |
||
| 192 | */ |
||
| 193 | 2 | public function setAlertTitle($alertTitle) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get the value of Alert Body. |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | 5 | public function getAlertBody() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Set the value of Alert Body. |
||
| 212 | * |
||
| 213 | * @param string $alertBody |
||
| 214 | * |
||
| 215 | * @return self |
||
| 216 | */ |
||
| 217 | 2 | public function setAlertBody($alertBody) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Get the value of Alert Title Loc Key. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | 5 | public function getAlertTitleLocKey() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Set the value of Alert Title Loc Key. |
||
| 236 | * |
||
| 237 | * @param string $alertTitleLocKey |
||
| 238 | * |
||
| 239 | * @return self |
||
| 240 | */ |
||
| 241 | 2 | public function setAlertTitleLocKey($alertTitleLocKey) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Get the value of Alert Title Loc Args. |
||
| 250 | * |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | 5 | public function getAlertTitleLocArgs() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set the value of Alert Title Loc Args. |
||
| 260 | * |
||
| 261 | * @param array $alertTitleLocArgs |
||
| 262 | * |
||
| 263 | * @return self |
||
| 264 | */ |
||
| 265 | 2 | public function setAlertTitleLocArgs(array $alertTitleLocArgs) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Get the value of Alert Action Loc Key. |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | 5 | public function getAlertActionLocKey() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Set the value of Alert Action Loc Key. |
||
| 284 | * |
||
| 285 | * @param string $alertActionLocKey |
||
| 286 | * |
||
| 287 | * @return self |
||
| 288 | */ |
||
| 289 | 2 | public function setAlertActionLocKey($alertActionLocKey) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get the value of Alert Loc Key. |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | 5 | public function getAlertLocKey() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Set the value of Alert Loc Key. |
||
| 308 | * |
||
| 309 | * @param string $alertLocKey |
||
| 310 | * |
||
| 311 | * @return self |
||
| 312 | */ |
||
| 313 | 2 | public function setAlertLocKey($alertLocKey) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get the value of Alert Loc Args. |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | 5 | public function getAlertLocArgs() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set the value of Alert Loc Args. |
||
| 332 | * |
||
| 333 | * @param array $alertLocArgs |
||
| 334 | * |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | 2 | public function setAlertLocArgs(array $alertLocArgs) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get the value of Alert Launch Image. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | 5 | public function getAlertLaunchImage() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Set the value of Alert Launch Image. |
||
| 356 | * |
||
| 357 | * @param string $alertLaunchImage |
||
| 358 | * |
||
| 359 | * @return self |
||
| 360 | */ |
||
| 361 | 2 | public function setAlertLaunchImage($alertLaunchImage) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Get the value of Badge. |
||
| 370 | * |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | 6 | public function getBadge() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Set the value of Badge. |
||
| 380 | * |
||
| 381 | * @param int $badge |
||
| 382 | * |
||
| 383 | * @return self |
||
| 384 | */ |
||
| 385 | 2 | public function setBadge($badge) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Get the value of Sound. |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | 7 | public function getSound() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Set the value of Sound. |
||
| 404 | * |
||
| 405 | * @param string $sound |
||
| 406 | * |
||
| 407 | * @return self |
||
| 408 | */ |
||
| 409 | 2 | public function setSound($sound) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get the value of Content Available. |
||
| 418 | * |
||
| 419 | * @return int |
||
| 420 | */ |
||
| 421 | 6 | public function getContentAvailable() |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Set the value of Content Available. |
||
| 428 | * |
||
| 429 | * @param int $contentAvailable |
||
| 430 | * |
||
| 431 | * @return self |
||
| 432 | */ |
||
| 433 | 2 | public function setContentAvailable($contentAvailable) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Get the value of Category. |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | 6 | public function getCategory() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Set the value of Category. |
||
| 452 | * |
||
| 453 | * @param string $category |
||
| 454 | * |
||
| 455 | * @return self |
||
| 456 | */ |
||
| 457 | 2 | public function setCategory($category) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Get the data array. |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | 7 | public function getData() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Set the data array. |
||
| 476 | * |
||
| 477 | * @param array $data |
||
| 478 | * |
||
| 479 | * @return self |
||
| 480 | */ |
||
| 481 | 3 | public function setData(array $data) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Set a key/value pair in the data array. |
||
| 490 | * |
||
| 491 | * @param string|int $key |
||
| 492 | * @param mixed $value |
||
| 493 | * |
||
| 494 | * @return self |
||
| 495 | */ |
||
| 496 | 2 | View Code Duplication | public function addData($key, $value) |
| 508 | |||
| 509 | /** |
||
| 510 | * Get the value of the payload "alert" key. |
||
| 511 | * |
||
| 512 | * @return string|array |
||
| 513 | */ |
||
| 514 | 5 | private function getPayloadAlertKeyValue() |
|
| 551 | } |
||
| 552 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.