Conditions | 29 |
Paths | 2 |
Total Lines | 176 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 1 | 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 |
||
99 | protected function interactiveHandler() |
||
100 | { |
||
101 | // use the framework default Whoops error handler |
||
102 | $this->whoops = new Run; |
||
|
|||
103 | |||
104 | $this->whoops->writeToOutput(false); |
||
105 | $this->whoops->allowQuit(false); |
||
106 | |||
107 | // define the default page handler |
||
108 | $this->pagehandler = new PrettyPageHandler; |
||
109 | $this->pagehandler->addResourcePath(__DIR__.DS.'Whoops'.DS.'resources'); |
||
110 | |||
111 | $this->pagehandler->addDataTableCallback('Application', function() |
||
112 | { |
||
113 | $application = \Application::getInstance(); |
||
114 | $environment = $application->getEnvironment(); |
||
115 | $request = \Request::getInstance(); |
||
116 | $route = $request ? $request->getRoute() : null; |
||
117 | |||
118 | return array( |
||
119 | 'Active application' => $application ? $application->getName() : '', |
||
120 | 'Application namespace' => $route ? rtrim($route->namespace, '\\') : '', |
||
121 | 'Environment' => $environment ? $environment->getName() : '', |
||
122 | ); |
||
123 | }); |
||
124 | |||
125 | $this->pagehandler->addDataTableCallback('Current Request', function() |
||
126 | { |
||
127 | $request = \Request::getInstance(); |
||
128 | $route = $request ? $request->getRoute() : null; |
||
129 | $controller = $route ? $route->controller : ''; |
||
130 | $parameters = $route ? $route->parameters : array(); |
||
131 | array_shift($parameters); |
||
132 | |||
133 | return array( |
||
134 | 'Original URI' => $route ? $route->uri : '', |
||
135 | 'Mapped URI' => $route ? $route->translation : '', |
||
136 | 'Controller' => $controller, |
||
137 | 'Action' => $controller ? ('action'.$route->action) : '', |
||
138 | 'HTTP Method' => $request ? $request->getInput()->getMethod() : '', |
||
139 | 'Parameters' => $parameters, |
||
140 | ); |
||
141 | }); |
||
142 | |||
143 | $this->pagehandler->addDataTableCallback('Request Parameters', function() |
||
144 | { |
||
145 | $request = \Request::getInstance(); |
||
146 | return $request ? $request->getInput()->getParam()->getContents() : ''; |
||
147 | }); |
||
148 | |||
149 | $this->pagehandler->addDataTableCallback('Permanent Session Data', function() |
||
150 | { |
||
151 | if ($application = \Application::getInstance()) |
||
152 | { |
||
153 | if ($session = $application->getSession()) |
||
154 | { |
||
155 | return $session->getContents(); |
||
156 | } |
||
157 | } |
||
158 | return 'no session active'; |
||
159 | }); |
||
160 | |||
161 | $this->pagehandler->addDataTableCallback('Flash Session Data', function() |
||
162 | { |
||
163 | if ($application = \Application::getInstance()) |
||
164 | { |
||
165 | if ($session = $application->getSession()) |
||
166 | { |
||
167 | return $session->get('flash'); |
||
168 | } |
||
169 | } |
||
170 | return 'no session active'; |
||
171 | }); |
||
172 | |||
173 | $this->pagehandler->addDataTableCallback('Defined Cookies', function() |
||
174 | { |
||
175 | $result = array(); |
||
176 | if ($input = \Input::getInstance()) |
||
177 | { |
||
178 | foreach ($input->getCookie() as $cookie) |
||
179 | { |
||
180 | $result[$cookie->getName()] = $cookie->getValue(); |
||
181 | if ($cookie->isDeleted()) |
||
182 | { |
||
183 | $result[$cookie->getName()] .= '; State=Deleted'; |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | $result[$cookie->getName()] .= '; State='.($cookie->isNew() ? 'New' : 'Request'); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | return $result; |
||
192 | }); |
||
193 | |||
194 | $this->pagehandler->addDataTableCallback('Uploaded Files', function() |
||
195 | { |
||
196 | $result = array(); |
||
197 | if ($input = \Input::getInstance()) |
||
198 | { |
||
199 | foreach ($input->getFile() as $file) |
||
200 | { |
||
201 | $result[] = $file; |
||
202 | } |
||
203 | } |
||
204 | return $result; |
||
205 | }); |
||
206 | |||
207 | $this->pagehandler->addDataTableCallback('Server Data', function() |
||
208 | { |
||
209 | return $_SERVER; |
||
210 | }); |
||
211 | |||
212 | $this->whoops->pushHandler($this->pagehandler); |
||
213 | |||
214 | // next on the stack goes the JSON handler, to deal with AJAX requests |
||
215 | if (Misc::isAjaxRequest()) |
||
216 | { |
||
217 | $jsonHandler = new JsonResponseHandler; |
||
218 | // $jsonHandler->addTraceToOutput(true); |
||
219 | $this->whoops->pushHandler($jsonHandler); |
||
220 | } |
||
221 | |||
222 | // add the Fuel production handler |
||
223 | $productionHandler = new ProductionHandler; |
||
224 | $this->whoops->pushHandler($productionHandler); |
||
225 | |||
226 | // activate the error handler |
||
227 | $this->whoops->register(); |
||
228 | |||
229 | // set a custom handler, so we can deal with translations |
||
230 | $current_handler = set_exception_handler(function($e) use(&$current_handler) |
||
231 | { |
||
232 | // get the locale |
||
233 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') |
||
234 | { |
||
235 | // if the locale is set to C, default to English |
||
236 | if (($locale = getenv('LC_ALL')) === 'C') |
||
237 | { |
||
238 | $locale = 'English'; |
||
239 | } |
||
240 | } |
||
241 | else |
||
242 | { |
||
243 | // if the locale is set to C, default to en_US |
||
244 | if (($locale = setlocale(LC_MESSAGES, null)) === 'C') |
||
245 | { |
||
246 | $locale = 'en_US'; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // get access to the exception's error message |
||
251 | $reflection = new \ReflectionClass($e); |
||
252 | $property = $reflection->getProperty("message"); |
||
253 | $property->setAccessible(true); |
||
254 | |||
255 | // get the translations for the current locale |
||
256 | if ($translations = $this->getMessages($locale, true)) |
||
257 | { |
||
258 | // does the error message exist? |
||
259 | $messageId = substr($e->getMessage(), 0,7); |
||
260 | if (isset($translations[$messageId])) |
||
261 | { |
||
262 | // swap the original message for the translated one |
||
263 | $property->setValue($e, $this->setMessage($translations[$messageId], $e->getMessage())); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | // call the original error handler with the translated exception message |
||
268 | $result = call_user_func($current_handler, $e); |
||
269 | |||
270 | // re-enable output buffering, then send the response for the handlers out |
||
271 | ob_start(); |
||
272 | echo $result; |
||
273 | }); |
||
274 | } |
||
275 | |||
338 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..