Total Complexity | 53 |
Total Lines | 357 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like QueryTrait 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 QueryTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | trait QueryTrait |
||
6 | { |
||
7 | private static $operationNamePlaceholder = '_operationNamePlaceholder_'; |
||
8 | |||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | public $operationName; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | public $variables = []; |
||
18 | |||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | public $isRootQuery = true; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | public $type = []; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | public $args = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | public $fields = []; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | public $skipIf = []; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | public $includeIf = []; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | public $fragments = []; |
||
53 | |||
54 | /** |
||
55 | * @param null $type |
||
|
|||
56 | * @param array $args |
||
57 | * @param array $fields |
||
58 | * |
||
59 | * @return self |
||
60 | */ |
||
61 | public static function create($type = null, array $args = [], array $fields = []) |
||
62 | { |
||
63 | return new self($type, $args, $fields); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string $operationName |
||
68 | * |
||
69 | * @return self |
||
70 | */ |
||
71 | public function operationName(string $operationName) |
||
72 | { |
||
73 | $this->operationName = $operationName; |
||
74 | |||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param $operationName |
||
80 | * @param $variables |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | private static function printQuery($operationName, $variables): string |
||
85 | { |
||
86 | if (null === $operationName) { |
||
87 | if (\count($variables)) { |
||
88 | $operationName = static::$operationNamePlaceholder; |
||
89 | } else { |
||
90 | return ''; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return sprintf('%s %s %s', static::KEYWORD, $operationName, static::printVariables($variables)); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param array $variables |
||
99 | * |
||
100 | * @return self |
||
101 | */ |
||
102 | public function variables(array $variables = []) |
||
103 | { |
||
104 | foreach ($variables as $variableName => $variableType) { |
||
105 | $this->variables[(string) $variableName] = (string) $variableType; |
||
106 | } |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param array $value |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | private static function printVariables(array $value): string |
||
117 | { |
||
118 | if (!\count($value)) { |
||
119 | return ''; |
||
120 | } |
||
121 | |||
122 | $variables = []; |
||
123 | |||
124 | foreach ($value as $variableName => $variableType) { |
||
125 | $variables[] = sprintf('%s: %s', $variableName, $variableType); |
||
126 | } |
||
127 | |||
128 | return sprintf('(%s)', implode(', ', $variables)); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param array $args |
||
133 | * |
||
134 | * @return self |
||
135 | */ |
||
136 | public function arguments(array $args = []) |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param array $value |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | private static function printArgs(array $value): string |
||
153 | { |
||
154 | if (!count($value)) { |
||
155 | return ''; |
||
156 | } |
||
157 | |||
158 | $args = []; |
||
159 | foreach ($value as $argName => $argValue) { |
||
160 | if (\is_string($argValue) && '$' !== $argValue[0]) { |
||
161 | $argValue = sprintf('"%s"', $argValue); |
||
162 | } |
||
163 | |||
164 | if (\is_bool($argValue) || \is_float($argValue)) { |
||
165 | $argValue = var_export($argValue, true); |
||
166 | } |
||
167 | |||
168 | $args[] = sprintf('%s: %s', $argName, $argValue); |
||
169 | } |
||
170 | |||
171 | return sprintf('(%s)', implode(', ', $args)); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param array $fields |
||
176 | * |
||
177 | * @return self |
||
178 | */ |
||
179 | public function fields(array $fields = []) |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @param array $fields |
||
203 | * |
||
204 | * @return self |
||
205 | */ |
||
206 | public function removeFields(array $fields = []): Query |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @param array $value |
||
217 | * @param array $skipIf |
||
218 | * @param array $includeIf |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | private static function printFields(array $value, array $skipIf = [], array $includeIf = []): string |
||
223 | { |
||
224 | $fields = []; |
||
225 | |||
226 | foreach ($value as $fieldAlias => $field) { |
||
227 | $directive = ''; |
||
228 | |||
229 | if (\is_string($field)) { |
||
230 | if ($fieldAlias !== $field) { |
||
231 | if (array_key_exists($fieldAlias, $skipIf)) { |
||
232 | $directive = sprintf('@skip(if: %s)', $skipIf[$fieldAlias]); |
||
233 | } elseif (array_key_exists($fieldAlias, $includeIf)) { |
||
234 | $directive = sprintf('@include(if: %s)', $includeIf[$fieldAlias]); |
||
235 | } |
||
236 | |||
237 | $fields[] = sprintf('%s: %s %s', $fieldAlias, $field, $directive); |
||
238 | } else { |
||
239 | if (array_key_exists($field, $skipIf)) { |
||
240 | $directive = sprintf('@skip(if: %s)', $skipIf[$field]); |
||
241 | } elseif (array_key_exists($field, $includeIf)) { |
||
242 | $directive = sprintf('@include(if: %s)', $includeIf[$field]); |
||
243 | } |
||
244 | |||
245 | $fields[] = sprintf('%s %s', $field, $directive); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | if ($field instanceof self) { |
||
250 | $field->isRootQuery = false; |
||
251 | |||
252 | if (array_key_exists($fieldAlias, $skipIf)) { |
||
253 | $directive = sprintf('@skip(if: %s)', $skipIf[$fieldAlias]); |
||
254 | } elseif (array_key_exists($fieldAlias, $includeIf)) { |
||
255 | $directive = sprintf('@include(if: %s)', $includeIf[$fieldAlias]); |
||
256 | } |
||
257 | |||
258 | if (null !== $field->type) { |
||
259 | $fieldAlias = sprintf('%s: %s', $fieldAlias, $field->type); |
||
260 | } |
||
261 | |||
262 | $fields[] = sprintf('%s %s { %s }', $fieldAlias, $directive, static::printFields($field->fields)); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | return implode(', ', $fields); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param array $values |
||
271 | * |
||
272 | * @return self |
||
273 | */ |
||
274 | public function skipIf(array $values = []) |
||
275 | { |
||
276 | foreach ($values as $field => $argument) { |
||
277 | $this->skipIf[$field] = $argument; |
||
278 | } |
||
279 | |||
280 | return $this; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param array $values |
||
285 | * |
||
286 | * @return self |
||
287 | */ |
||
288 | public function includeIf(array $values = []) |
||
289 | { |
||
290 | foreach ($values as $field => $argument) { |
||
291 | $this->includeIf[$field] = $argument; |
||
292 | } |
||
293 | |||
294 | return $this; |
||
295 | } |
||
296 | |||
297 | public function __toString() |
||
298 | { |
||
299 | if ($this->isRootQuery) { |
||
300 | $query = sprintf('%s { %s %s { %s } } %s', static::printQuery($this->operationName, $this->variables), static::printType($this->type), static::printArgs($this->args), static::printFields($this->fields, $this->skipIf, $this->includeIf), static::printFragments($this->fragments)); |
||
301 | } else { |
||
302 | $query = sprintf('%s { %s }', static::printType($this->type), static::printFields($this->fields, $this->skipIf, $this->includeIf)); |
||
303 | } |
||
304 | |||
305 | $query = \GraphQL\Language\Printer::doPrint(\GraphQL\Language\Parser::parse((string) $query)); |
||
306 | |||
307 | $query = str_replace(static::$operationNamePlaceholder, static::GENERATED_NAME_PREFIX.sha1($query), $query); |
||
308 | |||
309 | return $query; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param Fragment $fragment |
||
314 | * |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function addFragment(Fragment $fragment) |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param $value |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | private function printFragments($value) |
||
330 | { |
||
331 | $fragments = ''; |
||
332 | foreach ($value as $fragment) { |
||
333 | $fragments .= (string) $fragment; |
||
334 | } |
||
335 | |||
336 | return $fragments; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @param null $type |
||
341 | * @param array $args |
||
342 | * @param array $fields |
||
343 | */ |
||
344 | private function __construct($type = null, array $args = [], array $fields = []) |
||
351 | } |
||
352 | |||
353 | private static function printType($value): string |
||
362 | } |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 |