Total Complexity | 92 |
Total Lines | 288 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like GraphQLInputGenerator 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.
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 GraphQLInputGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class GraphQLInputGenerator extends BaseGenerator |
||
10 | { |
||
11 | /** |
||
12 | * @var CommandData |
||
13 | */ |
||
14 | private $commandData; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $fileName; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $fileContents; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $templateData; |
||
30 | |||
31 | public function __construct(CommandData $commandData) |
||
39 | } |
||
40 | |||
41 | public function generate() |
||
54 | } |
||
55 | |||
56 | public function rollback() |
||
61 | } |
||
62 | } |
||
63 | |||
64 | private function sanitiseFieldTypes(string $fieldType) |
||
163 | } |
||
164 | } |
||
165 | |||
166 | private function generateSchema() |
||
167 | { |
||
168 | $schema = []; |
||
169 | foreach ($this->commandData->fields as $field) { |
||
170 | if ($field->isFillable) { |
||
171 | if ('foreignId' === $field->fieldType) { |
||
172 | continue; |
||
173 | } else { |
||
174 | $field_type = $this->sanitiseFieldTypes($field->fieldType); |
||
175 | } |
||
176 | |||
177 | $field_type .= (Str::contains($field->validations, 'required') ? '!' : ''); |
||
178 | |||
179 | $schema[] = $field->name.': '.$field_type; |
||
180 | } |
||
181 | } |
||
182 | $schema = array_merge($schema, $this->generateRelations()); |
||
183 | $schema = implode(infy_nl_tab(1, 1), $schema); |
||
184 | $create_schema = str_replace('$TYPE$', 'Create', $schema); |
||
185 | $update_schema = str_replace('$TYPE$', 'Update', $schema); |
||
186 | $upsert_schema = str_replace('$TYPE$', 'Upsert', $schema); |
||
187 | |||
188 | return [ |
||
189 | '$CREATE_SCHEMA$' => $create_schema, |
||
190 | '$UPDATE_SCHEMA$' => str_replace('!', '', $update_schema), |
||
191 | '$UPSERT_SCHEMA$' => str_replace('!', '', $upsert_schema), |
||
192 | ]; |
||
193 | } |
||
194 | |||
195 | private function generateRelations() |
||
196 | { |
||
197 | $relations = []; |
||
198 | |||
199 | $count = 1; |
||
200 | $fieldsArr = []; |
||
201 | foreach ($this->commandData->relations as $relation) { |
||
202 | $field = (isset($relation->inputs[0])) ? $relation->inputs[0] : null; |
||
203 | |||
204 | $relationShipText = $field; |
||
205 | if (in_array($field, $fieldsArr)) { |
||
206 | $relationShipText = $relationShipText.'_'.$count; |
||
207 | $count++; |
||
208 | } |
||
209 | |||
210 | $relationText = $this->getRelationFunctionText($relation, $relationShipText); |
||
211 | if (false === empty($relationText)) { |
||
212 | $fieldsArr[] = $field; |
||
213 | $relations[] = $relationText; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | return $relations; |
||
218 | } |
||
219 | |||
220 | protected function getRelationFunctionText($relationship, $relationText = null) |
||
221 | { |
||
222 | extract($this->prepareRelationship($relationship, $relationText)); |
||
223 | |||
224 | if (false === empty($functionName)) { |
||
225 | return $this->generateRelation($functionName, $template); |
||
226 | } |
||
227 | |||
228 | return ''; |
||
229 | } |
||
230 | |||
231 | private function generateRelation($functionName, $template) |
||
232 | { |
||
233 | $template = str_replace('$FUNCTION_NAME$', $functionName, $template); |
||
234 | $template = str_replace('$RELATION_GRAPHQL_NAME$', ucfirst(Str::singular($functionName)), $template); |
||
235 | |||
236 | return $template; |
||
237 | } |
||
238 | |||
239 | protected function prepareRelationship($relationship, $relationText = null) |
||
297 | } |
||
298 | } |
||
299 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.