Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Parser { |
||
| 23 | const MSG_NOT_FOUND = 'Error opening local file '; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \Icewind\SMB\TimeZoneProvider |
||
| 27 | */ |
||
| 28 | protected $timeZoneProvider; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param \Icewind\SMB\TimeZoneProvider $timeZoneProvider |
||
| 32 | */ |
||
| 33 | 552 | public function __construct(TimeZoneProvider $timeZoneProvider) { |
|
| 36 | |||
| 37 | 36 | private function getErrorCode($line) { |
|
| 38 | 36 | $parts = explode(' ', $line); |
|
| 39 | 36 | foreach ($parts as $part) { |
|
| 40 | 36 | if (substr($part, 0, 9) === 'NT_STATUS') { |
|
| 41 | 36 | return $part; |
|
| 42 | } |
||
| 43 | 6 | } |
|
| 44 | 4 | return false; |
|
| 45 | } |
||
| 46 | |||
| 47 | 36 | public function checkForError($output, $path) { |
|
| 48 | 36 | if (strpos($output[0], 'does not exist')) { |
|
| 49 | throw new NotFoundException($path); |
||
| 50 | } |
||
| 51 | 36 | $error = $this->getErrorCode($output[0]); |
|
| 52 | |||
| 53 | 36 | if (substr($output[0], 0, strlen(self::MSG_NOT_FOUND)) === self::MSG_NOT_FOUND) { |
|
| 54 | 2 | $localPath = substr($output[0], strlen(self::MSG_NOT_FOUND)); |
|
| 55 | 2 | throw new InvalidResourceException('Failed opening local file "' . $localPath . '" for writing'); |
|
| 56 | } |
||
| 57 | |||
| 58 | switch ($error) { |
||
| 59 | 36 | case ErrorCodes::PathNotFound: |
|
| 60 | 36 | case ErrorCodes::ObjectNotFound: |
|
| 61 | 36 | case ErrorCodes::NoSuchFile: |
|
| 62 | 24 | throw new NotFoundException($path); |
|
| 63 | 14 | case ErrorCodes::NameCollision: |
|
| 64 | 2 | throw new AlreadyExistsException($path); |
|
| 65 | 12 | case ErrorCodes::AccessDenied: |
|
| 66 | throw new AccessDeniedException($path); |
||
| 67 | 12 | case ErrorCodes::DirectoryNotEmpty: |
|
| 68 | 2 | throw new NotEmptyException($path); |
|
| 69 | 10 | case ErrorCodes::FileIsADirectory: |
|
| 70 | 10 | case ErrorCodes::NotADirectory: |
|
| 71 | 6 | throw new InvalidTypeException($path); |
|
| 72 | 4 | case ErrorCodes::SharingViolation: |
|
| 73 | 2 | throw new FileInUseException($path); |
|
| 74 | 2 | default: |
|
| 75 | 2 | throw Exception::unknown($path, $error); |
|
| 76 | 2 | } |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * check if the first line holds a connection failure |
||
| 81 | * |
||
| 82 | * @param $line |
||
| 83 | * @throws AuthenticationException |
||
| 84 | * @throws InvalidHostException |
||
| 85 | * @throws NoLoginServerException |
||
| 86 | */ |
||
| 87 | 504 | public function checkConnectionError($line) { |
|
| 88 | 504 | $line = rtrim($line, ')'); |
|
| 89 | 504 | if (substr($line, -23) === ErrorCodes::LogonFailure) { |
|
| 90 | 2 | throw new AuthenticationException('Invalid login'); |
|
| 91 | } |
||
| 92 | 502 | if (substr($line, -26) === ErrorCodes::BadHostName) { |
|
| 93 | 6 | throw new InvalidHostException('Invalid hostname'); |
|
| 94 | } |
||
| 95 | 498 | if (substr($line, -22) === ErrorCodes::Unsuccessful) { |
|
| 96 | throw new InvalidHostException('Connection unsuccessful'); |
||
| 97 | } |
||
| 98 | 498 | if (substr($line, -28) === ErrorCodes::ConnectionRefused) { |
|
| 99 | throw new InvalidHostException('Connection refused'); |
||
| 100 | } |
||
| 101 | 498 | if (substr($line, -26) === ErrorCodes::NoLogonServers) { |
|
| 102 | throw new NoLoginServerException('No login server'); |
||
| 103 | } |
||
| 104 | 498 | } |
|
| 105 | |||
| 106 | 226 | public function parseMode($mode) { |
|
| 123 | |||
| 124 | 58 | public function parseStat($output) { |
|
| 150 | |||
| 151 | 500 | public function parseDir($output, $basePath) { |
|
| 169 | |||
| 170 | 4 | public function parseListShares($output) { |
|
| 171 | 4 | $shareNames = array(); |
|
| 172 | 4 | foreach ($output as $line) { |
|
| 182 | } |
||
| 183 |