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 | * List of tags processors to apply to every metric being sent out |
||
| 165 | * |
||
| 166 | * @var callable[] |
||
| 167 | */ |
||
| 168 | protected $tagProcessors = []; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Singleton Reference |
||
| 172 | * |
||
| 173 | * @param string $name Instance name |
||
| 174 | * |
||
| 175 | * @return Client Client instance |
||
| 176 | */ |
||
| 177 | 1 | public static function instance($name = 'default') |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Create a new instance |
||
| 187 | * |
||
| 188 | * @param string|null $instanceId |
||
| 189 | */ |
||
| 190 | 68 | public function __construct($instanceId = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Get string value of instance |
||
| 201 | * |
||
| 202 | * @return string String representation of this instance |
||
| 203 | */ |
||
| 204 | 2 | public function __toString() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Initialize Connection Details |
||
| 211 | * |
||
| 212 | * @param array $options Configuration options |
||
| 213 | * :host <string|ip> - host to talk to |
||
| 214 | * :port <int> - Port to communicate with |
||
| 215 | * :namespace <string> - Default namespace |
||
| 216 | * :timeout <float> - Timeout in seconds |
||
| 217 | * :onError <enum[error,exception,ignore]> - What we should do on error |
||
| 218 | * :dataDog <bool> - Use DataDog's version of statsd (Default: true) |
||
| 219 | * :tags <array> - List of tags to add to each message |
||
| 220 | * :tagProcessors <array> - List of tags processors to use |
||
| 221 | * |
||
| 222 | * @return Client This instance |
||
| 223 | * @throws ConfigurationException If port is invalid |
||
| 224 | */ |
||
| 225 | 68 | public function configure(array $options = []) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param callable $tagsProcessor |
||
| 273 | * |
||
| 274 | * @return Client |
||
| 275 | */ |
||
| 276 | 3 | public function addTagProcessor(callable $tagsProcessor) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Get Host |
||
| 284 | * |
||
| 285 | * @return string Host |
||
| 286 | */ |
||
| 287 | 1 | public function getHost() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Get Port |
||
| 294 | * |
||
| 295 | * @return int Port |
||
| 296 | */ |
||
| 297 | 3 | public function getPort() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Get Namespace |
||
| 304 | * |
||
| 305 | * @return string Namespace |
||
| 306 | */ |
||
| 307 | 1 | public function getNamespace() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Get Last Message |
||
| 314 | * |
||
| 315 | * @return string Last message sent to server |
||
| 316 | */ |
||
| 317 | 39 | public function getLastMessage() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Was the last write successful |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 2 | public function wasSuccessful() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Increment a metric |
||
| 334 | * |
||
| 335 | * @param string|string[] $metrics Metric(s) to increment |
||
| 336 | * @param int $delta Value to decrement the metric by |
||
| 337 | * @param float $sampleRate Sample rate of metric |
||
| 338 | * @param string[] $tags List of tags for this metric |
||
| 339 | * |
||
| 340 | * @return Client This instance |
||
| 341 | */ |
||
| 342 | 19 | public function increment($metrics, $delta = 1, $sampleRate = 1.0, array $tags = []) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Decrement a metric |
||
| 358 | * |
||
| 359 | * @param string|string[] $metrics Metric(s) to decrement |
||
| 360 | * @param int $delta Value to increment the metric by |
||
| 361 | * @param int $sampleRate Sample rate of metric |
||
| 362 | * @param string[] $tags List of tags for this metric |
||
| 363 | * |
||
| 364 | * @return Client This instance |
||
| 365 | */ |
||
| 366 | 2 | public function decrement($metrics, $delta = 1, $sampleRate = 1, array $tags = []) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Timing |
||
| 373 | * |
||
| 374 | * @param string $metric Metric to track |
||
| 375 | * @param float $time Time in milliseconds |
||
| 376 | * @param string[] $tags List of tags for this metric |
||
| 377 | * |
||
| 378 | * @return Client This instance |
||
| 379 | */ |
||
| 380 | 3 | public function timing($metric, $time, array $tags = []) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Time a function |
||
| 392 | * |
||
| 393 | * @param string $metric Metric to time |
||
| 394 | * @param callable $func Function to record |
||
| 395 | * @param string[] $tags List of tags for this metric |
||
| 396 | * |
||
| 397 | * @return Client This instance |
||
| 398 | */ |
||
| 399 | 1 | public function time($metric, callable $func, array $tags = []) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Gauges |
||
| 410 | * |
||
| 411 | * @param string $metric Metric to gauge |
||
| 412 | * @param int $value Set the value of the gauge |
||
| 413 | * @param string[] $tags List of tags for this metric |
||
| 414 | * |
||
| 415 | * @return Client This instance |
||
| 416 | */ |
||
| 417 | 2 | public function gauge($metric, $value, array $tags = []) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Histogram |
||
| 429 | * |
||
| 430 | * @param string $metric Metric to send |
||
| 431 | * @param float $value Value to send |
||
| 432 | * @param float $sampleRate Sample rate of metric |
||
| 433 | * @param string[] $tags List of tags for this metric |
||
| 434 | * |
||
| 435 | * @return Client This instance |
||
| 436 | */ |
||
| 437 | 4 | public function histogram($metric, $value, $sampleRate = 1.0, array $tags = []) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Sets - count the number of unique elements for a group |
||
| 450 | * |
||
| 451 | * @param string $metric |
||
| 452 | * @param int $value |
||
| 453 | * @param string[] $tags List of tags for this metric |
||
| 454 | * |
||
| 455 | * @return Client This instance |
||
| 456 | */ |
||
| 457 | 2 | public function set($metric, $value, array $tags = []) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Send a event notification |
||
| 469 | * |
||
| 470 | * @link http://docs.datadoghq.com/guides/dogstatsd/#events |
||
| 471 | * |
||
| 472 | * @param string $title Event Title |
||
| 473 | * @param string $text Event Text |
||
| 474 | * @param array $metadata Set of metadata for this event: |
||
| 475 | * - time - Assign a timestamp to the event. |
||
| 476 | * - hostname - Assign a hostname to the event |
||
| 477 | * - key - Assign an aggregation key to th event, to group it with some others |
||
| 478 | * - priority - Can be 'normal' or 'low' |
||
| 479 | * - source - Assign a source type to the event |
||
| 480 | * - alert - Can be 'error', 'warning', 'info' or 'success' |
||
| 481 | * @param string[] $tags List of tags for this event |
||
| 482 | * |
||
| 483 | * @return Client This instance |
||
| 484 | * @throws ConnectionException If there is a connection problem with the host |
||
| 485 | */ |
||
| 486 | 7 | public function event($title, $text, array $metadata = [], array $tags = []) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Service Checks |
||
| 512 | * |
||
| 513 | * @link http://docs.datadoghq.com/guides/dogstatsd/#service-checks |
||
| 514 | * |
||
| 515 | * @param string $name Name of the service |
||
| 516 | * @param int $status digit corresponding to the status you’re reporting (OK = 0, WARNING = 1, CRITICAL = 2, |
||
| 517 | * UNKNOWN = 3) |
||
| 518 | * @param array $metadata - time - Assign a timestamp to the service check |
||
| 519 | * - hostname - Assign a hostname to the service check |
||
| 520 | * @param string[] $tags List of tags for this event |
||
| 521 | * |
||
| 522 | * @return Client This instance |
||
| 523 | * @throws ConnectionException If there is a connection problem with the host |
||
| 524 | */ |
||
| 525 | 6 | public function serviceCheck($name, $status, array $metadata = [], array $tags = []) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * @param float $rate |
||
| 553 | * @param string $postfix |
||
| 554 | * |
||
| 555 | * @return bool |
||
| 556 | */ |
||
| 557 | 23 | private function isSampled($rate = 1.0, &$postfix = '') |
|
| 568 | |||
| 569 | /** |
||
| 570 | * @param string[] $tags A list of tags to apply to each message |
||
| 571 | * |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | 39 | private function formatTags(array $tags = []) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Send Data to StatsD Server |
||
| 594 | * |
||
| 595 | * @param string[] $data A list of messages to send to the server |
||
| 596 | * @param string[] $tags A list of tags to apply to each message |
||
| 597 | * |
||
| 598 | * @return Client This instance |
||
| 599 | * @throws ConnectionException If there is a connection problem with the host |
||
| 600 | */ |
||
| 601 | 28 | protected function send(array $data, array $tags = []) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * @param string[] $messages |
||
| 614 | * |
||
| 615 | * @return Client This instance |
||
| 616 | * @throws ConnectionException If there is a connection problem with the host |
||
| 617 | */ |
||
| 618 | 39 | protected function sendMessages(array $messages) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Process a set of tags with some user defined processes to add custom runtime data |
||
| 631 | * |
||
| 632 | * @param array $tags |
||
| 633 | * |
||
| 634 | * @return array|mixed |
||
| 635 | */ |
||
| 636 | 28 | private function processTags(array $tags) |
|
| 643 | } |
||
| 644 |