Total Complexity | 92 |
Total Lines | 336 |
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 | * Command data. |
||
13 | * |
||
14 | * @var CommandData |
||
15 | */ |
||
16 | private $commandData; |
||
17 | |||
18 | /** |
||
19 | * Filename. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | private $fileName; |
||
24 | |||
25 | /** |
||
26 | * File contents. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $fileContents; |
||
31 | |||
32 | /** |
||
33 | * Template data. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $templateData; |
||
38 | |||
39 | /** |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param CommandData $commandData Command Data. |
||
43 | */ |
||
44 | public function __construct(CommandData $commandData) |
||
45 | { |
||
46 | $this->commandData = $commandData; |
||
47 | $this->fileName = $commandData->config->pathGraphQL; |
||
48 | $this->fileContents = file_get_contents($this->fileName); |
||
49 | $this->templateData = get_artomator_template('graphql.inputs'); |
||
50 | $this->templateData = fill_template($this->commandData->dynamicVars, $this->templateData); |
||
51 | $this->templateData = fill_template($this->generateSchema(), $this->templateData); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Generate Command. |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | public function generate() |
||
60 | { |
||
61 | if (true === Str::contains($this->fileContents, $this->templateData)) { |
||
62 | $this->commandData->commandObj->info('GraphQL Inputs '.$this->commandData->config->mHumanPlural.' already exist; Skipping'); |
||
63 | |||
64 | return; |
||
65 | } |
||
66 | |||
67 | $this->fileContents .= $this->templateData; |
||
68 | |||
69 | file_put_contents($this->fileName, $this->fileContents); |
||
70 | |||
71 | $this->commandData->commandComment("\nGraphQL Inputs created"); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Rollback. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function rollback() |
||
80 | { |
||
81 | if (true === Str::contains($this->fileContents, $this->templateData)) { |
||
82 | file_put_contents($this->fileName, str_replace($this->templateData, '', $this->fileContents)); |
||
83 | $this->commandData->commandComment('GraphQL Inputs deleted'); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Sanitise the field types. |
||
89 | * |
||
90 | * @param string $fieldType Field type. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | private function sanitiseFieldTypes(string $fieldType) |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Generate Schema. |
||
182 | * |
||
183 | * @return void |
||
184 | */ |
||
185 | private function generateSchema() |
||
186 | { |
||
187 | $schema = []; |
||
188 | foreach ($this->commandData->fields as $field) { |
||
189 | if (true === $field->isFillable) { |
||
190 | if ('foreignId' === $field->fieldType) { |
||
191 | continue; |
||
192 | } else { |
||
193 | $field_type = $this->sanitiseFieldTypes($field->fieldType); |
||
194 | } |
||
195 | |||
196 | $field_type .= ((true === Str::contains($field->validations, 'required')) ? '!' : ''); |
||
197 | |||
198 | $schema[] = $field->name.': '.$field_type; |
||
199 | } |
||
200 | } |
||
201 | $schema = array_merge($schema, $this->generateRelations()); |
||
202 | $schema = implode(infy_nl_tab(1, 1), $schema); |
||
203 | $create_schema = str_replace('$TYPE$', 'Create', $schema); |
||
204 | $update_schema = str_replace('$TYPE$', 'Update', $schema); |
||
205 | $upsert_schema = str_replace('$TYPE$', 'Upsert', $schema); |
||
206 | |||
207 | return [ |
||
208 | '$CREATE_SCHEMA$' => $create_schema, |
||
209 | '$UPDATE_SCHEMA$' => str_replace('!', '', $update_schema), |
||
210 | '$UPSERT_SCHEMA$' => str_replace('!', '', $upsert_schema), |
||
211 | ]; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Generate Relations. |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | private function generateRelations() |
||
220 | { |
||
221 | $relations = []; |
||
222 | |||
223 | $count = 1; |
||
224 | $fieldsArr = []; |
||
225 | foreach ($this->commandData->relations as $relation) { |
||
226 | $field = (true === isset($relation->inputs[0])) ? $relation->inputs[0] : null; |
||
227 | |||
228 | $relationShipText = $field; |
||
229 | if (true === in_array($field, $fieldsArr)) { |
||
230 | $relationShipText = $relationShipText.'_'.$count; |
||
231 | $count++; |
||
232 | } |
||
233 | |||
234 | $relationText = $this->getRelationFunctionText($relation, $relationShipText); |
||
235 | if (false === empty($relationText)) { |
||
236 | $fieldsArr[] = $field; |
||
237 | $relations[] = $relationText; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | return $relations; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Get the relation function tex.t. |
||
246 | * |
||
247 | * @param string $relationship Relationship type. |
||
248 | * @param string|null $relationText Relationship text. |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | protected function getRelationFunctionText($relationship, $relationText = null) |
||
253 | { |
||
254 | extract($this->prepareRelationship($relationship, $relationText)); |
||
255 | |||
256 | if (false === empty($functionName)) { |
||
257 | return $this->generateRelation($functionName, $template); |
||
258 | } |
||
259 | |||
260 | return ''; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Generate Relation. |
||
265 | * |
||
266 | * @param string $functionName Function name. |
||
267 | * @param string $template Template text. |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | private function generateRelation($functionName, $template) |
||
272 | { |
||
273 | $template = str_replace('$FUNCTION_NAME$', $functionName, $template); |
||
274 | $template = str_replace('$RELATION_GRAPHQL_NAME$', ucfirst(Str::singular($functionName)), $template); |
||
275 | |||
276 | return $template; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Prepare Relationship. |
||
281 | * |
||
282 | * @param Model $relationship Relationship. |
||
283 | * @param string|null $relationText Relation Text. |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | protected function prepareRelationship($relationship, $relationText = null) |
||
345 | } |
||
346 | } |
||
347 |