Conditions | 14 |
Paths | 12 |
Total Lines | 77 |
Code Lines | 29 |
Lines | 18 |
Ratio | 23.38 % |
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 namespace Anomaly\Streams\Platform\Http; |
||
92 | protected function defineLocale() |
||
93 | { |
||
94 | /* |
||
95 | * Make sure the ORIGINAL_REQUEST_URI is always available |
||
96 | * Overwrite later as necessary |
||
97 | */ |
||
98 | $_SERVER['ORIGINAL_REQUEST_URI'] = array_get($_SERVER, 'REQUEST_URI'); |
||
99 | |||
100 | /* |
||
101 | * First grab the supported i18n locales |
||
102 | * that we should be looking for. |
||
103 | */ |
||
104 | $locales = require __DIR__ . '/../../resources/config/locales.php'; |
||
105 | |||
106 | if (file_exists($override = __DIR__ . '/../../../../../resources/core/config/streams/locales.php')) { |
||
107 | $locales = array_replace_recursive($locales, require $override); |
||
108 | } |
||
109 | |||
110 | if (!$hint = array_get($locales, 'hint')) { |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | /* |
||
115 | * Check the domain for a locale. |
||
116 | */ |
||
117 | $url = parse_url(array_get($_SERVER, 'HTTP_HOST')); |
||
118 | |||
119 | if ($url === false) { |
||
120 | throw new \Exception('Malformed URL: ' . $url); |
||
121 | } |
||
122 | |||
123 | $host = array_get($url, 'host'); |
||
124 | |||
125 | $pattern = '/^(' . implode('|', array_keys($locales['supported'])) . ')(\.)./'; |
||
126 | |||
127 | if ($host && ($hint === 'domain' || $hint === true) && preg_match($pattern, $host, $matches)) { |
||
128 | |||
129 | define('LOCALE', $matches[1]); |
||
130 | |||
131 | return; |
||
132 | } |
||
133 | |||
134 | /* |
||
135 | * Let's first look in the URI |
||
136 | * path for for a locale. |
||
137 | */ |
||
138 | $pattern = '/^\/(' . implode('|', array_keys($locales['supported'])) . ')\//'; |
||
139 | |||
140 | $uri = array_get($_SERVER, 'REQUEST_URI', filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL)); |
||
141 | |||
142 | View Code Duplication | if (($hint === 'uri' || $hint === true) && preg_match($pattern, $uri, $matches)) { |
|
143 | |||
144 | $_SERVER['ORIGINAL_REQUEST_URI'] = $uri; |
||
145 | $_SERVER['REQUEST_URI'] = preg_replace($pattern, '/', $uri); |
||
146 | |||
147 | define('LOCALE', $matches[1]); |
||
148 | |||
149 | return; |
||
150 | } |
||
151 | |||
152 | /* |
||
153 | * Check if we're on the home page. |
||
154 | */ |
||
155 | $pattern = '/^\/(' . implode('|', array_keys($locales['supported'])) . ')$/'; |
||
156 | |||
157 | $uri = array_get($_SERVER, 'REQUEST_URI', filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL)); |
||
158 | |||
159 | View Code Duplication | if (($hint === 'uri' || $hint === true) && preg_match($pattern, $uri, $matches)) { |
|
160 | |||
161 | $_SERVER['ORIGINAL_REQUEST_URI'] = $uri; |
||
162 | $_SERVER['REQUEST_URI'] = preg_replace($pattern, '/', $uri); |
||
163 | |||
164 | define('LOCALE', $matches[1]); |
||
165 | |||
166 | return; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.