| Conditions | 19 |
| Paths | 575 |
| Total Lines | 85 |
| 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 |
||
| 109 | protected function getTransformerResponse($tags) |
||
| 110 | { |
||
| 111 | try { |
||
| 112 | $transFormerTags = array_filter($tags, function ($tag) { |
||
| 113 | if (! ($tag instanceof Tag)) { |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | return \in_array(\strtolower($tag->getName()), ['transformer', 'transformercollection']); |
||
| 118 | }); |
||
| 119 | if (empty($transFormerTags)) { |
||
| 120 | // we didn't have any of the tags so goodbye |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | |||
| 124 | $modelTag = array_first(array_filter($tags, function ($tag) { |
||
| 125 | if (! ($tag instanceof Tag)) { |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | |||
| 129 | return \in_array(\strtolower($tag->getName()), ['transformermodel']); |
||
| 130 | })); |
||
| 131 | $tag = \array_first($transFormerTags); |
||
| 132 | $transformer = $tag->getContent(); |
||
| 133 | if (! \class_exists($transformer)) { |
||
| 134 | // if we can't find the transformer we can't generate a response |
||
| 135 | return; |
||
| 136 | } |
||
| 137 | $demoData = []; |
||
| 138 | |||
| 139 | $reflection = new ReflectionClass($transformer); |
||
| 140 | $method = $reflection->getMethod('transform'); |
||
| 141 | $parameter = \array_first($method->getParameters()); |
||
| 142 | $type = null; |
||
| 143 | if ($modelTag) { |
||
| 144 | $type = $modelTag->getContent(); |
||
| 145 | } |
||
| 146 | if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) { |
||
| 147 | // we can only get the type with reflection for PHP 7 |
||
| 148 | if ($parameter->hasType() && |
||
| 149 | ! $parameter->getType()->isBuiltin() && |
||
| 150 | \class_exists((string) $parameter->getType())) { |
||
| 151 | //we have a type |
||
| 152 | $type = (string) $parameter->getType(); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | if ($type) { |
||
| 156 | // we have a class so we try to create an instance |
||
| 157 | $demoData = new $type; |
||
| 158 | try { |
||
| 159 | // try a factory |
||
| 160 | $demoData = \factory($type)->make(); |
||
| 161 | } catch (\Exception $e) { |
||
| 162 | if ($demoData instanceof \Illuminate\Database\Eloquent\Model) { |
||
| 163 | // we can't use a factory but can try to get one from the database |
||
| 164 | try { |
||
| 165 | // check if we can find one |
||
| 166 | $newDemoData = $type::first(); |
||
| 167 | if ($newDemoData) { |
||
| 168 | $demoData = $newDemoData; |
||
| 169 | } |
||
| 170 | } catch (\Exception $e) { |
||
| 171 | // do nothing |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | $fractal = new Manager(); |
||
| 178 | $resource = []; |
||
| 179 | if ($tag->getName() == 'transformer') { |
||
| 180 | // just one |
||
| 181 | $resource = new Item($demoData, new $transformer); |
||
| 182 | } |
||
| 183 | if ($tag->getName() == 'transformercollection') { |
||
| 184 | // a collection |
||
| 185 | $resource = new Collection([$demoData, $demoData], new $transformer); |
||
| 186 | } |
||
| 187 | |||
| 188 | return \response($fractal->createData($resource)->toJson()); |
||
| 189 | } catch (\Exception $e) { |
||
| 190 | // it isn't possible to parse the transformer |
||
| 191 | return; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 |
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.