Complex classes like HtaccessFirewall 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 HtaccessFirewall, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class HtaccessFirewall |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | public static $sectionLabel = 'Firewall'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $path; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Filesystem |
||
| 27 | */ |
||
| 28 | private $fileSystem; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Initialize HtaccessFirewall. |
||
| 32 | * |
||
| 33 | * @param $path |
||
| 34 | * @param Filesystem $fileSystem |
||
| 35 | */ |
||
| 36 | public function __construct($path, Filesystem $fileSystem = null) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Deny host. |
||
| 45 | * |
||
| 46 | * @param Host $host |
||
| 47 | */ |
||
| 48 | public function deny(Host $host) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Undeny host. |
||
| 55 | * |
||
| 56 | * @param Host $host |
||
| 57 | */ |
||
| 58 | public function undeny(Host $host) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get all denied hosts. |
||
| 65 | * |
||
| 66 | * @return string[] |
||
| 67 | */ |
||
| 68 | public function getDenied() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Deactivate all denials. |
||
| 82 | */ |
||
| 83 | public function deactivate() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Reactivate all deactivated denials. |
||
| 97 | */ |
||
| 98 | public function reactivate() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set 403 error message. |
||
| 114 | * |
||
| 115 | * @param string $message |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function set403Message($message) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Remove 403 error message. |
||
| 146 | */ |
||
| 147 | public function remove403Message() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check whether Htaccess file exists. |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function exists() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Check whether Htaccess file exists and is readable. |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function readable() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Check whether Htaccess file exists and is writable. |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function writable() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Add single line. |
||
| 184 | * |
||
| 185 | * @param string $line |
||
| 186 | */ |
||
| 187 | private function addLine($line) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Remove single line. |
||
| 201 | * |
||
| 202 | * @param string $content |
||
| 203 | */ |
||
| 204 | private function removeLine($content) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get array of prefixed lines in section. |
||
| 220 | * |
||
| 221 | * @param string|string[] $prefixes |
||
| 222 | * |
||
| 223 | * @return string[] |
||
| 224 | */ |
||
| 225 | private function readLinesWithPrefix($prefixes) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get array of all lines in section. |
||
| 247 | * |
||
| 248 | * @return string[] |
||
| 249 | */ |
||
| 250 | private function readLines() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Write array of lines to section. |
||
| 273 | * |
||
| 274 | * @param string[] $lines |
||
| 275 | * |
||
| 276 | * @throws FileException |
||
| 277 | */ |
||
| 278 | private function writeLines($lines) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Check whether line is the begin of the section. |
||
| 323 | * |
||
| 324 | * @param $line |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | private function isBeginOfSection($line) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Check whether line is the end of the section. |
||
| 335 | * |
||
| 336 | * @param $line |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | private function isEndOfSection($line) |
||
| 344 | } |
||
| 345 |