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 |
||
32 | class Payload implements \JsonSerializable |
||
33 | { |
||
34 | const PAYLOAD_ROOT_KEY = 'aps'; |
||
35 | const PAYLOAD_ALERT_KEY = 'alert'; |
||
36 | const PAYLOAD_BADGE_KEY = 'badge'; |
||
37 | const PAYLOAD_SOUND_KEY = 'sound'; |
||
38 | const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available'; |
||
39 | const PAYLOAD_MUTABLE_CONTENT_KEY = 'mutable-content'; |
||
40 | const PAYLOAD_CATEGORY_KEY = 'category'; |
||
41 | const PAYLOAD_THREAD_ID_KEY = 'thread-id'; |
||
42 | const PAYLOAD_URL_ARGS_KEY = 'url-args'; |
||
43 | |||
44 | const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096; |
||
45 | const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120; |
||
46 | const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048; |
||
47 | |||
48 | /** |
||
49 | * The notification settings for your app on the user’s device determine whether an alert or banner is displayed. |
||
50 | * |
||
51 | * @var Alert|string |
||
52 | */ |
||
53 | private $alert; |
||
54 | |||
55 | /** |
||
56 | * The number to display as the badge of the app icon. |
||
57 | * If this property is absent, the badge is not changed. |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | private $badge; |
||
62 | |||
63 | /** |
||
64 | * The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $sound; |
||
69 | |||
70 | /** |
||
71 | * Include this key with a value of true to configure a silent notification. |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $contentAvailable; |
||
76 | |||
77 | /** |
||
78 | * Include this key with a value of true to configure a mutable content notification. |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | private $mutableContent; |
||
83 | |||
84 | /** |
||
85 | * Provide this key with a string value that represents the notification’s type. |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | private $category; |
||
90 | |||
91 | /** |
||
92 | * Provide this key with a string value that represents the app-specific identifier for grouping notifications. |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | private $threadId; |
||
97 | |||
98 | /** |
||
99 | * Provide this key with an array value that represents the url-args for Safari notifications. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | private $urlArgs; |
||
104 | |||
105 | /** |
||
106 | * Payload custom values. |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | private $customValues; |
||
111 | |||
112 | /** |
||
113 | * Push notification type |
||
114 | * |
||
115 | * https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns#2947607 |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | private $pushType; |
||
120 | |||
121 | protected function __construct() |
||
124 | |||
125 | /** |
||
126 | * @return Payload |
||
127 | */ |
||
128 | public static function create(): Payload |
||
132 | |||
133 | /** |
||
134 | * Get Alert. |
||
135 | * |
||
136 | * @return Alert|null |
||
137 | */ |
||
138 | public function getAlert() |
||
142 | |||
143 | /** |
||
144 | * Set Alert. |
||
145 | * |
||
146 | * @param Alert|string $alert |
||
147 | * |
||
148 | * @return Payload |
||
149 | */ |
||
150 | public function setAlert($alert): Payload |
||
158 | |||
159 | /** |
||
160 | * Get badge. |
||
161 | * |
||
162 | * @return int|null |
||
163 | */ |
||
164 | public function getBadge() |
||
168 | |||
169 | /** |
||
170 | * Set badge. |
||
171 | * |
||
172 | * @param int $value |
||
173 | * |
||
174 | * @return Payload |
||
175 | */ |
||
176 | public function setBadge(int $value): Payload |
||
182 | |||
183 | /** |
||
184 | * Get sound. |
||
185 | * |
||
186 | * @return string|null |
||
187 | */ |
||
188 | public function getSound() |
||
192 | |||
193 | /** |
||
194 | * Set sound. |
||
195 | * |
||
196 | * @param string $value |
||
197 | * |
||
198 | * @return Payload |
||
199 | */ |
||
200 | public function setSound(string $value): Payload |
||
206 | |||
207 | /** |
||
208 | * Set content availability. |
||
209 | * |
||
210 | * @param bool $value |
||
211 | * |
||
212 | * @return Payload |
||
213 | */ |
||
214 | public function setContentAvailability(bool $value): Payload |
||
220 | |||
221 | /** |
||
222 | * Get content availability. |
||
223 | * |
||
224 | * @return bool|null |
||
225 | */ |
||
226 | public function isContentAvailable() |
||
230 | |||
231 | /** |
||
232 | * Set the mutable-content key for Notification Service Extensions on iOS10. |
||
233 | * |
||
234 | * @see http://bit.ly/mutable-content |
||
235 | * |
||
236 | * @param bool $value |
||
237 | * |
||
238 | * @return Payload |
||
239 | */ |
||
240 | public function setMutableContent(bool $value): Payload |
||
246 | |||
247 | /** |
||
248 | * Is content mutable. |
||
249 | * |
||
250 | * @return bool|null |
||
251 | */ |
||
252 | public function hasMutableContent() |
||
256 | |||
257 | /** |
||
258 | * Get category. |
||
259 | * |
||
260 | * @return string|null |
||
261 | */ |
||
262 | public function getCategory() |
||
266 | |||
267 | /** |
||
268 | * Set category. |
||
269 | * |
||
270 | * @param string $value |
||
271 | * |
||
272 | * @return Payload |
||
273 | */ |
||
274 | public function setCategory(string $value): Payload |
||
280 | |||
281 | /** |
||
282 | * Get thread-id. |
||
283 | * |
||
284 | * @return string|null |
||
285 | */ |
||
286 | public function getThreadId() |
||
290 | |||
291 | /** |
||
292 | * Set thread-id. |
||
293 | * |
||
294 | * @param string $value |
||
295 | * |
||
296 | * @return Payload |
||
297 | */ |
||
298 | public function setThreadId(string $value): Payload |
||
304 | |||
305 | /** |
||
306 | * Get url-args. |
||
307 | * |
||
308 | * @return array|null |
||
309 | */ |
||
310 | public function getUrlArgs() |
||
314 | |||
315 | /** |
||
316 | * Set url-args. |
||
317 | * |
||
318 | * @param array $value |
||
319 | * |
||
320 | * @return Payload |
||
321 | */ |
||
322 | public function setUrlArgs(array $value): Payload |
||
328 | |||
329 | /** |
||
330 | * Set custom value for Payload. |
||
331 | * |
||
332 | * @param string $key |
||
333 | * @param mixed $value |
||
334 | * |
||
335 | * @return Payload |
||
336 | * @throws InvalidPayloadException |
||
337 | */ |
||
338 | public function setCustomValue(string $key, $value): Payload |
||
348 | |||
349 | /** |
||
350 | * Get custom value. |
||
351 | * |
||
352 | * @param $key |
||
353 | * |
||
354 | * @return mixed |
||
355 | * @throws InvalidPayloadException |
||
356 | */ |
||
357 | public function getCustomValue($key) |
||
365 | |||
366 | /** |
||
367 | * Set push type for Payload. |
||
368 | * |
||
369 | * @param string $pushType |
||
370 | * @return Payload |
||
371 | */ |
||
372 | public function setPushType($pushType) { |
||
377 | |||
378 | /** |
||
379 | * Get push type for Payload. |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | public function getPushType() { |
||
386 | |||
387 | /** |
||
388 | * Convert Payload to JSON. |
||
389 | * |
||
390 | * @return string |
||
391 | */ |
||
392 | public function toJson(): string |
||
396 | |||
397 | /** |
||
398 | * Specify data which should be serialized to JSON. |
||
399 | * |
||
400 | * @return array |
||
401 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
402 | */ |
||
403 | public function jsonSerialize() |
||
445 | |||
446 | /** |
||
447 | * Get default payload structure. |
||
448 | * |
||
449 | * @return array |
||
450 | */ |
||
451 | private static function getDefaultPayloadStructure() |
||
455 | } |
||
456 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..