| Total Complexity | 40 |
| Total Lines | 128 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FTP 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 FTP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class FTP extends StreamWrapper { |
||
| 42 | private $password; |
||
| 43 | private $user; |
||
| 44 | private $host; |
||
| 45 | private $secure; |
||
| 46 | private $root; |
||
| 47 | |||
| 48 | public function __construct($params) { |
||
| 49 | if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
||
| 50 | $this->host=$params['host']; |
||
| 51 | $this->user=$params['user']; |
||
| 52 | $this->password=$params['password']; |
||
| 53 | if (isset($params['secure'])) { |
||
| 54 | $this->secure = $params['secure']; |
||
| 55 | } else { |
||
| 56 | $this->secure = false; |
||
| 57 | } |
||
| 58 | $this->root=isset($params['root'])?$params['root']:'/'; |
||
| 59 | if (! $this->root || $this->root[0]!=='/') { |
||
| 60 | $this->root='/'.$this->root; |
||
| 61 | } |
||
| 62 | if (substr($this->root, -1) !== '/') { |
||
| 63 | $this->root .= '/'; |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | throw new \Exception('Creating FTP storage failed'); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getId() { |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * construct the ftp url |
||
| 76 | * @param string $path |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function constructUrl($path) { |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Unlinks file or directory |
||
| 90 | * @param string $path |
||
| 91 | */ |
||
| 92 | public function unlink($path) { |
||
| 100 | } |
||
| 101 | } |
||
| 102 | public function fopen($path,$mode) { |
||
| 138 | } |
||
| 139 | |||
| 140 | public function opendir($path) { |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | public function writeBack($tmpFile, $path) { |
||
| 157 | $this->uploadFile($tmpFile, $path); |
||
| 158 | unlink($tmpFile); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * check if php-ftp is installed |
||
| 163 | */ |
||
| 164 | public static function checkDependencies() { |
||
| 169 | } |
||
| 170 | } |
||
| 172 |