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 |
||
| 22 | class Client |
||
| 23 | { |
||
| 24 | const STATUS_OK = 0; |
||
| 25 | const STATUS_WARNING = 1; |
||
| 26 | const STATUS_CRITICAL = 2; |
||
| 27 | const STATUS_UNKNOWN = 3; |
||
| 28 | |||
| 29 | const PRIORITY_LOW = 'low'; |
||
| 30 | const PRIORITY_NORMAL = 'normal'; |
||
| 31 | |||
| 32 | const ALERT_ERROR = 'error'; |
||
| 33 | const ALERT_WARNING = 'warning'; |
||
| 34 | const ALERT_INFO = 'info'; |
||
| 35 | const ALERT_SUCCESS = 'success'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Instance instances array |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected static $instances = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Instance ID |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $instanceId; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Server Host |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $host = '127.0.0.1'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Server Port |
||
| 60 | * |
||
| 61 | * @var integer |
||
| 62 | */ |
||
| 63 | protected $port = 8125; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Last message sent to the server |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $message = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Class namespace |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $namespace = ''; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Timeout for creating the socket connection |
||
| 81 | * |
||
| 82 | * @var null|float |
||
| 83 | */ |
||
| 84 | protected $timeout; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Whether or not an exception should be thrown on failed connections |
||
| 88 | * |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $throwExceptions = true; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Socket connection |
||
| 95 | * |
||
| 96 | * @var resource|null |
||
| 97 | */ |
||
| 98 | protected $socket = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Metadata for the DataDog event message |
||
| 102 | * |
||
| 103 | * @var array - time - Assign a timestamp to the event. |
||
| 104 | * - hostname - Assign a hostname to the event |
||
| 105 | * - key - Assign an aggregation key to the event, to group it with some others |
||
| 106 | * - priority - Can be 'normal' or 'low' |
||
| 107 | * - source - Assign a source type to the event |
||
| 108 | * - alert - Can be 'error', 'warning', 'info' or 'success' |
||
| 109 | */ |
||
| 110 | protected $eventMetaData = [ |
||
| 111 | 'time' => 'd', |
||
| 112 | 'hostname' => 'h', |
||
| 113 | 'key' => 'k', |
||
| 114 | 'priority' => 'p', |
||
| 115 | 'source' => 's', |
||
| 116 | 'alert' => 't', |
||
| 117 | ]; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array - time - Assign a timestamp to the service check |
||
| 121 | * - hostname - Assign a hostname to the service check |
||
| 122 | */ |
||
| 123 | protected $serviceCheckMetaData = [ |
||
| 124 | 'time' => 'd', |
||
| 125 | 'hostname' => 'h', |
||
| 126 | ]; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var array - message - Assign a message to the service check |
||
| 130 | */ |
||
| 131 | protected $serviceCheckMessage = [ |
||
| 132 | 'message' => 'm', |
||
| 133 | ]; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Is the server type DataDog implementation |
||
| 137 | * |
||
| 138 | * @var bool |
||
| 139 | */ |
||
| 140 | protected $dataDog = true; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set of default tags to send to every request |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | protected $tags = []; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Singleton Reference |
||
| 151 | * |
||
| 152 | * @param string $name Instance name |
||
| 153 | * |
||
| 154 | * @return Client Client instance |
||
| 155 | */ |
||
| 156 | 1 | public static function instance($name = 'default') |
|
| 157 | { |
||
| 158 | 1 | if (!isset(static::$instances[$name])) { |
|
| 159 | 1 | static::$instances[$name] = new static($name); |
|
| 160 | 1 | } |
|
| 161 | 1 | return static::$instances[$name]; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Create a new instance |
||
| 166 | * |
||
| 167 | * @param string|null $instanceId |
||
| 168 | */ |
||
| 169 | 57 | public function __construct($instanceId = null) |
|
| 170 | { |
||
| 171 | 57 | $this->instanceId = $instanceId ?: uniqid(); |
|
| 172 | |||
| 173 | 57 | if (empty($this->timeout)) { |
|
| 174 | 57 | $this->timeout = (float) ini_get('default_socket_timeout'); |
|
| 175 | 57 | } |
|
| 176 | 57 | } |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Get string value of instance |
||
| 180 | * |
||
| 181 | * @return string String representation of this instance |
||
| 182 | */ |
||
| 183 | 2 | public function __toString() |
|
| 184 | { |
||
| 185 | 2 | return 'DogStatsD\Client::[' . $this->instanceId . ']'; |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Initialize Connection Details |
||
| 190 | * |
||
| 191 | * @param array $options Configuration options |
||
| 192 | * :host <string|ip> - host to talk to |
||
| 193 | * :port <int> - Port to communicate with |
||
| 194 | * :namespace <string> - Default namespace |
||
| 195 | * :timeout <float> - Timeout in seconds |
||
| 196 | * :throwExceptions <bool> - Throw an exception on connection error |
||
| 197 | * :dataDog <bool> - Use DataDog's version of statsd (Default: true) |
||
| 198 | * :tags <array> - List of tags to add to each message |
||
| 199 | * |
||
| 200 | * @return Client This instance |
||
| 201 | * @throws ConfigurationException If port is invalid |
||
| 202 | */ |
||
| 203 | 57 | public function configure(array $options = []) |
|
| 204 | { |
||
| 205 | $setOption = function ($name, $type = null) use ($options) { |
||
| 206 | 57 | if (isset($options[$name])) { |
|
| 207 | 21 | if (!is_null($type) && (gettype($options[$name]) != $type)) { |
|
| 208 | 6 | throw new ConfigurationException($this, sprintf( |
|
| 209 | 6 | "Option: %s is expected to be: '%s', was: '%s'", |
|
| 210 | 6 | $name, |
|
| 211 | 6 | $type, |
|
| 212 | 6 | gettype($options[$name]) |
|
| 213 | 6 | )); |
|
| 214 | } |
||
| 215 | 15 | $this->{$name} = $options[$name]; |
|
| 216 | 15 | } |
|
| 217 | 57 | }; |
|
| 218 | |||
| 219 | 57 | $setOption('host', 'string'); |
|
| 220 | 57 | $setOption('port', 'integer'); |
|
| 221 | 57 | $setOption('namespace', 'string'); |
|
| 222 | 57 | $setOption('timeout'); |
|
| 223 | 57 | $setOption('throwExceptions', 'boolean'); |
|
| 224 | 57 | $setOption('dataDog', 'boolean'); |
|
| 225 | 57 | $setOption('tags', 'array'); |
|
| 226 | |||
| 227 | 57 | if (!$this->port || !is_numeric($this->port) || $this->port > 65535) { |
|
| 228 | 1 | throw new ConfigurationException($this, 'Option: Port is out of range'); |
|
| 229 | } |
||
| 230 | |||
| 231 | 57 | return $this; |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get Host |
||
| 236 | * |
||
| 237 | * @return string Host |
||
| 238 | */ |
||
| 239 | 1 | public function getHost() |
|
| 240 | { |
||
| 241 | 1 | return $this->host; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get Port |
||
| 246 | * |
||
| 247 | * @return int Port |
||
| 248 | */ |
||
| 249 | 2 | public function getPort() |
|
| 250 | { |
||
| 251 | 2 | return $this->port; |
|
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get Namespace |
||
| 256 | * |
||
| 257 | * @return string Namespace |
||
| 258 | */ |
||
| 259 | 1 | public function getNamespace() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Get Last Message |
||
| 266 | * |
||
| 267 | * @return string Last message sent to server |
||
| 268 | */ |
||
| 269 | 37 | public function getLastMessage() |
|
| 270 | { |
||
| 271 | 37 | return $this->message; |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Increment a metric |
||
| 276 | * |
||
| 277 | * @param string|string[] $metrics Metric(s) to increment |
||
| 278 | * @param int $delta Value to decrement the metric by |
||
| 279 | * @param float $sampleRate Sample rate of metric |
||
| 280 | * @param string[] $tags List of tags for this metric |
||
| 281 | * |
||
| 282 | * @return Client This instance |
||
| 283 | */ |
||
| 284 | 17 | public function increment($metrics, $delta = 1, $sampleRate = 1.0, array $tags = []) |
|
| 285 | { |
||
| 286 | 17 | $metrics = is_array($metrics) ? $metrics : [$metrics]; |
|
| 287 | |||
| 288 | 17 | if ($this->isSampled($sampleRate, $postfix)) { |
|
| 289 | 16 | $data = []; |
|
| 290 | 16 | foreach ($metrics as $metric) { |
|
| 291 | 16 | $data[$metric] = $delta . '|c' . $postfix; |
|
| 292 | 16 | } |
|
| 293 | 16 | return $this->send($data, $tags); |
|
| 294 | } |
||
| 295 | 1 | return $this; |
|
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Decrement a metric |
||
| 300 | * |
||
| 301 | * @param string|string[] $metrics Metric(s) to decrement |
||
| 302 | * @param int $delta Value to increment the metric by |
||
| 303 | * @param int $sampleRate Sample rate of metric |
||
| 304 | * @param string[] $tags List of tags for this metric |
||
| 305 | * |
||
| 306 | * @return Client This instance |
||
| 307 | */ |
||
| 308 | 2 | public function decrement($metrics, $delta = 1, $sampleRate = 1, array $tags = []) |
|
| 309 | { |
||
| 310 | 2 | return $this->increment($metrics, 0 - $delta, $sampleRate, $tags); |
|
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Timing |
||
| 315 | * |
||
| 316 | * @param string $metric Metric to track |
||
| 317 | * @param float $time Time in milliseconds |
||
| 318 | * @param string[] $tags List of tags for this metric |
||
| 319 | * |
||
| 320 | * @return Client This instance |
||
| 321 | */ |
||
| 322 | 3 | public function timing($metric, $time, array $tags = []) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Time a function |
||
| 334 | * |
||
| 335 | * @param string $metric Metric to time |
||
| 336 | * @param callable $func Function to record |
||
| 337 | * @param string[] $tags List of tags for this metric |
||
| 338 | * |
||
| 339 | * @return Client This instance |
||
| 340 | */ |
||
| 341 | 1 | public function time($metric, callable $func, array $tags = []) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Gauges |
||
| 352 | * |
||
| 353 | * @param string $metric Metric to gauge |
||
| 354 | * @param int $value Set the value of the gauge |
||
| 355 | * @param string[] $tags List of tags for this metric |
||
| 356 | * |
||
| 357 | * @return Client This instance |
||
| 358 | */ |
||
| 359 | 2 | public function gauge($metric, $value, array $tags = []) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Histogram |
||
| 371 | * |
||
| 372 | * @param string $metric Metric to send |
||
| 373 | * @param float $value Value to send |
||
| 374 | * @param float $sampleRate Sample rate of metric |
||
| 375 | * @param string[] $tags List of tags for this metric |
||
| 376 | * |
||
| 377 | * @return Client This instance |
||
| 378 | */ |
||
| 379 | 4 | public function histogram($metric, $value, $sampleRate = 1.0, array $tags = []) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Sets - count the number of unique elements for a group |
||
| 392 | * |
||
| 393 | * @param string $metric |
||
| 394 | * @param int $value |
||
| 395 | * @param string[] $tags List of tags for this metric |
||
| 396 | * |
||
| 397 | * @return Client This instance |
||
| 398 | */ |
||
| 399 | 2 | public function set($metric, $value, array $tags = []) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Send a event notification |
||
| 411 | * |
||
| 412 | * @link http://docs.datadoghq.com/guides/dogstatsd/#events |
||
| 413 | * |
||
| 414 | * @param string $title Event Title |
||
| 415 | * @param string $text Event Text |
||
| 416 | * @param array $metadata Set of metadata for this event: |
||
| 417 | * - time - Assign a timestamp to the event. |
||
| 418 | * - hostname - Assign a hostname to the event |
||
| 419 | * - key - Assign an aggregation key to th event, to group it with some others |
||
| 420 | * - priority - Can be 'normal' or 'low' |
||
| 421 | * - source - Assign a source type to the event |
||
| 422 | * - alert - Can be 'error', 'warning', 'info' or 'success' |
||
| 423 | * @param string[] $tags List of tags for this event |
||
| 424 | * |
||
| 425 | * @return Client This instance |
||
| 426 | * @throws ConnectionException If there is a connection problem with the host |
||
| 427 | */ |
||
| 428 | 6 | public function event($title, $text, array $metadata = [], array $tags = []) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Service Checks |
||
| 454 | * |
||
| 455 | * @link http://docs.datadoghq.com/guides/dogstatsd/#service-checks |
||
| 456 | * |
||
| 457 | * @param string $name Name of the service |
||
| 458 | * @param int $status digit corresponding to the status you’re reporting (OK = 0, WARNING = 1, CRITICAL = 2, |
||
| 459 | * UNKNOWN = 3) |
||
| 460 | * @param array $metadata - time - Assign a timestamp to the service check |
||
| 461 | * - hostname - Assign a hostname to the service check |
||
| 462 | * @param string[] $tags List of tags for this event |
||
| 463 | * |
||
| 464 | * @return Client This instance |
||
| 465 | * @throws ConnectionException If there is a connection problem with the host |
||
| 466 | */ |
||
| 467 | 6 | public function serviceCheck($name, $status, array $metadata = [], array $tags = []) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * @param float $rate |
||
| 495 | * @param string $postfix |
||
| 496 | * |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | 21 | private function isSampled($rate = 1.0, &$postfix = '') |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @param string[] $tags A list of tags to apply to each message |
||
| 513 | * |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | 36 | private function formatTags(array $tags = []) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Send Data to StatsD Server |
||
| 536 | * |
||
| 537 | * @param string[] $data A list of messages to send to the server |
||
| 538 | * @param string[] $tags A list of tags to apply to each message |
||
| 539 | * |
||
| 540 | * @return Client This instance |
||
| 541 | * @throws ConnectionException If there is a connection problem with the host |
||
| 542 | */ |
||
| 543 | 26 | protected function send(array $data, array $tags = []) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * @param string[] $messages |
||
| 556 | * |
||
| 557 | * @return Client This instance |
||
| 558 | * @throws ConnectionException If there is a connection problem with the host |
||
| 559 | */ |
||
| 560 | 36 | protected function sendMessages(array $messages) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Attempt to write the current message to the socket |
||
| 573 | * |
||
| 574 | * @return bool |
||
| 575 | */ |
||
| 576 | 35 | private function write() |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Creates a persistent connection to the udp host:port |
||
| 594 | * |
||
| 595 | * @return resource |
||
| 596 | * @throws ConnectionException If there is a connection problem with the host |
||
| 597 | */ |
||
| 598 | 36 | protected function connect() |
|
| 618 | |||
| 619 | 3 | public function __destruct() |
|
| 625 | } |
||
| 626 |