| Conditions | 11 |
| Paths | 63 |
| Total Lines | 53 |
| 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 |
||
| 119 | private static function getCanonicalPath($path): string |
||
| 120 | { |
||
| 121 | $path = static::normalizeWindowsPath($path); |
||
| 122 | |||
| 123 | $absolutePathPrefix = ''; |
||
| 124 | if (static::isAbsolutePath($path)) { |
||
| 125 | if (static::isWindows() && strpos($path, ':/') === 1) { |
||
| 126 | $absolutePathPrefix = substr($path, 0, 3); |
||
| 127 | $path = substr($path, 3); |
||
| 128 | } else { |
||
| 129 | $path = ltrim($path, '/'); |
||
| 130 | $absolutePathPrefix = '/'; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $pathParts = explode('/', $path); |
||
| 135 | $pathPartsLength = count($pathParts); |
||
| 136 | for ($partCount = 0; $partCount < $pathPartsLength; $partCount++) { |
||
| 137 | // double-slashes in path: remove element |
||
| 138 | if ($pathParts[$partCount] === '') { |
||
| 139 | array_splice($pathParts, $partCount, 1); |
||
| 140 | $partCount--; |
||
| 141 | $pathPartsLength--; |
||
| 142 | } |
||
| 143 | // "." in path: remove element |
||
| 144 | if (($pathParts[$partCount] ?? '') === '.') { |
||
| 145 | array_splice($pathParts, $partCount, 1); |
||
| 146 | $partCount--; |
||
| 147 | $pathPartsLength--; |
||
| 148 | } |
||
| 149 | // ".." in path: |
||
| 150 | if (($pathParts[$partCount] ?? '') === '..') { |
||
| 151 | if ($partCount === 0) { |
||
| 152 | array_splice($pathParts, $partCount, 1); |
||
| 153 | $partCount--; |
||
| 154 | $pathPartsLength--; |
||
| 155 | } elseif ($partCount >= 1) { |
||
| 156 | // Rremove this and previous element |
||
| 157 | array_splice($pathParts, $partCount - 1, 2); |
||
| 158 | $partCount -= 2; |
||
| 159 | $pathPartsLength -= 2; |
||
| 160 | } elseif ($absolutePathPrefix) { |
||
| 161 | // can't go higher than root dir |
||
| 162 | // simply remove this part and continue |
||
| 163 | array_splice($pathParts, $partCount, 1); |
||
| 164 | $partCount--; |
||
| 165 | $pathPartsLength--; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return $absolutePathPrefix . implode('/', $pathParts); |
||
| 171 | } |
||
| 172 | |||
| 199 |
If you suppress an error, we recommend checking for the error condition explicitly: