Complex classes like Payload 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 Payload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Payload implements \JsonSerializable |
||
| 34 | { |
||
| 35 | const PAYLOAD_ROOT_KEY = 'aps'; |
||
| 36 | const PAYLOAD_ALERT_KEY = 'alert'; |
||
| 37 | const PAYLOAD_BADGE_KEY = 'badge'; |
||
| 38 | const PAYLOAD_SOUND_KEY = 'sound'; |
||
| 39 | const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available'; |
||
| 40 | const PAYLOAD_MUTABLE_CONTENT_KEY = 'mutable-content'; |
||
| 41 | const PAYLOAD_CATEGORY_KEY = 'category'; |
||
| 42 | const PAYLOAD_THREAD_ID_KEY = 'thread-id'; |
||
| 43 | const PAYLOAD_URL_ARGS_KEY = 'url-args'; |
||
| 44 | |||
| 45 | const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096; |
||
| 46 | const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120; |
||
| 47 | const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048; |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * The notification settings for your app on the user’s device determine whether an alert or banner is displayed. |
||
| 52 | * |
||
| 53 | * @var Alert|string |
||
| 54 | */ |
||
| 55 | private $alert; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The number to display as the badge of the app icon. |
||
| 59 | * If this property is absent, the badge is not changed. |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | private $badge; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container. |
||
| 67 | * |
||
| 68 | * @var Sound|string |
||
| 69 | */ |
||
| 70 | private $sound; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Include this key with a value of true to configure a silent notification. |
||
| 74 | * |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $contentAvailable; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Include this key with a value of true to configure a mutable content notification. |
||
| 81 | * |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $mutableContent; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Provide this key with a string value that represents the notification’s type. |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | private $category; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Provide this key with a string value that represents the app-specific identifier for grouping notifications. |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | private $threadId; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Provide this key with an array value that represents the url-args for Safari notifications. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private $urlArgs; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Payload custom values. |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private $customValues; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Push notification type |
||
| 116 | * |
||
| 117 | * https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns#2947607 |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | private $pushType; |
||
| 122 | |||
| 123 | protected function __construct() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return Payload |
||
| 129 | */ |
||
| 130 | public static function create(): Payload |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get Alert. |
||
| 137 | * |
||
| 138 | * @return Alert|string|array|null |
||
| 139 | */ |
||
| 140 | public function getAlert() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set Alert. |
||
| 147 | * |
||
| 148 | * @param Alert|string $alert|array $alert |
||
|
|
|||
| 149 | * |
||
| 150 | * @return Payload |
||
| 151 | */ |
||
| 152 | public function setAlert($alert): Payload |
||
| 153 | { |
||
| 154 | if ($alert instanceof Alert || is_string($alert) || is_array($alert)) { |
||
| 155 | $this->alert = $alert; |
||
| 156 | } |
||
| 157 | |||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get badge. |
||
| 163 | * |
||
| 164 | * @return int|null |
||
| 165 | */ |
||
| 166 | public function getBadge() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set badge. |
||
| 173 | * |
||
| 174 | * @param int $value |
||
| 175 | * |
||
| 176 | * @return Payload |
||
| 177 | */ |
||
| 178 | public function setBadge(int $value): Payload |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get sound. |
||
| 187 | * |
||
| 188 | * @return string|null |
||
| 189 | */ |
||
| 190 | public function getSound() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set sound. |
||
| 197 | * |
||
| 198 | * @param Sound|string $sound |
||
| 199 | * |
||
| 200 | * @return Payload |
||
| 201 | */ |
||
| 202 | public function setSound($sound): Payload |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set content availability. |
||
| 213 | * |
||
| 214 | * @param bool $value |
||
| 215 | * |
||
| 216 | * @return Payload |
||
| 217 | */ |
||
| 218 | public function setContentAvailability(bool $value): Payload |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get content availability. |
||
| 227 | * |
||
| 228 | * @return bool|null |
||
| 229 | */ |
||
| 230 | public function isContentAvailable() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set the mutable-content key for Notification Service Extensions on iOS10. |
||
| 237 | * |
||
| 238 | * @see http://bit.ly/mutable-content |
||
| 239 | * |
||
| 240 | * @param bool $value |
||
| 241 | * |
||
| 242 | * @return Payload |
||
| 243 | */ |
||
| 244 | public function setMutableContent(bool $value): Payload |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Is content mutable. |
||
| 253 | * |
||
| 254 | * @return bool|null |
||
| 255 | */ |
||
| 256 | public function hasMutableContent() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get category. |
||
| 263 | * |
||
| 264 | * @return string|null |
||
| 265 | */ |
||
| 266 | public function getCategory() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set category. |
||
| 273 | * |
||
| 274 | * @param string $value |
||
| 275 | * |
||
| 276 | * @return Payload |
||
| 277 | */ |
||
| 278 | public function setCategory(string $value): Payload |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get thread-id. |
||
| 287 | * |
||
| 288 | * @return string|null |
||
| 289 | */ |
||
| 290 | public function getThreadId() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set thread-id. |
||
| 297 | * |
||
| 298 | * @param string $value |
||
| 299 | * |
||
| 300 | * @return Payload |
||
| 301 | */ |
||
| 302 | public function setThreadId(string $value): Payload |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get url-args. |
||
| 311 | * |
||
| 312 | * @return array|null |
||
| 313 | */ |
||
| 314 | public function getUrlArgs() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Set url-args. |
||
| 321 | * |
||
| 322 | * @param array $value |
||
| 323 | * |
||
| 324 | * @return Payload |
||
| 325 | */ |
||
| 326 | public function setUrlArgs(array $value): Payload |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set custom value for Payload. |
||
| 335 | * |
||
| 336 | * @param string $key |
||
| 337 | * @param mixed $value |
||
| 338 | * |
||
| 339 | * @return Payload |
||
| 340 | * @throws InvalidPayloadException |
||
| 341 | */ |
||
| 342 | public function setCustomValue(string $key, $value): Payload |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Merges custom value for Payload. |
||
| 355 | * |
||
| 356 | * @param string $key |
||
| 357 | * @param mixed $value |
||
| 358 | * |
||
| 359 | * @return Payload |
||
| 360 | * @throws InvalidPayloadException |
||
| 361 | */ |
||
| 362 | public function addCustomValue(string $key, $value): Payload |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get custom value. |
||
| 371 | * |
||
| 372 | * @param $key |
||
| 373 | * |
||
| 374 | * @return mixed |
||
| 375 | * @throws InvalidPayloadException |
||
| 376 | */ |
||
| 377 | public function getCustomValue($key) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Set push type for Payload. |
||
| 388 | * |
||
| 389 | * @param string $pushType |
||
| 390 | * @return Payload |
||
| 391 | */ |
||
| 392 | public function setPushType($pushType) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get push type for Payload. |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getPushType() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Convert Payload to JSON. |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | public function toJson(): string |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Specify data which should be serialized to JSON. |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 426 | */ |
||
| 427 | public function jsonSerialize() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get default payload structure. |
||
| 481 | * |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | private static function getDefaultPayloadStructure() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param $jsonPayload |
||
| 491 | * @return void |
||
| 492 | * @throws InvalidPayloadException |
||
| 493 | */ |
||
| 494 | private function checkPayloadSize($jsonPayload) |
||
| 505 | } |
||
| 506 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.