| Total Complexity | 47 |
| Total Lines | 280 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
Complex classes like PingCommandBuilder 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.
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 PingCommandBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class PingCommandBuilder implements PingCommand |
||
| 26 | { |
||
| 27 | protected int $count; |
||
| 28 | protected ?string $host; |
||
| 29 | protected float $interval; |
||
| 30 | protected int $packet_size; |
||
| 31 | protected int $ttl; |
||
| 32 | protected int $timeout; |
||
| 33 | protected int $version; |
||
| 34 | |||
| 35 | /**- |
||
| 36 | * PingCommand constructor. |
||
| 37 | * |
||
| 38 | * @param string|null $host |
||
| 39 | * @throws InvalidIPAddressException |
||
| 40 | */ |
||
| 41 | public function __construct(?string $host = null) |
||
| 42 | { |
||
| 43 | if ($this->isValidIPAddress($host)) { |
||
| 44 | $this->host = $host; |
||
| 45 | $this->setIPAddressVersion(); |
||
| 46 | } else { |
||
| 47 | if (str_ends_with($host, '/')) { |
||
|
|
|||
| 48 | $host = substr($host, 0, -1); |
||
| 49 | } |
||
| 50 | |||
| 51 | // We assume that is an URL... |
||
| 52 | //TODO: Needs URL validation |
||
| 53 | $pattern = '/^http:\/\/|^https:\/\//'; |
||
| 54 | |||
| 55 | if (preg_match($pattern, $host)) { |
||
| 56 | $this->host = preg_replace($pattern, '', $host); |
||
| 57 | } else { |
||
| 58 | $this->host = $host; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->count = config('ping.count'); |
||
| 63 | $this->interval = config('ping.interval'); |
||
| 64 | $this->packet_size = config('ping.packet_size'); |
||
| 65 | $this->timeout = config('ping.timeout'); |
||
| 66 | $this->ttl = config('ping.ttl'); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Validates an IP Address. |
||
| 71 | * |
||
| 72 | * @param string|null $ip_address |
||
| 73 | * |
||
| 74 | * @throws InvalidIPAddressException |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | private function isValidIPAddress(?string $ip_address = null): bool |
||
| 79 | { |
||
| 80 | return IPAddress::Validate($ip_address); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Select the appropriate IP version. |
||
| 85 | */ |
||
| 86 | private function setIPAddressVersion(): void |
||
| 87 | { |
||
| 88 | if (strpos($this->host, IPAddress::IPV4_SEPARATOR) > 0) { |
||
| 89 | $this->useIPv4(); |
||
| 90 | } elseif (strpos($this->host, IPAddress::IPV6_SEPARATOR) > 0) { |
||
| 91 | $this->useIPv6(); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set IP protocol version 4. |
||
| 97 | */ |
||
| 98 | private function useIPv4(): void |
||
| 99 | { |
||
| 100 | $this->version = 4; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set IP protocol version 6. |
||
| 105 | */ |
||
| 106 | private function useIPv6(): void |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Stop after sending count ECHO_REQUEST packets. With deadline option, ping |
||
| 113 | * waits for count ECHO_REPLY packets, until the timeout expires. |
||
| 114 | * |
||
| 115 | * @param int $count |
||
| 116 | * |
||
| 117 | * @throws NegativeValueException |
||
| 118 | * |
||
| 119 | * @return PingCommandBuilder |
||
| 120 | */ |
||
| 121 | public function count(int $count): PingCommandBuilder |
||
| 122 | { |
||
| 123 | if ($count < 0) { |
||
| 124 | throw new NegativeValueException(); |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->count = $count; |
||
| 128 | |||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Wait interval seconds between sending each packet. The default is to wait |
||
| 134 | * for one second between each packet normally, or not to wait in flood mode. |
||
| 135 | * Only super-user may set interval to values less than 0.2 seconds. |
||
| 136 | * |
||
| 137 | * @param float $interval |
||
| 138 | * |
||
| 139 | * @return PingCommandBuilder |
||
| 140 | */ |
||
| 141 | public function interval(float $interval): PingCommandBuilder |
||
| 142 | { |
||
| 143 | $this->interval = $interval; |
||
| 144 | |||
| 145 | return $this; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Specifies the number of data bytes to be sent. The default is 56, which |
||
| 150 | * translates into 64 ICMP data bytes when combined with the 8 bytes of ICMP |
||
| 151 | * header data. |
||
| 152 | * |
||
| 153 | * @param int $packet_size |
||
| 154 | * |
||
| 155 | * @return PingCommandBuilder |
||
| 156 | */ |
||
| 157 | public function packetSize(int $packet_size): PingCommandBuilder |
||
| 158 | { |
||
| 159 | $this->packet_size = $packet_size; |
||
| 160 | |||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * ping only. Set the IP Time to Live. |
||
| 166 | * |
||
| 167 | * @param int $ttl |
||
| 168 | * |
||
| 169 | * @throws MaxValueException |
||
| 170 | * |
||
| 171 | * @return PingCommandBuilder |
||
| 172 | */ |
||
| 173 | public function ttl(int $ttl): PingCommandBuilder |
||
| 174 | { |
||
| 175 | if ($ttl > 255) { |
||
| 176 | throw new MaxValueException(); |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->ttl = $ttl; |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Time to wait for a response. The option affects only timeout in absence |
||
| 186 | * of any responses, otherwise ping waits for two RTTs. |
||
| 187 | * (Seconds for Linux OS, Milliseconds for Windows). |
||
| 188 | * |
||
| 189 | * @param int $timeout |
||
| 190 | * |
||
| 191 | * @return PingCommandBuilder |
||
| 192 | */ |
||
| 193 | public function timeout(int $timeout): PingCommandBuilder |
||
| 194 | { |
||
| 195 | $this->timeout = $timeout; |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the Linux Ping Command. |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | private function getLinuxCommand(): string |
||
| 206 | { |
||
| 207 | $command = ['ping -n']; |
||
| 208 | |||
| 209 | (!isset($this->version)) ?: array_push($command, '-'.$this->version); |
||
| 210 | (!isset($this->count)) ?: array_push($command, '-c '.$this->count); |
||
| 211 | (!isset($this->interval)) ?: array_push($command, '-i '.$this->interval); |
||
| 212 | (!isset($this->packet_size)) ?: array_push($command, '-s '.$this->packet_size); |
||
| 213 | (!isset($this->timeout)) ?: array_push($command, '-W '.$this->timeout); |
||
| 214 | (!isset($this->ttl)) ?: array_push($command, '-t '.$this->ttl); |
||
| 215 | |||
| 216 | $command[] = $this->host; |
||
| 217 | |||
| 218 | return implode(' ', $command); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the OSX Ping Command. |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | private function getOSXCommand(): string |
||
| 227 | { |
||
| 228 | $command = ['ping']; |
||
| 229 | |||
| 230 | // (!isset($this->version)) ?: array_push($command, '-'.$this->version); |
||
| 231 | (!isset($this->count)) ?: array_push($command, '-c '.$this->count); |
||
| 232 | (!isset($this->interval)) ?: array_push($command, '-i '.$this->interval); |
||
| 233 | (!isset($this->packet_size)) ?: array_push($command, '-s '.$this->packet_size); |
||
| 234 | (!isset($this->timeout)) ?: array_push($command, '-t '.$this->timeout); |
||
| 235 | (!isset($this->ttl)) ?: array_push($command, '-m '.$this->ttl); |
||
| 236 | |||
| 237 | $command[] = $this->host; |
||
| 238 | |||
| 239 | return implode(' ', $command); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns the Windows Ping Command. |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | private function getWindowsCommand(): string |
||
| 248 | { |
||
| 249 | $command = ['ping']; |
||
| 250 | |||
| 251 | (!isset($this->version)) ?: array_push($command, '-'.$this->version); |
||
| 252 | (!isset($this->count)) ?: array_push($command, '-n '.$this->count); |
||
| 253 | (!isset($this->packet_size)) ?: array_push($command, '-l '.$this->packet_size); |
||
| 254 | (!isset($this->timeout)) ?: array_push($command, '-w '.($this->timeout * 1000)); |
||
| 255 | (!isset($this->ttl)) ?: array_push($command, '-i '.$this->ttl); |
||
| 256 | |||
| 257 | $command[] = $this->host; |
||
| 258 | |||
| 259 | return implode(' ', $command); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Return an object with the options. |
||
| 264 | * |
||
| 265 | * @return object |
||
| 266 | */ |
||
| 267 | public function getOptions(): object |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Return the Ping Command. |
||
| 285 | * |
||
| 286 | * @throws UnknownOSException |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function get(): string |
||
| 305 | } |
||
| 306 | } |
||
| 307 |