| Total Complexity | 44 |
| Total Lines | 209 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like FtpConnection 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 FtpConnection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class FtpConnection { |
||
| 30 | /** @var resource|\FTP\Connection */ |
||
|
|
|||
| 31 | private $connection; |
||
| 32 | |||
| 33 | public function __construct(bool $secure, string $hostname, int $port, string $username, string $password) { |
||
| 34 | if ($secure) { |
||
| 35 | $connection = ftp_ssl_connect($hostname, $port); |
||
| 36 | } else { |
||
| 37 | $connection = ftp_connect($hostname, $port); |
||
| 38 | } |
||
| 39 | |||
| 40 | if ($connection === false) { |
||
| 41 | throw new \Exception("Failed to connect to ftp"); |
||
| 42 | } |
||
| 43 | |||
| 44 | if (ftp_login($connection, $username, $password) === false) { |
||
| 45 | throw new \Exception("Failed to connect to login to ftp"); |
||
| 46 | } |
||
| 47 | |||
| 48 | ftp_pasv($connection, true); |
||
| 49 | $this->connection = $connection; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function __destruct() { |
||
| 53 | if ($this->connection) { |
||
| 54 | ftp_close($this->connection); |
||
| 55 | } |
||
| 56 | $this->connection = null; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function setUtf8Mode(): bool { |
||
| 60 | $response = ftp_raw($this->connection, "OPTS UTF8 ON"); |
||
| 61 | return substr($response[0], 0, 3) === '200'; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function fput(string $path, $handle) { |
||
| 66 | } |
||
| 67 | |||
| 68 | public function fget($handle, string $path) { |
||
| 69 | return @ftp_fget($this->connection, $handle, $path, FTP_BINARY); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function mkdir(string $path) { |
||
| 74 | } |
||
| 75 | |||
| 76 | public function chdir(string $path) { |
||
| 77 | return @ftp_chdir($this->connection, $path); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function delete(string $path) { |
||
| 82 | } |
||
| 83 | |||
| 84 | public function rmdir(string $path) { |
||
| 86 | } |
||
| 87 | |||
| 88 | public function rename(string $source, string $target) { |
||
| 90 | } |
||
| 91 | |||
| 92 | public function mdtm(string $path): int { |
||
| 93 | $result = @ftp_mdtm($this->connection, $path); |
||
| 94 | |||
| 95 | // filezilla doesn't like empty path with mdtm |
||
| 96 | if ($result === -1 && $path === "") { |
||
| 97 | $result = @ftp_mdtm($this->connection, "/"); |
||
| 98 | } |
||
| 99 | return $result; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function size(string $path) { |
||
| 104 | } |
||
| 105 | |||
| 106 | public function systype() { |
||
| 107 | return @ftp_systype($this->connection); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function nlist(string $path) { |
||
| 118 | } |
||
| 119 | |||
| 120 | public function mlsd(string $path) { |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | // rawlist parsing logic is based on the ftp implementation from https://github.com/thephpleague/flysystem |
||
| 141 | private function parseRawList(array $rawList, string $directory): array { |
||
| 142 | return array_map(function ($item) use ($directory) { |
||
| 143 | return $this->parseRawListItem($item, $directory); |
||
| 144 | }, $rawList); |
||
| 145 | } |
||
| 146 | |||
| 147 | private function parseRawListItem(string $item, string $directory): array { |
||
| 151 | } |
||
| 152 | |||
| 153 | private function parseUnixItem(string $item, string $directory): array { |
||
| 154 | $item = preg_replace('#\s+#', ' ', $item, 7); |
||
| 155 | |||
| 156 | if (count(explode(' ', $item, 9)) !== 9) { |
||
| 157 | throw new \RuntimeException("Metadata can't be parsed from item '$item' , not enough parts."); |
||
| 158 | } |
||
| 159 | |||
| 160 | [$permissions, /* $number */, /* $owner */, /* $group */, $size, $month, $day, $time, $name] = explode(' ', $item, 9); |
||
| 161 | if ($name === '.') { |
||
| 162 | $type = 'cdir'; |
||
| 163 | } elseif ($name === '..') { |
||
| 164 | $type = 'pdir'; |
||
| 165 | } else { |
||
| 166 | $type = substr($permissions, 0, 1) === 'd' ? 'dir' : 'file'; |
||
| 167 | } |
||
| 168 | |||
| 169 | $parsedDate = (new \DateTime()) |
||
| 170 | ->setTimestamp(strtotime("$month $day $time")); |
||
| 171 | $tomorrow = (new \DateTime())->add(new \DateInterval("P1D")); |
||
| 172 | |||
| 173 | // since the provided date doesn't include the year, we either set it to the correct year |
||
| 174 | // or when the date would otherwise be in the future (by more then 1 day to account for timezone errors) |
||
| 175 | // we use last year |
||
| 176 | if ($parsedDate > $tomorrow) { |
||
| 177 | $parsedDate = $parsedDate->sub(new \DateInterval("P1Y")); |
||
| 178 | } |
||
| 179 | |||
| 180 | $formattedDate = $parsedDate |
||
| 181 | ->format('YmdHis'); |
||
| 182 | |||
| 183 | return [ |
||
| 184 | 'type' => $type, |
||
| 185 | 'name' => $name, |
||
| 186 | 'modify' => $formattedDate, |
||
| 187 | 'perm' => $this->normalizePermissions($permissions), |
||
| 188 | 'size' => (int)$size, |
||
| 189 | ]; |
||
| 190 | } |
||
| 191 | |||
| 192 | private function normalizePermissions(string $permissions) { |
||
| 193 | $isDir = substr($permissions, 0, 1) === 'd'; |
||
| 194 | // remove the type identifier and only use owner permissions |
||
| 195 | $permissions = substr($permissions, 1, 4); |
||
| 196 | |||
| 197 | // map the string rights to the ftp counterparts |
||
| 198 | $filePermissionsMap = ['r' => 'r', 'w' => 'fadfw']; |
||
| 199 | $dirPermissionsMap = ['r' => 'e', 'w' => 'flcdmp']; |
||
| 200 | |||
| 201 | $map = $isDir ? $dirPermissionsMap : $filePermissionsMap; |
||
| 202 | |||
| 203 | return array_reduce(str_split($permissions), function ($ftpPermissions, $permission) use ($map) { |
||
| 204 | if (isset($map[$permission])) { |
||
| 205 | $ftpPermissions .= $map[$permission]; |
||
| 206 | } |
||
| 207 | return $ftpPermissions; |
||
| 208 | }, ''); |
||
| 209 | } |
||
| 210 | |||
| 211 | private function parseWindowsItem(string $item, string $directory): array { |
||
| 238 | ]; |
||
| 239 | } |
||
| 240 | } |
||
| 241 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths