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