Complex classes like ControllerQueryProvider 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 ControllerQueryProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class ControllerQueryProvider implements QueryProviderInterface |
||
42 | { |
||
43 | /** |
||
44 | * @var object |
||
45 | */ |
||
46 | private $controller; |
||
47 | /** |
||
48 | * @var Reader |
||
49 | */ |
||
50 | private $annotationReader; |
||
51 | /** |
||
52 | * @var TypeMapperInterface |
||
53 | */ |
||
54 | private $typeMapper; |
||
55 | /** |
||
56 | * @var HydratorInterface |
||
57 | */ |
||
58 | private $hydrator; |
||
59 | /** |
||
60 | * @var AuthenticationServiceInterface |
||
61 | */ |
||
62 | private $authenticationService; |
||
63 | /** |
||
64 | * @var AuthorizationServiceInterface |
||
65 | */ |
||
66 | private $authorizationService; |
||
67 | /** |
||
68 | * @var ContainerInterface |
||
69 | */ |
||
70 | private $registry; |
||
71 | |||
72 | /** |
||
73 | * @param object $controller |
||
74 | */ |
||
75 | public function __construct($controller, Reader $annotationReader, TypeMapperInterface $typeMapper, HydratorInterface $hydrator, AuthenticationServiceInterface $authenticationService, AuthorizationServiceInterface $authorizationService, ?ContainerInterface $container = null) |
||
85 | |||
86 | /** |
||
87 | * @return Field[] |
||
88 | */ |
||
89 | public function getQueries(): array |
||
93 | |||
94 | /** |
||
95 | * @return Field[] |
||
96 | */ |
||
97 | public function getMutations(): array |
||
101 | |||
102 | /** |
||
103 | * @return Field[] |
||
104 | */ |
||
105 | private function getFieldsByAnnotations(string $annotationName): array |
||
147 | |||
148 | /** |
||
149 | * Checks the @Logged and @Right annotations. |
||
150 | * |
||
151 | * @param \ReflectionMethod $reflectionMethod |
||
152 | * @return bool |
||
153 | */ |
||
154 | private function isAuthorized(\ReflectionMethod $reflectionMethod) : bool |
||
171 | |||
172 | /** |
||
173 | * Note: there is a bug in $refMethod->allowsNull that forces us to use $standardRefMethod->allowsNull instead. |
||
174 | * |
||
175 | * @param ReflectionMethod $refMethod |
||
176 | * @param \ReflectionMethod $standardRefMethod |
||
177 | * @return array |
||
178 | * @throws MissingTypeHintException |
||
179 | */ |
||
180 | private function mapParameters(ReflectionMethod $refMethod, \ReflectionMethod $standardRefMethod) |
||
181 | { |
||
182 | $args = []; |
||
183 | |||
184 | $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
||
185 | |||
186 | foreach ($standardRefMethod->getParameters() as $standardParameter) { |
||
187 | $allowsNull = $standardParameter->allowsNull(); |
||
188 | $parameter = $refMethod->getParameter($standardParameter->getName()); |
||
189 | |||
190 | $type = (string) $parameter->getType(); |
||
191 | if ($type === '') { |
||
192 | throw MissingTypeHintException::missingTypeHint($parameter); |
||
193 | } |
||
194 | $phpdocType = $typeResolver->resolve($type); |
||
195 | |||
196 | try { |
||
197 | $arr = [ |
||
198 | 'type' => $this->mapType($phpdocType, $parameter->getDocBlockTypes(), $allowsNull || $parameter->isDefaultValueAvailable(), true), |
||
199 | ]; |
||
200 | } catch (TypeMappingException $e) { |
||
201 | throw TypeMappingException::wrapWithParamInfo($e, $parameter); |
||
202 | } |
||
203 | |||
204 | if ($standardParameter->allowsNull()) { |
||
205 | $arr['default'] = null; |
||
206 | } |
||
207 | if ($standardParameter->isDefaultValueAvailable()) { |
||
208 | $arr['default'] = $standardParameter->getDefaultValue(); |
||
209 | } |
||
210 | |||
211 | $args[$parameter->getName()] = $arr; |
||
212 | } |
||
213 | |||
214 | return $args; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param Type $type |
||
219 | * @param Type[] $docBlockTypes |
||
220 | * @return TypeInterface |
||
221 | */ |
||
222 | private function mapType(Type $type, array $docBlockTypes, bool $isNullable, bool $mapToInputType): TypeInterface |
||
251 | |||
252 | /** |
||
253 | * Casts a Type to a GraphQL type. |
||
254 | * Does not deal with nullable. |
||
255 | * |
||
256 | * @param Type $type |
||
257 | * @param bool $mapToInputType |
||
258 | * @return TypeInterface |
||
259 | */ |
||
260 | private function toGraphQlType(Type $type, bool $mapToInputType): TypeInterface |
||
290 | |||
291 | /** |
||
292 | * Removes "null" from the list of types. |
||
293 | * |
||
294 | * @param Type[] $docBlockTypeHints |
||
295 | * @return array |
||
296 | */ |
||
297 | private function typesWithoutNullable(array $docBlockTypeHints): array |
||
303 | |||
304 | /** |
||
305 | * @param Type[] $docBlockTypeHints |
||
306 | * @return bool |
||
307 | */ |
||
308 | private function isNullable(array $docBlockTypeHints): bool |
||
317 | } |
||
318 |
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: