Conditions | 23 |
Paths | 1152 |
Total Lines | 73 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
99 | protected static function detectBasePath(ServerRequestInterface $request) |
||
100 | { |
||
101 | $server = $request->getServerParams(); |
||
102 | |||
103 | $filename = isset($server['SCRIPT_FILENAME']) ? $server['SCRIPT_FILENAME'] : ''; |
||
104 | $scriptName = isset($server['SCRIPT_NAME']) ? $server['SCRIPT_NAME'] : null; |
||
105 | $phpSelf = isset($server['PHP_SELF']) ? $server['PHP_SELF'] : null; |
||
106 | $origScriptName = isset($server['ORIG_SCRIPT_NAME']) ? $server['ORIG_SCRIPT_NAME'] : null; |
||
107 | |||
108 | if ($scriptName !== null && basename($scriptName) === $filename) { |
||
109 | $baseUrl = $scriptName; |
||
110 | } elseif ($phpSelf !== null && basename($phpSelf) === $filename) { |
||
111 | $baseUrl = $phpSelf; |
||
112 | } elseif ($origScriptName !== null && basename($origScriptName) === $filename) { |
||
113 | // 1and1 shared hosting compatibility. |
||
114 | $baseUrl = $origScriptName; |
||
115 | } else { |
||
116 | // Backtrack up the SCRIPT_FILENAME to find the portion |
||
117 | // matching PHP_SELF. |
||
118 | $baseUrl = '/'; |
||
119 | $basename = basename($filename); |
||
120 | |||
121 | if ($basename) { |
||
122 | $path = ($phpSelf ? trim($phpSelf, '/') : ''); |
||
123 | $basePos = strpos($path, $basename) ?: 0; |
||
124 | $baseUrl .= substr($path, 0, $basePos).$basename; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // If the baseUrl is empty, then simply return it. |
||
129 | if (empty($baseUrl)) { |
||
130 | return ''; |
||
131 | } |
||
132 | |||
133 | // Does the base URL have anything in common with the request URI? |
||
134 | $requestUri = $request->getUri()->getPath(); |
||
135 | |||
136 | // Full base URL matches. |
||
137 | if (0 === strpos($requestUri, $baseUrl)) { |
||
138 | return $baseUrl; |
||
139 | } |
||
140 | |||
141 | // Directory portion of base path matches. |
||
142 | $baseDir = str_replace('\\', '/', dirname($baseUrl)); |
||
143 | |||
144 | if (0 === strpos($requestUri, $baseDir)) { |
||
145 | return $baseDir; |
||
146 | } |
||
147 | |||
148 | $truncatedRequestUri = $requestUri; |
||
149 | |||
150 | if (false !== ($pos = strpos($requestUri, '?'))) { |
||
151 | $truncatedRequestUri = substr($requestUri, 0, $pos); |
||
152 | } |
||
153 | |||
154 | $basename = basename($baseUrl); |
||
155 | |||
156 | // No match whatsoever |
||
157 | if (empty($basename) || false === strpos($truncatedRequestUri, $basename)) { |
||
158 | return ''; |
||
159 | } |
||
160 | |||
161 | // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
||
162 | // out of the base path. $pos !== 0 makes sure it is not matching a |
||
163 | // value from PATH_INFO or QUERY_STRING. |
||
164 | if (strlen($requestUri) >= strlen($baseUrl) |
||
165 | && (false !== ($pos = strpos($requestUri, $baseUrl)) && $pos !== 0) |
||
166 | ) { |
||
167 | $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
||
168 | } |
||
169 | |||
170 | return $baseUrl; |
||
171 | } |
||
172 | } |
||
173 |