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