Conditions | 15 |
Paths | 58 |
Total Lines | 53 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 240 |
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 |
||
124 | public function afterExecuteRoute(Dispatcher $dispatcher) |
||
125 | { |
||
126 | // Merge response into view variables |
||
127 | $response = $dispatcher->getReturnedValue(); |
||
128 | |||
129 | // Quiet output |
||
130 | $quiet = $this->dispatcher->getParam('quiet'); |
||
131 | if ($quiet) { |
||
132 | exit(!$response? 1 : 0); |
||
1 ignored issue
–
show
|
|||
133 | } |
||
134 | |||
135 | // Normalize response |
||
136 | if (is_array($response)) { |
||
137 | $this->view->setVars($response, true); |
||
138 | } |
||
139 | $normalizedResponse = $this->normalizeResponse(is_array($response) ? null : $response); |
||
140 | $dispatcher->setReturnedValue($normalizedResponse); |
||
141 | |||
142 | // Set response |
||
143 | $verbose = $this->dispatcher->getParam('verbose'); |
||
144 | $ret = $verbose? $normalizedResponse : $response; |
||
145 | |||
146 | // Format response |
||
147 | $format = $this->dispatcher->getParam('format'); |
||
148 | $format ??= 'dump'; |
||
149 | |||
150 | switch (strtolower($format)) { |
||
151 | case 'dump': |
||
152 | dd($ret); |
||
153 | case 'json': |
||
154 | echo json_encode($ret); |
||
155 | break; |
||
156 | case 'xml': |
||
157 | $xml = new \SimpleXMLElement(''); |
||
158 | array_walk_recursive($ret, array ($xml,'addChild')); |
||
159 | echo $xml->asXML(); |
||
160 | break; |
||
161 | case 'serialize': |
||
162 | echo serialize($ret); |
||
163 | break; |
||
164 | case 'raw': |
||
165 | if (is_array($ret) || is_object($ret)) { |
||
166 | print_r($ret); |
||
167 | } |
||
168 | else if (is_bool($ret)) { |
||
169 | echo $ret? 'true' : 'false'; |
||
170 | } |
||
171 | else { |
||
172 | echo strval($ret); |
||
173 | } |
||
174 | break; |
||
175 | default: |
||
176 | throw new \Exception('Unknown output format `'.$format.'` expected one of the string value: `json` `xml` `serialize` `dump` `raw`'); |
||
177 | } |
||
180 |