| Conditions | 38 |
| Paths | > 20000 |
| Total Lines | 138 |
| Code Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 36 | public function __construct(SplFileInfo $fileInfo) |
||
| 37 | { |
||
| 38 | $path = $fileInfo->getPathname(); |
||
| 39 | |||
| 40 | $perms = 0; |
||
| 41 | $owner = null; |
||
| 42 | $group = null; |
||
| 43 | $mtime = null; |
||
| 44 | $realpath = null; |
||
| 45 | $linktarget = null; |
||
| 46 | $size = null; |
||
| 47 | $is_file = false; |
||
| 48 | $is_dir = false; |
||
| 49 | $is_link = false; |
||
| 50 | $typename = 'Unknown file'; |
||
| 51 | |||
| 52 | try { |
||
| 53 | // SplFileInfo::getRealPath will return cwd when path is '' |
||
| 54 | if ('' !== $path && $fileInfo->getRealPath()) { |
||
| 55 | $perms = $fileInfo->getPerms(); |
||
| 56 | $size = $fileInfo->getSize(); |
||
| 57 | $owner = $fileInfo->getOwner(); |
||
| 58 | $group = $fileInfo->getGroup(); |
||
| 59 | $mtime = $fileInfo->getMTime(); |
||
| 60 | $realpath = $fileInfo->getRealPath(); |
||
| 61 | } |
||
| 62 | |||
| 63 | $is_dir = $fileInfo->isDir(); |
||
| 64 | $is_file = $fileInfo->isFile(); |
||
| 65 | $is_link = $fileInfo->isLink(); |
||
| 66 | |||
| 67 | if ($is_link) { |
||
| 68 | $lt = $fileInfo->getLinkTarget(); |
||
| 69 | $linktarget = false === $lt ? null : $lt; |
||
| 70 | } |
||
| 71 | } catch (RuntimeException $e) { |
||
| 72 | if (false === \strpos($e->getMessage(), ' open_basedir ')) { |
||
| 73 | throw $e; // @codeCoverageIgnore |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $typeflag = '-'; |
||
| 78 | |||
| 79 | switch ($perms & 0xF000) { |
||
| 80 | case 0xC000: |
||
| 81 | $typename = 'Socket'; |
||
| 82 | $typeflag = 's'; |
||
| 83 | break; |
||
| 84 | case 0x6000: |
||
| 85 | $typename = 'Block device'; |
||
| 86 | $typeflag = 'b'; |
||
| 87 | break; |
||
| 88 | case 0x2000: |
||
| 89 | $typename = 'Character device'; |
||
| 90 | $typeflag = 'c'; |
||
| 91 | break; |
||
| 92 | case 0x1000: |
||
| 93 | $typename = 'Named pipe'; |
||
| 94 | $typeflag = 'p'; |
||
| 95 | break; |
||
| 96 | default: |
||
| 97 | if ($is_file) { |
||
| 98 | if ($is_link) { |
||
| 99 | $typename = 'File symlink'; |
||
| 100 | $typeflag = 'l'; |
||
| 101 | } else { |
||
| 102 | $typename = 'File'; |
||
| 103 | $typeflag = '-'; |
||
| 104 | } |
||
| 105 | } elseif ($is_dir) { |
||
| 106 | if ($is_link) { |
||
| 107 | $typename = 'Directory symlink'; |
||
| 108 | $typeflag = 'l'; |
||
| 109 | } else { |
||
| 110 | $typename = 'Directory'; |
||
| 111 | $typeflag = 'd'; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | |||
| 117 | $flags = [$typeflag]; |
||
| 118 | |||
| 119 | // User |
||
| 120 | $flags[] = (($perms & 0400) ? 'r' : '-'); |
||
| 121 | $flags[] = (($perms & 0200) ? 'w' : '-'); |
||
| 122 | if ($perms & 0100) { |
||
| 123 | $flags[] = ($perms & 04000) ? 's' : 'x'; |
||
| 124 | } else { |
||
| 125 | $flags[] = ($perms & 04000) ? 'S' : '-'; |
||
| 126 | } |
||
| 127 | |||
| 128 | // Group |
||
| 129 | $flags[] = (($perms & 0040) ? 'r' : '-'); |
||
| 130 | $flags[] = (($perms & 0020) ? 'w' : '-'); |
||
| 131 | if ($perms & 0010) { |
||
| 132 | $flags[] = ($perms & 02000) ? 's' : 'x'; |
||
| 133 | } else { |
||
| 134 | $flags[] = ($perms & 02000) ? 'S' : '-'; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Other |
||
| 138 | $flags[] = (($perms & 0004) ? 'r' : '-'); |
||
| 139 | $flags[] = (($perms & 0002) ? 'w' : '-'); |
||
| 140 | if ($perms & 0001) { |
||
| 141 | $flags[] = ($perms & 01000) ? 's' : 'x'; |
||
| 142 | } else { |
||
| 143 | $flags[] = ($perms & 01000) ? 'S' : '-'; |
||
| 144 | } |
||
| 145 | |||
| 146 | $contents = \implode($flags).' '.$owner.' '.$group.' '.$size.' '; |
||
| 147 | |||
| 148 | if (null !== $mtime) { |
||
| 149 | if (\date('Y', $mtime) === \date('Y')) { |
||
| 150 | $contents .= \date('M d H:i', $mtime); |
||
| 151 | } else { |
||
| 152 | $contents .= \date('M d Y', $mtime); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $contents .= ' '; |
||
| 157 | |||
| 158 | if ($is_link && null !== $linktarget) { |
||
| 159 | $contents .= $path.' -> '.$linktarget; |
||
| 160 | } elseif (null !== $realpath && \strlen($realpath) < \strlen($path)) { |
||
| 161 | $contents .= $realpath; |
||
| 162 | } else { |
||
| 163 | $contents .= $path; |
||
| 164 | } |
||
| 165 | |||
| 166 | $label = $typename; |
||
| 167 | |||
| 168 | if (null !== $size && $is_file) { |
||
| 169 | $size = Utils::getHumanReadableBytes($size); |
||
| 170 | $label .= ' ('.$size['value'].$size['unit'].')'; |
||
| 171 | } |
||
| 172 | |||
| 173 | parent::__construct($label, $contents, 'splfileinfo'); |
||
| 174 | } |
||
| 181 |