Complex classes like LaravelGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LaravelGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class LaravelGenerator extends AbstractGenerator |
||
15 | { |
||
16 | /** |
||
17 | * @param \Illuminate\Routing\Route $route |
||
18 | * @param array $bindings |
||
19 | * @param array $headers |
||
20 | * @param bool $withResponse |
||
21 | * |
||
22 | * @return array |
||
23 | */ |
||
24 | public function processRoute($route, $bindings = [], $headers = [], $withResponse = true) |
||
25 | { |
||
26 | $content = ''; |
||
27 | |||
28 | $routeAction = $route->getAction(); |
||
29 | $routeGroup = $this->getRouteGroup($routeAction['uses']); |
||
30 | $routeDescription = $this->getRouteDescription($routeAction['uses']); |
||
31 | $showresponse = null; |
||
32 | |||
33 | if ($withResponse) { |
||
34 | $response = null; |
||
35 | $docblockResponse = $this->getDocblockResponse($routeDescription['tags']); |
||
36 | if ($docblockResponse) { |
||
37 | // we have a response from the docblock ( @response ) |
||
38 | $response = $docblockResponse; |
||
39 | $showresponse = true; |
||
40 | } |
||
41 | if (! $response) { |
||
42 | $transformerResponse = $this->getTransformerResponse($routeDescription['tags']); |
||
43 | if ($transformerResponse) { |
||
44 | // we have a transformer response from the docblock ( @transformer || @transformercollection ) |
||
45 | $response = $transformerResponse; |
||
46 | $showresponse = true; |
||
47 | } |
||
48 | } |
||
49 | if (! $response) { |
||
50 | $responseClassResponse = $this->getResponseClassResponse($routeDescription['tags']); |
||
51 | if ($responseClassResponse) { |
||
52 | // we have a class response from the docblock ( @responseClass ) |
||
53 | $response = $responseClassResponse; |
||
54 | $showresponse = true; |
||
55 | } |
||
56 | } |
||
57 | if (! $response) { |
||
58 | $dataResponse = $this->getDataResponse($routeDescription['tags']); |
||
59 | if ($dataResponse) { |
||
60 | // we have a data response from the docblock ( @data ) |
||
61 | $response = $dataResponse; |
||
62 | $showresponse = true; |
||
63 | } |
||
64 | } |
||
65 | if (! $response) { |
||
66 | $response = $this->getRouteResponse($route, $bindings, $headers); |
||
67 | } |
||
68 | if ($response->headers->get('Content-Type') === 'application/json') { |
||
69 | $content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT); |
||
70 | } else { |
||
71 | $content = $response->getContent(); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return $this->getParameters([ |
||
76 | 'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))), |
||
77 | 'resource' => $routeGroup, |
||
78 | 'title' => $routeDescription['short'], |
||
79 | 'description' => $routeDescription['long'], |
||
80 | 'methods' => $this->getMethods($route), |
||
81 | 'uri' => $this->getUri($route), |
||
82 | 'parameters' => [], |
||
83 | 'response' => $content, |
||
84 | 'showresponse' => $showresponse, |
||
85 | ], $routeAction, $bindings); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get a response from the transformer tags. |
||
90 | * |
||
91 | * @param array $tags |
||
92 | * |
||
93 | * @return mixed |
||
94 | */ |
||
95 | protected function getTransformerResponse($tags) |
||
96 | { |
||
97 | try { |
||
98 | $transFormerTag = $this->getFirstTagFromDocblock($tags, ['transformer', 'transformercollection']); |
||
99 | |||
100 | if (empty($transFormerTag) || count($transFormerTag) == 0) { |
||
101 | // we didn't have any of the tags so goodbye |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | $modelTag = $this->getFirstTagFromDocblock($tags, ['transformermodel']); |
||
106 | $transformer = $transFormerTag->getContent(); |
||
107 | if (! \class_exists($transformer)) { |
||
108 | // if we can't find the transformer we can't generate a response |
||
109 | return; |
||
110 | } |
||
111 | $demoData = []; |
||
112 | |||
113 | $reflection = new ReflectionClass($transformer); |
||
114 | $method = $reflection->getMethod('transform'); |
||
115 | $parameter = \array_first($method->getParameters()); |
||
116 | $type = null; |
||
117 | if ($modelTag) { |
||
118 | $type = $modelTag->getContent(); |
||
119 | } |
||
120 | if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) { |
||
121 | // we can only get the type with reflection for PHP 7 |
||
122 | if ($parameter->hasType() && ! $parameter->getType()->isBuiltin() && \class_exists((string) $parameter->getType())) { |
||
123 | //we have a type |
||
124 | $type = (string) $parameter->getType(); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // if we have modelTag |
||
129 | if ($type) { |
||
|
|||
130 | // we have a class so we try to create an instance |
||
131 | $demoData = new $type; |
||
132 | try { |
||
133 | // try a factory |
||
134 | $demoData = \factory($type)->make(); |
||
135 | } catch (\Exception $e) { |
||
136 | if ($demoData instanceof \Illuminate\Database\Eloquent\Model) { |
||
137 | // we can't use a factory but can try to get one from the database |
||
138 | try { |
||
139 | // check if we can find one |
||
140 | $newDemoData = $type::first(); |
||
141 | if ($newDemoData) { |
||
142 | $demoData = $newDemoData; |
||
143 | } |
||
144 | } catch (\Exception $e) { |
||
145 | // do nothing |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | } else { |
||
150 | // or try get data use ( @data ) tag |
||
151 | $demoData = $this->getDataTag($tags); |
||
152 | } |
||
153 | |||
154 | $serializerTags = $this->getFirstTagFromDocblock($tags, 'serializer'); |
||
155 | |||
156 | if (is_object($serializerTags)) { |
||
157 | $serializer = $serializerTags->getContent(); |
||
158 | } |
||
159 | |||
160 | $fractal = new Manager(); |
||
161 | $resource = []; |
||
162 | // allow use custom serializer |
||
163 | if (! empty($serializer) && class_exists($serializer)) { |
||
164 | $fractal->setSerializer(new $serializer()); |
||
165 | } |
||
166 | if ($transFormerTag->getName() == 'transformer') { |
||
167 | // just one |
||
168 | $resource = new Item($demoData, new $transformer); |
||
169 | } |
||
170 | if ($transFormerTag->getName() == 'transformercollection') { |
||
171 | // a collection |
||
172 | $resource = new Collection([$demoData, $demoData], new $transformer); |
||
173 | } |
||
174 | |||
175 | return \response($fractal->createData($resource)->toJson()); |
||
176 | } catch (\Exception $e) { |
||
177 | // it isn't possible to parse the transformer |
||
178 | return; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Get response content use responseclass tag. |
||
184 | * |
||
185 | * @param $tags |
||
186 | * |
||
187 | * @return bool|void |
||
188 | */ |
||
189 | protected function getResponseClassResponse($tags) |
||
190 | { |
||
191 | try { |
||
192 | $responseClassTag = $this->getFirstTagFromDocblock($tags, ['responseclass']); |
||
193 | |||
194 | if (empty($responseClassTag) || count($responseClassTag) == 0) { |
||
195 | // we didn't have any of the tags so goodbye |
||
196 | return false; |
||
197 | } |
||
198 | |||
199 | $responseClass = $responseClassTag->getContent(); |
||
200 | if (! \class_exists($responseClass)) { |
||
201 | // if we can't find the response class we can't generate a response |
||
202 | return; |
||
203 | } |
||
204 | |||
205 | $demoData = new $responseClass($this->getDataTag($tags, null)); |
||
206 | |||
207 | return \response($demoData); |
||
208 | } catch (\Exception $exception) { |
||
209 | return; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param Route $route |
||
215 | * |
||
216 | * @return mixed |
||
217 | */ |
||
218 | public function getUri($route) |
||
226 | |||
227 | /** |
||
228 | * @param Route $route |
||
229 | * |
||
230 | * @return mixed |
||
231 | */ |
||
232 | public function getMethods($route) |
||
240 | |||
241 | /** |
||
242 | * Prepares / Disables route middlewares. |
||
243 | * |
||
244 | * @param bool $disable |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | public function prepareMiddleware($disable = true) |
||
252 | |||
253 | /** |
||
254 | * Call the given URI and return the Response. |
||
255 | * |
||
256 | * @param string $method |
||
257 | * @param string $uri |
||
258 | * @param array $parameters |
||
259 | * @param array $cookies |
||
260 | * @param array $files |
||
261 | * @param array $server |
||
262 | * @param string $content |
||
263 | * |
||
264 | * @return \Illuminate\Http\Response |
||
265 | */ |
||
266 | public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) |
||
287 | |||
288 | /** |
||
289 | * @param string $route |
||
290 | * @param array $bindings |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | protected function getRouteRules($route, $bindings) |
||
323 | |||
324 | /** |
||
325 | * Get Custom Data from data tag. |
||
326 | * |
||
327 | * @param $tags |
||
328 | * @param array $default |
||
329 | * |
||
330 | * @return array|null |
||
331 | */ |
||
332 | protected function getDataTag($tags, $default = []) |
||
349 | |||
350 | /** |
||
351 | * Get Response use @data tag. |
||
352 | * |
||
353 | * @param $tags |
||
354 | * |
||
355 | * @return bool|\Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
||
356 | */ |
||
357 | protected function getDataResponse($tags) |
||
363 | } |
||
364 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: