Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
32 | class ControllerQueryProvider implements QueryProviderInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var object |
||
36 | */ |
||
37 | private $controller; |
||
38 | /** |
||
39 | * @var Reader |
||
40 | */ |
||
41 | private $annotationReader; |
||
42 | /** |
||
43 | * @var TypeMapperInterface |
||
44 | */ |
||
45 | private $typeMapper; |
||
46 | /** |
||
47 | * @var HydratorInterface |
||
48 | */ |
||
49 | private $hydrator; |
||
50 | /** |
||
51 | * @var AuthenticationServiceInterface |
||
52 | */ |
||
53 | private $authenticationService; |
||
54 | /** |
||
55 | * @var AuthorizationServiceInterface |
||
56 | */ |
||
57 | private $authorizationService; |
||
58 | |||
59 | /** |
||
60 | * @param object $controller |
||
61 | */ |
||
62 | View Code Duplication | public function __construct($controller, Reader $annotationReader, TypeMapperInterface $typeMapper, HydratorInterface $hydrator, AuthenticationServiceInterface $authenticationService, AuthorizationServiceInterface $authorizationService) |
|
|
|||
63 | { |
||
64 | $this->controller = $controller; |
||
65 | $this->annotationReader = $annotationReader; |
||
66 | $this->typeMapper = $typeMapper; |
||
67 | $this->hydrator = $hydrator; |
||
68 | $this->authenticationService = $authenticationService; |
||
69 | $this->authorizationService = $authorizationService; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return Field[] |
||
74 | */ |
||
75 | public function getQueries(): array |
||
79 | |||
80 | /** |
||
81 | * @return Field[] |
||
82 | */ |
||
83 | public function getMutations(): array |
||
87 | |||
88 | /** |
||
89 | * @return Field[] |
||
90 | */ |
||
91 | private function getFieldsByAnnotations(string $annotationName): array |
||
119 | |||
120 | /** |
||
121 | * Checks the @Logged and @Right annotations. |
||
122 | * |
||
123 | * @param \ReflectionMethod $reflectionMethod |
||
124 | * @return bool |
||
125 | */ |
||
126 | private function isAuthorized(\ReflectionMethod $reflectionMethod) : bool |
||
143 | |||
144 | /** |
||
145 | * Note: there is a bug in $refMethod->allowsNull that forces us to use $standardRefMethod->allowsNull instead. |
||
146 | * |
||
147 | * @param ReflectionMethod $refMethod |
||
148 | * @param \ReflectionMethod $standardRefMethod |
||
149 | * @return array |
||
150 | */ |
||
151 | private function mapParameters(ReflectionMethod $refMethod, \ReflectionMethod $standardRefMethod) |
||
162 | |||
163 | /** |
||
164 | * @param Type $type |
||
165 | * @param Type[] $docBlockTypes |
||
166 | * @return TypeInterface |
||
167 | */ |
||
168 | private function mapType(Type $type, array $docBlockTypes, bool $isNullable): TypeInterface |
||
198 | |||
199 | /** |
||
200 | * Casts a Type to a GraphQL type. |
||
201 | * Does not deal with nullable. |
||
202 | * |
||
203 | * @param Type $type |
||
204 | * @return TypeInterface |
||
205 | */ |
||
206 | private function toGraphQlType(Type $type): TypeInterface |
||
220 | |||
221 | /** |
||
222 | * Removes "null" from the list of types. |
||
223 | * |
||
224 | * @param Type[] $docBlockTypeHints |
||
225 | * @return array |
||
226 | */ |
||
227 | private function typesWithoutNullable(array $docBlockTypeHints): array |
||
233 | |||
234 | /** |
||
235 | * @param Type[] $docBlockTypeHints |
||
236 | * @return bool |
||
237 | */ |
||
238 | private function isNullable(array $docBlockTypeHints): bool |
||
247 | } |
||
248 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.