| Conditions | 10 |
| Paths | 20 |
| Total Lines | 25 |
| 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 |
||
| 116 | public function resolvePath(string $path, $basePath = true): string |
||
| 117 | { |
||
| 118 | // Make absolute path |
||
| 119 | if ($path[0] !== DIRECTORY_SEPARATOR) { |
||
| 120 | if ($basePath === true) { |
||
| 121 | // Get PWD first to avoid getcwd() resolving symlinks if in symlinked folder |
||
| 122 | $path = (getenv('PWD') ?: getcwd()) . DIRECTORY_SEPARATOR . $path; |
||
| 123 | } elseif ('' !== $basePath) { |
||
| 124 | $path = $basePath . DIRECTORY_SEPARATOR . $path; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // Resolve '.' and '..' |
||
| 129 | $components = array(); |
||
| 130 | foreach (explode(DIRECTORY_SEPARATOR, rtrim($path, DIRECTORY_SEPARATOR)) as $name) { |
||
| 131 | if ($name === '..') { |
||
| 132 | array_pop($components); |
||
| 133 | } elseif ($name !== '.' && !(\count($components) && $name === '')) { |
||
| 134 | // … && !(count($components) && $name === '') - we want to keep initial '/' for abs paths |
||
| 135 | $components[] = $name; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return implode(DIRECTORY_SEPARATOR, $components); |
||
| 140 | } |
||
| 141 | } |
||
| 142 |