Conditions | 36 |
Paths | 1360 |
Total Lines | 117 |
Code Lines | 64 |
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 |
||
105 | public function initialize(Context $context, array $parameters = array()) |
||
106 | { |
||
107 | parent::initialize($context, $parameters); |
||
108 | |||
109 | /** @var WebRequest $rq */ |
||
110 | $rq = $this->context->getRequest(); |
||
111 | |||
112 | /** @var RequestDataHolder $rd */ |
||
113 | $rd = $rq->getRequestData(); |
||
114 | |||
115 | // 'scheme://authority' is necessary so parse_url doesn't stumble over '://' in the request URI |
||
116 | $ru = array_merge(array('path' => '', 'query' => ''), parse_url('scheme://authority' . $rq->getRequestUri())); |
||
117 | |||
118 | if (isset($_SERVER['QUERY_STRING'])) { |
||
119 | $qs = $_SERVER['QUERY_STRING']; |
||
120 | } else { |
||
121 | $qs = ''; |
||
122 | } |
||
123 | |||
124 | // when rewriting, apache strips one (not all) trailing ampersand from the end of QUERY_STRING... normalize: |
||
125 | $rewritten = (preg_replace('/&+$/D', '', $qs) !== preg_replace('/&+$/D', '', $ru['query'])); |
||
126 | |||
127 | if ($this->isEnabled() && $rewritten) { |
||
128 | // strip the one trailing ampersand, see above |
||
129 | $queryWasEmptied = false; |
||
130 | if ($ru['query'] !== '' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) { |
||
131 | $ru['query'] = preg_replace('/&$/D', '', $ru['query']); |
||
132 | if ($ru['query'] == '') { |
||
133 | $queryWasEmptied = true; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $stripFromQuery = '&' . $ru['query']; |
||
138 | if ($ru['query'] == '' && !$queryWasEmptied && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) { |
||
139 | // if the query is empty, simply give apache2 nothing instead of an "&", since that could kill a real trailing ampersand in the path, as Apache strips those from the query string (which has the rewritten path), but not the request uri |
||
140 | $stripFromQuery = ''; |
||
141 | } |
||
142 | $this->input = preg_replace('/' . preg_quote($stripFromQuery, '/') . '$/D', '', $qs); |
||
143 | |||
144 | if (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache/2') !== false) { |
||
145 | $sru = $_SERVER['REQUEST_URI']; |
||
146 | |||
147 | if (($fqmp = strpos($sru, '?')) !== false && ($fqmp == strlen($sru)-1)) { |
||
148 | // strip a trailing question mark, but only if it really is the query string separator (i.e. the only question mark in the URI) |
||
149 | $sru = substr($sru, 0, -1); |
||
150 | } elseif ($ru['query'] !== '' || $queryWasEmptied) { |
||
151 | // if there is a trailing ampersand (in query string or path, whatever ends the URL), strip it (but just one) |
||
152 | $sru = preg_replace('/&$/D', '', $sru); |
||
153 | } |
||
154 | |||
155 | // multiple consecutive slashes got lost in our input thanks to an apache bug |
||
156 | // let's fix that |
||
157 | $cqs = preg_replace('#/{2,}#', '/', rawurldecode($ru['query'])); |
||
158 | $cru = preg_replace('#/{2,}#', '/', rawurldecode($sru)); |
||
159 | $tmp = preg_replace('/' . preg_quote($this->input . (($cqs != '' || $queryWasEmptied) ? '?' . $cqs : ''), '/') . '$/D', '', $cru); |
||
160 | $input = preg_replace('/^' . preg_quote($tmp, '/') . '/', '', $sru); |
||
161 | if ($ru['query'] !== '' || $queryWasEmptied) { |
||
162 | $input = preg_replace('/' . preg_quote('?' . $ru['query'], '/') . '$/D', '', $input); |
||
163 | } |
||
164 | $this->input = $input; |
||
165 | } |
||
166 | |||
167 | if (!(isset($_SERVER['SERVER_SOFTWARE']) && (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache/1') !== false || (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false && isset($_SERVER['UNENCODED_URL']))))) { |
||
168 | // don't do that for Apache 1 or IIS 7 with URL Rewrite Module, it's already rawurldecode()d there |
||
169 | $this->input = rawurldecode($this->input); |
||
170 | } |
||
171 | |||
172 | $this->basePath = $this->prefix = preg_replace('/' . preg_quote($this->input, '/') . '$/D', '', rawurldecode($ru['path'])); |
||
173 | |||
174 | // that was easy. now clean up $_GET and the Request |
||
175 | $parsedRuQuery = $parsedInput = ''; |
||
176 | parse_str($ru['query'], $parsedRuQuery); |
||
177 | parse_str($this->input, $parsedInput); |
||
178 | if (get_magic_quotes_gpc()) { |
||
179 | $parsedRuQuery = WebRequest::clearMagicQuotes($parsedRuQuery); |
||
180 | $parsedInput = WebRequest::clearMagicQuotes($parsedInput, false /* start on the first level */); |
||
181 | } |
||
182 | foreach (array_diff(array_keys($parsedInput), array_keys($parsedRuQuery)) as $unset) { |
||
183 | // our element is in $_GET |
||
184 | unset($_GET[$unset]); |
||
185 | unset($GLOBALS['HTTP_GET_VARS'][$unset]); |
||
186 | // if it is not also in $_POST, then we need to remove it from the request params |
||
187 | if (!isset($_POST[$unset])) { |
||
188 | $rd->removeParameter($unset); |
||
189 | // and from $_REQUEST, too! |
||
190 | unset($_REQUEST[$unset]); |
||
191 | } |
||
192 | } |
||
193 | } else { |
||
194 | $sn = $_SERVER['SCRIPT_NAME']; |
||
195 | $path = rawurldecode($ru['path']); |
||
196 | |||
197 | $appendFrom = 0; |
||
198 | $this->prefix = Toolkit::stringBase($sn, $path, $appendFrom); |
||
199 | $this->prefix .= substr($sn, $appendFrom); |
||
200 | |||
201 | $this->input = substr($path, $appendFrom); |
||
202 | if (!isset($_SERVER['SERVER_SOFTWARE']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') === false || isset($_SERVER['HTTP_X_REWRITE_URL']) || !isset($_SERVER['GATEWAY_INTERFACE']) || strpos($_SERVER['GATEWAY_INTERFACE'], 'CGI') === false) { |
||
203 | // don't do that for IIS-CGI, it's already rawurldecode()d there |
||
204 | $this->input = rawurldecode($this->input); |
||
205 | } |
||
206 | |||
207 | $this->basePath = str_replace('\\', '/', dirname($this->prefix)); |
||
208 | } |
||
209 | |||
210 | $this->inputParameters = $_GET; |
||
211 | |||
212 | if (!$this->input) { |
||
213 | $this->input = "/"; |
||
214 | } |
||
215 | |||
216 | if (substr($this->basePath, -1, 1) != '/') { |
||
217 | $this->basePath .= '/'; |
||
218 | } |
||
219 | |||
220 | $this->baseHref = $rq->getUrlScheme() . '://' . $rq->getUrlAuthority() . $this->basePath; |
||
221 | } |
||
222 | |||
444 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: