Conditions | 19 |
Paths | 575 |
Total Lines | 85 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
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 |
||
158 | protected function getTransformerResponse($tags) |
||
159 | { |
||
160 | try { |
||
161 | $transFormerTags = array_filter($tags, function ($tag) { |
||
162 | if (! ($tag instanceof Tag)) { |
||
163 | return false; |
||
164 | } |
||
165 | |||
166 | return \in_array(\strtolower($tag->getName()), ['transformer', 'transformercollection']); |
||
167 | }); |
||
168 | if (empty($transFormerTags)) { |
||
169 | // we didn't have any of the tags so goodbye |
||
170 | return false; |
||
171 | } |
||
172 | |||
173 | $modelTag = array_first(array_filter($tags, function ($tag) { |
||
174 | if (! ($tag instanceof Tag)) { |
||
175 | return false; |
||
176 | } |
||
177 | |||
178 | return \in_array(\strtolower($tag->getName()), ['transformermodel']); |
||
179 | })); |
||
180 | $tag = \array_first($transFormerTags); |
||
181 | $transformer = $tag->getContent(); |
||
182 | if (! \class_exists($transformer)) { |
||
183 | // if we can't find the transformer we can't generate a response |
||
184 | return; |
||
185 | } |
||
186 | $demoData = []; |
||
187 | |||
188 | $reflection = new ReflectionClass($transformer); |
||
189 | $method = $reflection->getMethod('transform'); |
||
190 | $parameter = \array_first($method->getParameters()); |
||
191 | $type = null; |
||
192 | if ($modelTag) { |
||
193 | $type = $modelTag->getContent(); |
||
194 | } |
||
195 | if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) { |
||
196 | // we can only get the type with reflection for PHP 7 |
||
197 | if ($parameter->hasType() && |
||
198 | ! $parameter->getType()->isBuiltin() && |
||
199 | \class_exists((string) $parameter->getType())) { |
||
200 | //we have a type |
||
201 | $type = (string) $parameter->getType(); |
||
202 | } |
||
203 | } |
||
204 | if ($type) { |
||
205 | // we have a class so we try to create an instance |
||
206 | $demoData = new $type; |
||
207 | try { |
||
208 | // try a factory |
||
209 | $demoData = \factory($type)->make(); |
||
210 | } catch (\Exception $e) { |
||
211 | if ($demoData instanceof \Illuminate\Database\Eloquent\Model) { |
||
212 | // we can't use a factory but can try to get one from the database |
||
213 | try { |
||
214 | // check if we can find one |
||
215 | $newDemoData = $type::first(); |
||
216 | if ($newDemoData) { |
||
217 | $demoData = $newDemoData; |
||
218 | } |
||
219 | } catch (\Exception $e) { |
||
220 | // do nothing |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | $fractal = new Manager(); |
||
227 | $resource = []; |
||
228 | if ($tag->getName() == 'transformer') { |
||
229 | // just one |
||
230 | $resource = new Item($demoData, new $transformer); |
||
231 | } |
||
232 | if ($tag->getName() == 'transformercollection') { |
||
233 | // a collection |
||
234 | $resource = new Collection([$demoData, $demoData], new $transformer); |
||
235 | } |
||
236 | |||
237 | return \response($fractal->createData($resource)->toJson()); |
||
238 | } catch (\Exception $e) { |
||
239 | // it isn't possible to parse the transformer |
||
240 | return; |
||
241 | } |
||
242 | } |
||
243 | |||
281 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.