Complex classes like Vhost 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 Vhost, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class Vhost implements VhostInterface |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * Active account |
||
| 51 | * |
||
| 52 | * @var boolean |
||
| 53 | */ |
||
| 54 | protected $active = false; |
||
| 55 | /** |
||
| 56 | * Primary domain |
||
| 57 | * |
||
| 58 | * @var DomainInterface |
||
| 59 | */ |
||
| 60 | protected $primaryDomain; |
||
| 61 | /** |
||
| 62 | * Secondary domains |
||
| 63 | * |
||
| 64 | * @var DomainInterface[] |
||
| 65 | */ |
||
| 66 | protected $secondaryDomains = []; |
||
| 67 | /** |
||
| 68 | * Document root |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $docroot; |
||
| 73 | /** |
||
| 74 | * Virtual host type |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $type = self::TYPE_APACHE; |
||
| 79 | /** |
||
| 80 | * Ports |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $ports = []; |
||
| 85 | /** |
||
| 86 | * Active PHP version |
||
| 87 | * |
||
| 88 | * @var null|string |
||
| 89 | */ |
||
| 90 | protected $php = null; |
||
| 91 | /** |
||
| 92 | * Absolute URL to redirect to |
||
| 93 | * |
||
| 94 | * @var null|string |
||
| 95 | */ |
||
| 96 | protected $redirectUrl = null; |
||
| 97 | /** |
||
| 98 | * Redirect status code |
||
| 99 | * |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | protected $redirectStatus = self::REDIRECT_DEFAULT_STATUS; |
||
| 103 | /** |
||
| 104 | * Default port for HTTP virtual hosts |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | */ |
||
| 108 | const PORT_HTTP_DEFAULT = 80; |
||
| 109 | /** |
||
| 110 | * Default port for HTTPS virtual hosts |
||
| 111 | * |
||
| 112 | * @var int |
||
| 113 | */ |
||
| 114 | const PORT_HTTPS_DEFAULT = 443; |
||
| 115 | /** |
||
| 116 | * Default redirect status |
||
| 117 | * |
||
| 118 | * @var int |
||
| 119 | */ |
||
| 120 | const REDIRECT_DEFAULT_STATUS = 301; |
||
| 121 | /** |
||
| 122 | * HTTP protocol |
||
| 123 | * |
||
| 124 | * @var int |
||
| 125 | */ |
||
| 126 | const PROTOCOL_HTTP = 1; |
||
| 127 | /** |
||
| 128 | * HTTPS protocol |
||
| 129 | * |
||
| 130 | * @var int |
||
| 131 | */ |
||
| 132 | const PROTOCOL_HTTPS = 2; |
||
| 133 | /** |
||
| 134 | * Apache virtual host |
||
| 135 | * |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | const TYPE_APACHE = 'apache'; |
||
| 139 | /** |
||
| 140 | * Supported protocols |
||
| 141 | * |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | public static $supportedProtocols = [ |
||
| 145 | self::PROTOCOL_HTTP => 'http', |
||
| 146 | self::PROTOCOL_HTTPS => 'https', |
||
| 147 | ]; |
||
| 148 | /** |
||
| 149 | * Default protocol ports |
||
| 150 | * |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | public static $defaultProtocolPorts = [ |
||
| 154 | self::PROTOCOL_HTTP => 80, |
||
| 155 | self::PROTOCOL_HTTPS => 443, |
||
| 156 | ]; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Virtual host constructor |
||
| 160 | * |
||
| 161 | * @param DomainInterface $primaryDomain Primary domain |
||
| 162 | * @param string $docroot Document root |
||
| 163 | * @param string $type Virtual host type |
||
| 164 | * @internal param int $port Port |
||
| 165 | */ |
||
| 166 | 10 | public function __construct(DomainInterface $primaryDomain, $docroot, $type = self::TYPE_APACHE) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Return whether the account is active |
||
| 175 | * |
||
| 176 | * @return boolean Active |
||
| 177 | */ |
||
| 178 | public function isActive() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Set whether the account is active |
||
| 185 | * |
||
| 186 | * @param boolean $active Active |
||
| 187 | * @return VhostInterface Self reference |
||
| 188 | */ |
||
| 189 | public function setActive($active) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Return the primary domain |
||
| 197 | * |
||
| 198 | * @return DomainInterface Primary domain |
||
| 199 | */ |
||
| 200 | 2 | public function getPrimaryDomain() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Return the document root |
||
| 207 | * |
||
| 208 | * @return string Document root |
||
| 209 | */ |
||
| 210 | 1 | public function getDocroot() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Return the virtual host type |
||
| 217 | * |
||
| 218 | * @return string Virtual host type |
||
| 219 | */ |
||
| 220 | public function getType() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Return the port |
||
| 227 | * |
||
| 228 | * @param int $protocol Protocol |
||
| 229 | * @return int|null Port |
||
| 230 | * @throws \RuntimeException If the protocol is unsupported |
||
| 231 | */ |
||
| 232 | 3 | public function getPort($protocol) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Return all supported protocols and corresponding ports |
||
| 246 | * |
||
| 247 | * @return array Protocols and ports |
||
| 248 | */ |
||
| 249 | 1 | public function getPorts() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Return the secondary domains |
||
| 256 | * |
||
| 257 | * @param bool $excludeWildcards Exclude wildcard domains |
||
| 258 | * @return DomainInterface[] Secondary domains |
||
| 259 | */ |
||
| 260 | 1 | public function getSecondaryDomains($excludeWildcards = false) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Set the secondary domains |
||
| 272 | * |
||
| 273 | * @param DomainInterface[] $secondaryDomains |
||
| 274 | * @return Vhost Self reference |
||
| 275 | * @throws \RuntimeException If the domain is invalid |
||
| 276 | */ |
||
| 277 | 1 | public function setSecondaryDomains(array $secondaryDomains) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Add a secondary domain |
||
| 296 | * |
||
| 297 | * @param DomainInterface $secondaryDomain Secondary domain |
||
| 298 | * @return Vhost Self reference |
||
| 299 | */ |
||
| 300 | 1 | public function addSecondaryDomain(DomainInterface $secondaryDomain) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Remove a secondary domain |
||
| 310 | * |
||
| 311 | * @param DomainInterface $secondaryDomain Secondary domain |
||
| 312 | * @return Vhost Self reference |
||
| 313 | */ |
||
| 314 | 1 | public function removeSecondaryDomain(DomainInterface $secondaryDomain) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Return the active PHP version |
||
| 322 | * |
||
| 323 | * @return null|string Active PHP version |
||
| 324 | */ |
||
| 325 | 1 | public function getPhp() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set the active PHP version |
||
| 332 | * |
||
| 333 | * @param null|string $php Active PHP version |
||
| 334 | * @return Vhost Self reference |
||
| 335 | * @throws \RuntimeException If the PHP version is invalid |
||
| 336 | */ |
||
| 337 | 1 | public function setPhp($php) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Enable a supported protocol |
||
| 350 | * |
||
| 351 | * @param int $protocol Protocol |
||
| 352 | * @param int $port Port |
||
| 353 | * @return Vhost Self reference |
||
| 354 | * @throws \RuntimeException If the protocol is unsupported |
||
| 355 | * @throws \RuntimeException If the protocol port is invalid |
||
| 356 | */ |
||
| 357 | 3 | public function enableProtocol($protocol, $port = null) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Disable a supported protocol |
||
| 378 | * |
||
| 379 | * @param int $protocol Protocol |
||
| 380 | * @return Vhost Self reference |
||
| 381 | * @throws \RuntimeException If the protocol is unsupported |
||
| 382 | */ |
||
| 383 | 2 | public function disableProtocol($protocol) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Return the redirect URL |
||
| 398 | * |
||
| 399 | * @return null|string Redirect URL |
||
| 400 | */ |
||
| 401 | 1 | public function getRedirectUrl() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Set the redirect URL |
||
| 408 | * |
||
| 409 | * @param null|string $redirectUrl Redirect URL |
||
| 410 | * @return Vhost Self reference |
||
| 411 | * @throws \RuntimeException If the redirect URL is invalid |
||
| 412 | */ |
||
| 413 | 1 | public function setRedirectUrl($redirectUrl) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Return the redirect HTTP status code |
||
| 431 | * |
||
| 432 | * @return int Redirect HTTP status code |
||
| 433 | */ |
||
| 434 | 1 | public function getRedirectStatus() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Set the redirect HTTP status code |
||
| 441 | * |
||
| 442 | * @param int $redirectStatus Redirect HTTP status code |
||
| 443 | * @return Vhost Self reference |
||
| 444 | * @throw \RuntimeException If the redirect HTTP status code is invalid |
||
| 445 | */ |
||
| 446 | 1 | public function setRedirectStatus($redirectStatus) |
|
| 455 | } |
||
| 456 |