Conditions | 22 |
Paths | 58 |
Total Lines | 82 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 506 |
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 |
||
95 | public function afterExecuteRoute(Dispatcher $dispatcher): void |
||
96 | { |
||
97 | // Merge response into view variables |
||
98 | $response = $dispatcher->getReturnedValue(); |
||
99 | |||
100 | // Quiet output |
||
101 | $quiet = $this->dispatcher->getParam('quiet'); |
||
102 | if ($quiet) { |
||
103 | exit(!$response ? 1 : 0); |
||
1 ignored issue
–
show
|
|||
104 | } |
||
105 | |||
106 | // Normalize response |
||
107 | if (is_array($response)) { |
||
108 | $this->view->setVars($response, true); |
||
109 | } |
||
110 | else { |
||
111 | $this->view->setVars((array)$response, true); |
||
112 | } |
||
113 | $normalizedResponse = $this->normalizeResponse((bool)$response); |
||
114 | $dispatcher->setReturnedValue($normalizedResponse); |
||
115 | |||
116 | // Set response |
||
117 | $verbose = $this->dispatcher->getParam('verbose'); |
||
118 | $ret = $verbose ? $normalizedResponse : $response; |
||
119 | |||
120 | // Format response |
||
121 | $format = $this->dispatcher->getParam('format'); |
||
122 | $format ??= 'json'; |
||
123 | switch (strtolower($format)) { |
||
124 | case 'dump': |
||
125 | dump($ret); |
||
126 | break; |
||
127 | |||
128 | case 'var_dump': |
||
129 | var_dump($ret); |
||
130 | break; |
||
131 | |||
132 | case 'var_export': |
||
133 | var_export($ret); |
||
134 | break; |
||
135 | |||
136 | case 'print_r': |
||
137 | print_r($ret); |
||
138 | break; |
||
139 | |||
140 | case 'serialize': |
||
141 | echo serialize($ret); |
||
142 | break; |
||
143 | |||
144 | case 'json': |
||
145 | echo json_encode($ret, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
||
146 | break; |
||
147 | |||
148 | case 'string': |
||
149 | if (is_string($ret)) { |
||
150 | echo $ret; |
||
151 | } |
||
152 | elseif (is_bool($ret)) { |
||
153 | echo $ret? 'true' : 'false'; |
||
154 | } |
||
155 | elseif (is_null($ret)) { |
||
156 | echo 'null'; |
||
157 | } |
||
158 | elseif (is_numeric($ret)) { |
||
159 | echo $ret; |
||
160 | } |
||
161 | else { |
||
162 | echo json_encode($ret, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
||
163 | } |
||
164 | break; |
||
165 | |||
166 | case 'raw': |
||
167 | if (is_string($ret) || is_bool($ret) || is_null($ret) || is_numeric($ret)) { |
||
168 | echo $ret; |
||
169 | } |
||
170 | else { |
||
171 | echo json_encode($ret, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
||
172 | } |
||
173 | break; |
||
174 | |||
175 | default: |
||
176 | throw new CliException('Unknown output format `' . $format . '` expected one of the string value: `json` `serialize` `dump` `raw`'); |
||
177 | } |
||
180 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.