Conditions | 10 |
Paths | 11 |
Total Lines | 36 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 22 |
CRAP Score | 10 |
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 |
||
62 | 10 | public function generateRelativePath($path) |
|
63 | { |
||
64 | 10 | if ($this->isNetworkPath($path) || !$this->hasLeadingSlash($path)) { |
|
65 | 1 | return $path; |
|
66 | } |
||
67 | |||
68 | 9 | $uri = $this->request->getUri(); |
|
69 | |||
70 | 9 | $basePath = $uri->getPath(); |
|
71 | 9 | if ($basePath === $path) { |
|
72 | 1 | return ''; |
|
73 | } |
||
74 | |||
75 | 8 | $baseParts = explode('/', $basePath, -1); |
|
76 | 8 | $pathParts = explode('/', $path); |
|
77 | |||
78 | 8 | foreach ($baseParts as $i => $segment) { |
|
79 | 7 | if (isset($pathParts[$i]) && $segment === $pathParts[$i]) { |
|
80 | 7 | unset($baseParts[$i], $pathParts[$i]); |
|
81 | 7 | } else { |
|
82 | 2 | break; |
|
83 | } |
||
84 | 8 | } |
|
85 | |||
86 | 8 | $path = str_repeat('../', count($baseParts)) . implode('/', $pathParts); |
|
87 | |||
88 | 8 | if (empty($path)) { |
|
89 | 1 | return './'; |
|
90 | } |
||
91 | |||
92 | 7 | if (empty($baseParts) && false !== strpos(current($pathParts), ':')) { |
|
93 | 1 | $path = './' . $path; |
|
94 | 1 | } |
|
95 | |||
96 | 7 | return $path; |
|
97 | } |
||
98 | |||
120 |