Complex classes like FtpTransport 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 FtpTransport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class FtpTransport extends AbstractTransport implements ProgressAwareInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var resource |
||
| 17 | */ |
||
| 18 | protected $ftpConnection; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $fileName; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var MatcherInterface |
||
| 27 | */ |
||
| 28 | protected $fileMatcher; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $host |
||
| 32 | * @param string $user |
||
| 33 | * @param string $pass |
||
| 34 | * @param string $file |
||
| 35 | * @param array $options |
||
| 36 | * |
||
| 37 | * @return FtpTransport |
||
| 38 | */ |
||
| 39 | public static function create($host, $user = null, $pass = null, $file, array $options = []) |
||
| 55 | |||
| 56 | public function __clone() |
||
| 64 | |||
| 65 | public function __destruct() |
||
| 69 | |||
| 70 | public function __toString() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function getHost() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return string|null |
||
| 91 | */ |
||
| 92 | public function getUser() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return string|null |
||
| 99 | */ |
||
| 100 | public function getPass() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return string|null |
||
| 107 | */ |
||
| 108 | public function getMode() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $mode |
||
| 115 | */ |
||
| 116 | public function setMode($mode) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return bool|null |
||
| 123 | */ |
||
| 124 | public function getPasv() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param bool $pasv |
||
| 131 | */ |
||
| 132 | public function setPasv($pasv) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return bool|null |
||
| 139 | */ |
||
| 140 | public function getPattern() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param bool $pattern |
||
| 147 | */ |
||
| 148 | public function setPattern($pattern) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $file |
||
| 155 | */ |
||
| 156 | public function setFilename($file) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the file to download from the ftp. Handles globbing rules and |
||
| 164 | * checks if the file is listed in the remote dir. |
||
| 165 | * |
||
| 166 | * @throws TransportException When remote file could not be found |
||
| 167 | * @return string |
||
| 168 | * |
||
| 169 | */ |
||
| 170 | public function getFilename() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return \DateTime|null |
||
| 182 | */ |
||
| 183 | public function getLastModifiedDate() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | public function getSize() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param MatcherInterface $matcher |
||
| 201 | */ |
||
| 202 | public function setFileMatcher(MatcherInterface $matcher) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return MatcherInterface |
||
| 209 | */ |
||
| 210 | public function getFileMatcher() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param MatcherInterface $matcher |
||
| 221 | * |
||
| 222 | * @throws TransportException |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | protected function searchFile(MatcherInterface $matcher) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $destination |
||
| 255 | */ |
||
| 256 | protected function doFetch($destination) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @throws TransportException |
||
| 266 | * @return string |
||
| 267 | * |
||
| 268 | */ |
||
| 269 | protected function downloadToTmpFile() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Returns shared ftp connection. |
||
| 303 | * |
||
| 304 | * @return resource |
||
| 305 | */ |
||
| 306 | protected function getFtpConnection() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Connects to ftp. |
||
| 329 | * |
||
| 330 | * @param string $host |
||
| 331 | * @param string $user |
||
| 332 | * @param string $pass |
||
| 333 | * |
||
| 334 | * @throws TransportException |
||
| 335 | * @return resource |
||
| 336 | * |
||
| 337 | */ |
||
| 338 | protected function connect($host, $user, $pass) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Closes shared ftp connection. |
||
| 352 | */ |
||
| 353 | protected function closeFtpConnection() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return MatcherInterface |
||
| 364 | */ |
||
| 365 | protected function createFileMatcher() |
||
| 382 | } |
||
| 383 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.