Complex classes like AbstractFileServer 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 AbstractFileServer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | abstract class AbstractFileServer implements FileServerInterface |
||
| 10 | { |
||
| 11 | /** @var ConnectionSettingsInterface */ |
||
| 12 | protected $connectionSettings; |
||
| 13 | |||
| 14 | function __construct(ConnectionSettingsInterface $connectionSettings) |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Check if the connection settings interface was set. |
||
| 21 | * |
||
| 22 | * @return boolean |
||
| 23 | */ |
||
| 24 | public function hasConnectionSettings() |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get the connection settings interface. |
||
| 31 | * |
||
| 32 | * @return ConnectionSettingsInterface|null |
||
| 33 | */ |
||
| 34 | public function getConnectionSettings() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Set the connection settings interface. |
||
| 44 | * |
||
| 45 | * @param ConnectionSettingsInterface $value The new value. |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | public function setConnectionSettings($value) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set the HTTP response code and optionally print it. |
||
| 56 | * |
||
| 57 | * @param integer|null $code The desired response code. Default is the previous one or 200. |
||
| 58 | * @param bool|true $output If true, the response will be printed. |
||
| 59 | * @param string $outputTemplate The output template to use when printing the response. |
||
| 60 | * @return integer|null The response code. |
||
| 61 | * @throws \Exception For unknown HTTP status codes. |
||
| 62 | */ |
||
| 63 | public function setHttpResponseCode($code=null, $output=true, $outputTemplate='<h1>HTTP <code/> <message/></h1>') |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Override to specify a different request file key. |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getRequestFileKey() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Implement to return the default request or null if required to be specified. |
||
| 139 | * |
||
| 140 | * @return array|null |
||
| 141 | */ |
||
| 142 | public abstract function getDefaultRequest(); |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Implement to return the default request method or null if required to be specified. |
||
| 146 | * |
||
| 147 | * @return string|null |
||
| 148 | */ |
||
| 149 | public abstract function getDefaultRequestMethod(); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Implement to validate the given request format. |
||
| 153 | * |
||
| 154 | * @param array $request |
||
| 155 | * @return boolean |
||
| 156 | */ |
||
| 157 | public function isRequestFormatValid($request) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Implement to return array of allowed request methods. |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public abstract function getAllowedRequestMethods(); |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Process either the current request or the request with the specified parameters and method. |
||
| 170 | * |
||
| 171 | * @param boolean $output If true, standard error messages will be printed. |
||
| 172 | * @param array $request The request parameters. |
||
| 173 | * @param string $requestMethod The request method. |
||
| 174 | * @throws RequestFormatException |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | public function processRequest($output = true, $request = null, $requestMethod = null) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Write the file attachment HTTP response headers. |
||
| 207 | * |
||
| 208 | * @param string $contentType |
||
| 209 | * @param integer $fileSize |
||
| 210 | * @param string $filePath |
||
| 211 | */ |
||
| 212 | protected function writeHeaders($contentType, $fileSize, $filePath) |
||
| 221 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.