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 |
||
| 24 | class Client |
||
| 25 | { |
||
| 26 | const STATUS_OK = 0; |
||
| 27 | const STATUS_WARNING = 1; |
||
| 28 | const STATUS_CRITICAL = 2; |
||
| 29 | const STATUS_UNKNOWN = 3; |
||
| 30 | |||
| 31 | const PRIORITY_LOW = 'low'; |
||
| 32 | const PRIORITY_NORMAL = 'normal'; |
||
| 33 | |||
| 34 | const ALERT_ERROR = 'error'; |
||
| 35 | const ALERT_WARNING = 'warning'; |
||
| 36 | const ALERT_INFO = 'info'; |
||
| 37 | const ALERT_SUCCESS = 'success'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Instance instances array |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected static $instances = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Instance ID |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $instanceId; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Server Host |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $host = '127.0.0.1'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Server Port |
||
| 62 | * |
||
| 63 | * @var integer |
||
| 64 | */ |
||
| 65 | protected $port = 8125; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Last message sent to the server |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $message = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Was the last message sucessfully sent |
||
| 76 | * |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $written; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Class namespace |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $namespace = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Timeout for creating the socket connection |
||
| 90 | * |
||
| 91 | * @var null|float |
||
| 92 | */ |
||
| 93 | protected $timeout; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * What we should do on connection failure |
||
| 97 | * |
||
| 98 | * Options: |
||
| 99 | * - error |
||
| 100 | * - exception |
||
| 101 | * - ignore |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $onError = 'exception'; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Socket connection |
||
| 109 | * |
||
| 110 | * @var WriterInterface |
||
| 111 | */ |
||
| 112 | protected $stream; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Metadata for the DataDog event message |
||
| 116 | * |
||
| 117 | * @var array - time - Assign a timestamp to the event. |
||
| 118 | * - hostname - Assign a hostname to the event |
||
| 119 | * - key - Assign an aggregation key to the event, to group it with some others |
||
| 120 | * - priority - Can be 'normal' or 'low' |
||
| 121 | * - source - Assign a source type to the event |
||
| 122 | * - alert - Can be 'error', 'warning', 'info' or 'success' |
||
| 123 | */ |
||
| 124 | protected $eventMetaData = [ |
||
| 125 | 'time' => 'd', |
||
| 126 | 'hostname' => 'h', |
||
| 127 | 'key' => 'k', |
||
| 128 | 'priority' => 'p', |
||
| 129 | 'source' => 's', |
||
| 130 | 'alert' => 't', |
||
| 131 | ]; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var array - time - Assign a timestamp to the service check |
||
| 135 | * - hostname - Assign a hostname to the service check |
||
| 136 | */ |
||
| 137 | protected $serviceCheckMetaData = [ |
||
| 138 | 'time' => 'd', |
||
| 139 | 'hostname' => 'h', |
||
| 140 | ]; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var array - message - Assign a message to the service check |
||
| 144 | */ |
||
| 145 | protected $serviceCheckMessage = [ |
||
| 146 | 'message' => 'm', |
||
| 147 | ]; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Is the server type DataDog implementation |
||
| 151 | * |
||
| 152 | * @var bool |
||
| 153 | */ |
||
| 154 | protected $dataDog = true; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Set of default tags to send to every request |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | protected $tags = []; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Singleton Reference |
||
| 165 | * |
||
| 166 | * @param string $name Instance name |
||
| 167 | * |
||
| 168 | * @return Client Client instance |
||
| 169 | */ |
||
| 170 | 1 | public static function instance($name = 'default') |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Create a new instance |
||
| 180 | * |
||
| 181 | * @param string|null $instanceId |
||
| 182 | */ |
||
| 183 | 61 | public function __construct($instanceId = null) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Get string value of instance |
||
| 194 | * |
||
| 195 | * @return string String representation of this instance |
||
| 196 | */ |
||
| 197 | 2 | public function __toString() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Initialize Connection Details |
||
| 204 | * |
||
| 205 | * @param array $options Configuration options |
||
| 206 | * :host <string|ip> - host to talk to |
||
| 207 | * :port <int> - Port to communicate with |
||
| 208 | * :namespace <string> - Default namespace |
||
| 209 | * :timeout <float> - Timeout in seconds |
||
| 210 | * :onError <enum[error,exception,ignore]> - What we should do on error |
||
| 211 | * :dataDog <bool> - Use DataDog's version of statsd (Default: true) |
||
| 212 | * :tags <array> - List of tags to add to each message |
||
| 213 | * |
||
| 214 | * @return Client This instance |
||
| 215 | * @throws ConfigurationException If port is invalid |
||
| 216 | */ |
||
| 217 | 61 | public function configure(array $options = []) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get Host |
||
| 260 | * |
||
| 261 | * @return string Host |
||
| 262 | */ |
||
| 263 | 1 | public function getHost() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Get Port |
||
| 270 | * |
||
| 271 | * @return int Port |
||
| 272 | */ |
||
| 273 | 2 | public function getPort() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Get Namespace |
||
| 280 | * |
||
| 281 | * @return string Namespace |
||
| 282 | */ |
||
| 283 | 1 | public function getNamespace() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Get Last Message |
||
| 290 | * |
||
| 291 | * @return string Last message sent to server |
||
| 292 | */ |
||
| 293 | 36 | public function getLastMessage() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Was the last write successful |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | 1 | public function wasSuccessful() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Increment a metric |
||
| 310 | * |
||
| 311 | * @param string|string[] $metrics Metric(s) to increment |
||
| 312 | * @param int $delta Value to decrement the metric by |
||
| 313 | * @param float $sampleRate Sample rate of metric |
||
| 314 | * @param string[] $tags List of tags for this metric |
||
| 315 | * |
||
| 316 | * @return Client This instance |
||
| 317 | */ |
||
| 318 | 17 | public function increment($metrics, $delta = 1, $sampleRate = 1.0, array $tags = []) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Decrement a metric |
||
| 334 | * |
||
| 335 | * @param string|string[] $metrics Metric(s) to decrement |
||
| 336 | * @param int $delta Value to increment the metric by |
||
| 337 | * @param int $sampleRate Sample rate of metric |
||
| 338 | * @param string[] $tags List of tags for this metric |
||
| 339 | * |
||
| 340 | * @return Client This instance |
||
| 341 | */ |
||
| 342 | 2 | public function decrement($metrics, $delta = 1, $sampleRate = 1, array $tags = []) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Timing |
||
| 349 | * |
||
| 350 | * @param string $metric Metric to track |
||
| 351 | * @param float $time Time in milliseconds |
||
| 352 | * @param string[] $tags List of tags for this metric |
||
| 353 | * |
||
| 354 | * @return Client This instance |
||
| 355 | */ |
||
| 356 | 3 | public function timing($metric, $time, array $tags = []) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Time a function |
||
| 368 | * |
||
| 369 | * @param string $metric Metric to time |
||
| 370 | * @param callable $func Function to record |
||
| 371 | * @param string[] $tags List of tags for this metric |
||
| 372 | * |
||
| 373 | * @return Client This instance |
||
| 374 | */ |
||
| 375 | 1 | public function time($metric, callable $func, array $tags = []) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Gauges |
||
| 386 | * |
||
| 387 | * @param string $metric Metric to gauge |
||
| 388 | * @param int $value Set the value of the gauge |
||
| 389 | * @param string[] $tags List of tags for this metric |
||
| 390 | * |
||
| 391 | * @return Client This instance |
||
| 392 | */ |
||
| 393 | 2 | public function gauge($metric, $value, array $tags = []) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Histogram |
||
| 405 | * |
||
| 406 | * @param string $metric Metric to send |
||
| 407 | * @param float $value Value to send |
||
| 408 | * @param float $sampleRate Sample rate of metric |
||
| 409 | * @param string[] $tags List of tags for this metric |
||
| 410 | * |
||
| 411 | * @return Client This instance |
||
| 412 | */ |
||
| 413 | 4 | public function histogram($metric, $value, $sampleRate = 1.0, array $tags = []) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Sets - count the number of unique elements for a group |
||
| 426 | * |
||
| 427 | * @param string $metric |
||
| 428 | * @param int $value |
||
| 429 | * @param string[] $tags List of tags for this metric |
||
| 430 | * |
||
| 431 | * @return Client This instance |
||
| 432 | */ |
||
| 433 | 2 | public function set($metric, $value, array $tags = []) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Send a event notification |
||
| 445 | * |
||
| 446 | * @link http://docs.datadoghq.com/guides/dogstatsd/#events |
||
| 447 | * |
||
| 448 | * @param string $title Event Title |
||
| 449 | * @param string $text Event Text |
||
| 450 | * @param array $metadata Set of metadata for this event: |
||
| 451 | * - time - Assign a timestamp to the event. |
||
| 452 | * - hostname - Assign a hostname to the event |
||
| 453 | * - key - Assign an aggregation key to th event, to group it with some others |
||
| 454 | * - priority - Can be 'normal' or 'low' |
||
| 455 | * - source - Assign a source type to the event |
||
| 456 | * - alert - Can be 'error', 'warning', 'info' or 'success' |
||
| 457 | * @param string[] $tags List of tags for this event |
||
| 458 | * |
||
| 459 | * @return Client This instance |
||
| 460 | * @throws ConnectionException If there is a connection problem with the host |
||
| 461 | */ |
||
| 462 | 6 | public function event($title, $text, array $metadata = [], array $tags = []) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Service Checks |
||
| 488 | * |
||
| 489 | * @link http://docs.datadoghq.com/guides/dogstatsd/#service-checks |
||
| 490 | * |
||
| 491 | * @param string $name Name of the service |
||
| 492 | * @param int $status digit corresponding to the status you’re reporting (OK = 0, WARNING = 1, CRITICAL = 2, |
||
| 493 | * UNKNOWN = 3) |
||
| 494 | * @param array $metadata - time - Assign a timestamp to the service check |
||
| 495 | * - hostname - Assign a hostname to the service check |
||
| 496 | * @param string[] $tags List of tags for this event |
||
| 497 | * |
||
| 498 | * @return Client This instance |
||
| 499 | * @throws ConnectionException If there is a connection problem with the host |
||
| 500 | */ |
||
| 501 | 6 | public function serviceCheck($name, $status, array $metadata = [], array $tags = []) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * @param float $rate |
||
| 529 | * @param string $postfix |
||
| 530 | * |
||
| 531 | * @return bool |
||
| 532 | */ |
||
| 533 | 21 | private function isSampled($rate = 1.0, &$postfix = '') |
|
| 544 | |||
| 545 | /** |
||
| 546 | * @param string[] $tags A list of tags to apply to each message |
||
| 547 | * |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | 36 | private function formatTags(array $tags = []) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Send Data to StatsD Server |
||
| 570 | * |
||
| 571 | * @param string[] $data A list of messages to send to the server |
||
| 572 | * @param string[] $tags A list of tags to apply to each message |
||
| 573 | * |
||
| 574 | * @return Client This instance |
||
| 575 | * @throws ConnectionException If there is a connection problem with the host |
||
| 576 | */ |
||
| 577 | 26 | protected function send(array $data, array $tags = []) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * @param string[] $messages |
||
| 590 | * |
||
| 591 | * @return Client This instance |
||
| 592 | * @throws ConnectionException If there is a connection problem with the host |
||
| 593 | */ |
||
| 594 | 36 | protected function sendMessages(array $messages) |
|
| 604 | } |
||
| 605 |