Conditions | 19 |
Paths | 54 |
Total Lines | 69 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
67 | protected function createHttpBasePath(): string |
||
68 | { |
||
69 | $request = $this->request; |
||
70 | |||
71 | $filename = $request[$request::SCRIPT_FILENAME] ?? ''; |
||
72 | $scriptName = $request[$request::SCRIPT_NAME] ?? ''; |
||
73 | $phpSelf = $request['PHP_SELF'] ?? $scriptName; |
||
74 | $origScriptName = $request['ORIG_SCRIPT_NAME'] ?? ''; |
||
75 | |||
76 | if ($scriptName !== null && basename($scriptName) === $filename) { |
||
77 | $basePath = $scriptName; |
||
78 | } elseif ($phpSelf !== null && basename($phpSelf) === $filename) { |
||
79 | $basePath = $phpSelf; |
||
80 | } elseif ($origScriptName !== null && basename($origScriptName) === $filename) { |
||
81 | // 1and1 shared hosting compatibility. |
||
82 | $basePath = $origScriptName; |
||
83 | } else { |
||
84 | // Backtrack up the SCRIPT_FILENAME to find the portion |
||
85 | // matching PHP_SELF. |
||
86 | $basePath = ''; |
||
87 | $basename = basename($filename); |
||
88 | |||
89 | if ($basename) { |
||
90 | $path = ($phpSelf ? trim($phpSelf, '/') : ''); |
||
91 | $basePos = strpos($path, $basename) ?: 0; |
||
92 | $basePath .= substr($path, 0, $basePos) . $basename; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // If the basePath is empty, then simply return it. |
||
97 | if (empty($basePath)) { |
||
98 | return ''; |
||
99 | } |
||
100 | |||
101 | // Does the base URL have anything in common with the request URI? |
||
102 | $requestUri = (string) $this->requestUri; |
||
103 | |||
104 | // Full base URL matches. |
||
105 | if (0 === strpos($requestUri, $basePath)) { |
||
106 | return $basePath; |
||
107 | } |
||
108 | |||
109 | // Directory portion of base path matches. |
||
110 | $baseDir = '/' . str_replace('\\', '/', dirname($basePath)); |
||
111 | if (0 === strpos($requestUri, $baseDir)) { |
||
112 | return $baseDir; |
||
113 | } |
||
114 | |||
115 | $truncatedRequestUri = $requestUri; |
||
116 | if (false !== ($pos = strpos($requestUri, '?'))) { |
||
117 | $truncatedRequestUri = substr($requestUri, 0, $pos); |
||
118 | } |
||
119 | |||
120 | // No match whatsoever |
||
121 | $basename = basename($basePath); |
||
122 | if (empty($basename) || false === strpos($truncatedRequestUri, $basename)) { |
||
123 | return ''; |
||
124 | } |
||
125 | |||
126 | // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
||
127 | // out of the base path. $pos !== 0 makes sure it is not matching a |
||
128 | // value from PATH_INFO or QUERY_STRING. |
||
129 | if (strlen($requestUri) >= strlen($basePath) |
||
130 | && (false !== ($pos = strpos($requestUri, $basePath)) && $pos !== 0) |
||
131 | ) { |
||
132 | $basePath = substr($requestUri, 0, $pos + strlen($basePath)); |
||
133 | } |
||
134 | |||
135 | return $basePath; |
||
136 | } |
||
138 |