Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Client |
||
10 | { |
||
11 | /** |
||
12 | * The Slack incoming webhook endpoint. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $endpoint; |
||
17 | |||
18 | /** |
||
19 | * The default channel to send messages to. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $channel; |
||
24 | |||
25 | /** |
||
26 | * The default username to send messages as. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $username; |
||
31 | |||
32 | /** |
||
33 | * The default icon to send messages with. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $icon; |
||
38 | |||
39 | /** |
||
40 | * Whether to link names like @regan or leave |
||
41 | * them as plain text. |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $link_names = false; |
||
46 | |||
47 | /** |
||
48 | * Whether Slack should unfurl text-based URLs. |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $unfurl_links = false; |
||
53 | |||
54 | /** |
||
55 | * Whether Slack should unfurl media URLs. |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $unfurl_media = true; |
||
60 | |||
61 | /** |
||
62 | * Whether message text should be formatted with Slack's |
||
63 | * Markdown-like language. |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $allow_markdown = true; |
||
68 | |||
69 | /** |
||
70 | * The attachment fields which should be formatted with |
||
71 | * Slack's Markdown-like language. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $markdown_in_attachments = []; |
||
76 | |||
77 | /** |
||
78 | * The Guzzle HTTP client instance. |
||
79 | * |
||
80 | * @var \GuzzleHttp\Client |
||
81 | */ |
||
82 | protected $guzzle; |
||
83 | |||
84 | /** |
||
85 | * @var Team |
||
86 | */ |
||
87 | protected $defaultTeam; |
||
88 | |||
89 | /** |
||
90 | * Instantiate a new Client. |
||
91 | * |
||
92 | * @param Team[] $teams |
||
93 | * @param string $defaultTeamName |
||
94 | * @param array $attributes |
||
95 | * @param Guzzle $guzzle |
||
96 | * @throws Exception |
||
97 | */ |
||
98 | public function __construct(array $teams, $defaultTeamName, array $attributes = [], Guzzle $guzzle = null) |
||
132 | |||
133 | /** |
||
134 | * @return Team[] |
||
135 | */ |
||
136 | protected function getTeams() |
||
140 | |||
141 | /** |
||
142 | * Pass any unhandled methods through to a new Message |
||
143 | * instance. |
||
144 | * |
||
145 | * @param string $name The name of the method |
||
146 | * @param array $arguments The method arguments |
||
147 | * @return \Maknz\Slack\Message |
||
148 | */ |
||
149 | public function __call($name, $arguments) |
||
153 | |||
154 | /** |
||
155 | * Get the Slack endpoint. |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | public function getEndpoint() |
||
163 | |||
164 | /** |
||
165 | * Set the Slack endpoint. |
||
166 | * |
||
167 | * @param string $endpoint |
||
168 | * @return void |
||
169 | */ |
||
170 | public function setEndpoint($endpoint) |
||
174 | |||
175 | /** |
||
176 | * Get the default channel messages will be created for. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getDefaultChannel() |
||
184 | |||
185 | /** |
||
186 | * Set the default channel messages will be created for. |
||
187 | * |
||
188 | * @param string $channel |
||
189 | * @return void |
||
190 | */ |
||
191 | public function setDefaultChannel($channel) |
||
195 | |||
196 | /** |
||
197 | * Get the default username messages will be created for. |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getDefaultUsername() |
||
205 | |||
206 | /** |
||
207 | * Set the default username messages will be created for. |
||
208 | * |
||
209 | * @param string $username |
||
210 | * @return void |
||
211 | */ |
||
212 | public function setDefaultUsername($username) |
||
216 | |||
217 | /** |
||
218 | * Get the default icon messages will be created with. |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getDefaultIcon() |
||
226 | |||
227 | /** |
||
228 | * Set the default icon messages will be created with. |
||
229 | * |
||
230 | * @param string $icon |
||
231 | * @return void |
||
232 | */ |
||
233 | public function setDefaultIcon($icon) |
||
237 | |||
238 | /** |
||
239 | * Get whether messages sent will have names (like @regan) |
||
240 | * will be converted into links. |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function getLinkNames() |
||
248 | |||
249 | /** |
||
250 | * Set whether messages sent will have names (like @regan) |
||
251 | * will be converted into links. |
||
252 | * |
||
253 | * @param bool $value |
||
254 | * @return void |
||
255 | */ |
||
256 | public function setLinkNames($value) |
||
260 | |||
261 | /** |
||
262 | * Get whether text links should be unfurled. |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function getUnfurlLinks() |
||
270 | |||
271 | /** |
||
272 | * Set whether text links should be unfurled. |
||
273 | * |
||
274 | * @param bool $value |
||
275 | * @return void |
||
276 | */ |
||
277 | public function setUnfurlLinks($value) |
||
281 | |||
282 | /** |
||
283 | * Get whether media links should be unfurled. |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function getUnfurlMedia() |
||
291 | |||
292 | /** |
||
293 | * Set whether media links should be unfurled. |
||
294 | * |
||
295 | * @param bool $value |
||
296 | * @return void |
||
297 | */ |
||
298 | public function setUnfurlMedia($value) |
||
302 | |||
303 | /** |
||
304 | * Get whether message text should be formatted with |
||
305 | * Slack's Markdown-like language. |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function getAllowMarkdown() |
||
313 | |||
314 | /** |
||
315 | * Set whether message text should be formatted with |
||
316 | * Slack's Markdown-like language. |
||
317 | * |
||
318 | * @param bool $value |
||
319 | * @return void |
||
320 | */ |
||
321 | public function setAllowMarkdown($value) |
||
325 | |||
326 | /** |
||
327 | * Get the attachment fields which should be formatted |
||
328 | * in Slack's Markdown-like language. |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | public function getMarkdownInAttachments() |
||
336 | |||
337 | /** |
||
338 | * Set the attachment fields which should be formatted |
||
339 | * in Slack's Markdown-like language. |
||
340 | * |
||
341 | * @param array $fields |
||
342 | * @return void |
||
343 | */ |
||
344 | public function setMarkdownInAttachments(array $fields) |
||
348 | |||
349 | /** |
||
350 | * Create a new message with defaults. |
||
351 | * |
||
352 | * @return \Maknz\Slack\Message |
||
353 | */ |
||
354 | public function createMessage() |
||
370 | |||
371 | /** |
||
372 | * Send a message. |
||
373 | * |
||
374 | * @param \Maknz\Slack\Message $message |
||
375 | * @return void |
||
376 | */ |
||
377 | public function sendMessage(Message $message) |
||
389 | |||
390 | /** |
||
391 | * Prepares the payload to be sent to the webhook. |
||
392 | * |
||
393 | * @param \Maknz\Slack\Message $message The message to send |
||
394 | * @return array |
||
395 | */ |
||
396 | public function preparePayload(Message $message) |
||
416 | |||
417 | /** |
||
418 | * Get the attachments in array form. |
||
419 | * |
||
420 | * @param \Maknz\Slack\Message $message |
||
421 | * @return array |
||
422 | */ |
||
423 | protected function getAttachmentsAsArrays(Message $message) |
||
433 | |||
434 | /** |
||
435 | * @return Team |
||
436 | */ |
||
437 | protected function getDefaultTeam() |
||
441 | |||
442 | /** |
||
443 | * @param $teamName |
||
444 | * @return $this |
||
445 | * @throws Exception |
||
446 | */ |
||
447 | protected function setDefaultTeam($teamName) |
||
454 | |||
455 | /** |
||
456 | * @param string $teamName |
||
457 | * @return $this |
||
458 | * @throws Exception |
||
459 | */ |
||
460 | public function team($teamName) |
||
472 | |||
473 | /** |
||
474 | * @param $teamName |
||
475 | * @return Team |
||
476 | * @throws Exception |
||
477 | */ |
||
478 | private function getTeam($teamName) |
||
488 | |||
489 | } |
||
490 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: