Conditions | 19 |
Paths | 72 |
Total Lines | 66 |
Code Lines | 36 |
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 |
||
140 | protected function detectBaseUri(): string |
||
141 | { |
||
142 | $filename = $this->getServerParam('SCRIPT_FILENAME', ''); |
||
143 | $scriptName = $this->getServerParam('SCRIPT_NAME'); |
||
144 | $phpSelf = $this->getServerParam('PHP_SELF'); |
||
145 | $origScriptName = $this->getServerParam('ORIG_SCRIPT_NAME'); |
||
146 | |||
147 | if ($scriptName !== null && basename($scriptName) === $filename) { |
||
148 | $baseUri = $scriptName; |
||
149 | } elseif ($phpSelf !== null && basename($phpSelf) === $filename) { |
||
150 | $baseUri = $phpSelf; |
||
151 | } elseif ($origScriptName !== null && basename($origScriptName) === $filename) { |
||
152 | // 1and1 shared hosting compatibility. |
||
153 | $baseUri = $origScriptName; |
||
154 | } else { |
||
155 | // Backtrack up the SCRIPT_FILENAME to find the portion |
||
156 | // matching PHP_SELF. |
||
157 | $baseUri = '/'; |
||
158 | $basename = basename($filename); |
||
159 | if ($basename) { |
||
160 | $path = ($phpSelf ? trim($phpSelf, '/') : ''); |
||
161 | $basePos = strpos($path, $basename) ?: 0; |
||
162 | $baseUri .= substr($path, 0, $basePos) . $basename; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | // If the baseUri is empty, then simply return it. |
||
167 | if (empty($baseUri)) { |
||
168 | return ''; |
||
169 | } |
||
170 | |||
171 | // Does the base URL have anything in common with the request URI? |
||
172 | $requestUri = $this->request->getUri()->getPath(); |
||
173 | |||
174 | // Full base URL matches. |
||
175 | if (0 === strpos($requestUri, $baseUri)) { |
||
176 | return $baseUri; |
||
177 | } |
||
178 | |||
179 | // Directory portion of base path matches. |
||
180 | $baseDir = str_replace('\\', '/', dirname($baseUri)); |
||
181 | if (0 === strpos($requestUri, $baseDir)) { |
||
182 | return $baseDir; |
||
183 | } |
||
184 | $truncatedRequestUri = $requestUri; |
||
185 | if (false !== ($pos = strpos($requestUri, '?'))) { |
||
186 | $truncatedRequestUri = substr($requestUri, 0, $pos); |
||
187 | } |
||
188 | $basename = basename($baseUri); |
||
189 | |||
190 | // No match whatsoever |
||
191 | if (empty($basename) || false === strpos($truncatedRequestUri, $basename)) { |
||
192 | return ''; |
||
193 | } |
||
194 | |||
195 | // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
||
196 | // out of the base path. $pos !== 0 makes sure it is not matching a |
||
197 | // value from PATH_INFO or QUERY_STRING. |
||
198 | if (strlen($requestUri) >= strlen($baseUri) |
||
199 | && (false !== ($pos = strpos($requestUri, $baseUri)) && $pos !== 0) |
||
200 | ) { |
||
201 | $baseUri = substr($requestUri, 0, $pos + strlen($baseUri)); |
||
202 | } |
||
203 | |||
204 | return $baseUri; |
||
205 | } |
||
206 | |||
234 |