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 |
||
| 8 | class Client |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The Slack incoming webhook endpoint. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $endpoint; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The default channel to send messages to. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $channel; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * If set to true, all messages will be sent to the default channel, |
||
| 26 | * even if you specify another one during the runtime. |
||
| 27 | * |
||
| 28 | * This is useful for dev environment. |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $sticky_channel = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The default username to send messages as. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $username; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The default icon to send messages with. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $icon; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Whether to link names like @regan or leave |
||
| 50 | * them as plain text. |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $link_names = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Whether Slack should unfurl text-based URLs. |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $unfurl_links = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Whether Slack should unfurl media URLs. |
||
| 65 | * |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | protected $unfurl_media = true; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Whether message text should be formatted with Slack's |
||
| 72 | * Markdown-like language. |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $allow_markdown = true; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The attachment fields which should be formatted with |
||
| 80 | * Slack's Markdown-like language. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $markdown_in_attachments = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The Guzzle HTTP client instance. |
||
| 88 | * |
||
| 89 | * @var \GuzzleHttp\Client |
||
| 90 | */ |
||
| 91 | protected $guzzle; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Instantiate a new Client. |
||
| 95 | * |
||
| 96 | * @param string $endpoint |
||
| 97 | * @param array $attributes |
||
| 98 | * @return void |
||
|
|
|||
| 99 | */ |
||
| 100 | public function __construct($endpoint, array $attributes = [], Guzzle $guzzle = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Pass any unhandled methods through to a new Message |
||
| 145 | * instance. |
||
| 146 | * |
||
| 147 | * @param string $name The name of the method |
||
| 148 | * @param array $arguments The method arguments |
||
| 149 | * @return \Maknz\Slack\Message |
||
| 150 | */ |
||
| 151 | public function __call($name, $arguments) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the Slack endpoint. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getEndpoint() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Set the Slack endpoint. |
||
| 168 | * |
||
| 169 | * @param string $endpoint |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function setEndpoint($endpoint) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get the default channel messages will be created for. |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getDefaultChannel() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set the default channel messages will be created for. |
||
| 189 | * |
||
| 190 | * @param string $channel |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function setDefaultChannel($channel) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function isStickyChannel() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param bool $sticky_channel |
||
| 208 | */ |
||
| 209 | public function setStickyChannel($sticky_channel) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the default username messages will be created for. |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getDefaultUsername() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Set the default username messages will be created for. |
||
| 226 | * |
||
| 227 | * @param string $username |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | public function setDefaultUsername($username) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the default icon messages will be created with. |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getDefaultIcon() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set the default icon messages will be created with. |
||
| 247 | * |
||
| 248 | * @param string $icon |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function setDefaultIcon($icon) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get whether messages sent will have names (like @regan) |
||
| 258 | * will be converted into links. |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function getLinkNames() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Set whether messages sent will have names (like @regan) |
||
| 269 | * will be converted into links. |
||
| 270 | * |
||
| 271 | * @param bool $value |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | public function setLinkNames($value) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get whether text links should be unfurled. |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function getUnfurlLinks() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Set whether text links should be unfurled. |
||
| 291 | * |
||
| 292 | * @param bool $value |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function setUnfurlLinks($value) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get whether media links should be unfurled. |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function getUnfurlMedia() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set whether media links should be unfurled. |
||
| 312 | * |
||
| 313 | * @param bool $value |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | public function setUnfurlMedia($value) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get whether message text should be formatted with |
||
| 323 | * Slack's Markdown-like language. |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function getAllowMarkdown() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set whether message text should be formatted with |
||
| 334 | * Slack's Markdown-like language. |
||
| 335 | * |
||
| 336 | * @param bool $value |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | public function setAllowMarkdown($value) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the attachment fields which should be formatted |
||
| 346 | * in Slack's Markdown-like language. |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getMarkdownInAttachments() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Set the attachment fields which should be formatted |
||
| 357 | * in Slack's Markdown-like language. |
||
| 358 | * |
||
| 359 | * @param array $fields |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function setMarkdownInAttachments(array $fields) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Create a new message with defaults. |
||
| 369 | * |
||
| 370 | * @return \Maknz\Slack\Message |
||
| 371 | */ |
||
| 372 | public function createMessage() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Send a message. |
||
| 391 | * |
||
| 392 | * @param \Maknz\Slack\Message $message |
||
| 393 | * @return void |
||
| 394 | */ |
||
| 395 | public function sendMessage(Message $message) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Prepares the payload to be sent to the webhook. |
||
| 410 | * |
||
| 411 | * @param \Maknz\Slack\Message $message The message to send |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public function preparePayload(Message $message) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get the attachments in array form. |
||
| 437 | * |
||
| 438 | * @param \Maknz\Slack\Message $message |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | protected function getAttachmentsAsArrays(Message $message) |
||
| 451 | } |
||
| 452 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.