| Conditions | 7 |
| Paths | 9 |
| Total Lines | 59 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 31 | public function parseFileListing($output) |
||
| 32 | { |
||
| 33 | $lines = array_values(array_filter(explode("\n", $output))); |
||
| 34 | $members = array(); |
||
| 35 | |||
| 36 | // BSDTar outputs two differents format of date according to the mtime |
||
| 37 | // of the member. If the member is younger than six months the year is not shown. |
||
| 38 | // On 4.5+ FreeBSD system the day is displayed first |
||
| 39 | $dateFormats = array('M d Y', 'M d H:i', 'd M Y', 'd M H:i'); |
||
| 40 | |||
| 41 | foreach ($lines as $line) { |
||
| 42 | $matches = array(); |
||
| 43 | |||
| 44 | // drw-rw-r-- 0 toto titi 0 Jan 3 1980 practice/ |
||
| 45 | // -rw-rw-r-- 0 toto titi 10240 Jan 22 13:31 practice/records |
||
| 46 | if (!preg_match_all("#" . |
||
| 47 | self::PERMISSIONS . "\s+" . // match (drw-r--r--) |
||
| 48 | self::HARD_LINK . "\s+" . // match (1) |
||
| 49 | self::OWNER . "\s" . // match (toto) |
||
| 50 | self::GROUP . "\s+" . // match (titi) |
||
| 51 | self::FILESIZE . "\s+" . // match (0) |
||
| 52 | self::DATE . "\s+" . // match (Jan 3 1980) |
||
| 53 | self::FILENAME . // match (practice) |
||
| 54 | "#", $line, $matches, PREG_SET_ORDER |
||
| 55 | )) { |
||
| 56 | continue; |
||
| 57 | } |
||
| 58 | |||
| 59 | $chunks = array_shift($matches); |
||
| 60 | |||
| 61 | if (8 !== count($chunks)) { |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | $date = null; |
||
| 66 | |||
| 67 | foreach ($dateFormats as $format) { |
||
| 68 | $date = \DateTime::createFromFormat($format, $chunks[6]); |
||
| 69 | |||
| 70 | if (false === $date) { |
||
| 71 | continue; |
||
| 72 | } else { |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | if (false === $date) { |
||
| 78 | throw new RuntimeException(sprintf('Failed to parse mtime date from %s', $line)); |
||
| 79 | } |
||
| 80 | |||
| 81 | $members[] = array( |
||
| 82 | 'location' => $chunks[7], |
||
| 83 | 'size' => $chunks[5], |
||
| 84 | 'mtime' => $date, |
||
| 85 | 'is_dir' => 'd' === $chunks[1][0] |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $members; |
||
| 90 | } |
||
| 116 |