Total Complexity | 44 |
Total Lines | 235 |
Duplicated Lines | 15.74 % |
Coverage | 76.64% |
Changes | 0 |
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:
Complex classes like NamespaceDefinitionExtension 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 NamespaceDefinitionExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class NamespaceDefinitionExtension extends AbstractDefinitionExtension |
||
32 | { |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $globalConfig = []; |
||
37 | |||
38 | /** |
||
39 | * PaginationDefinitionExtension constructor. |
||
40 | * |
||
41 | * @param array $config |
||
42 | 21 | */ |
|
43 | public function __construct($config = []) |
||
44 | 21 | { |
|
45 | 21 | $this->globalConfig = $config; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * {@inheritDoc} |
||
50 | 21 | */ |
|
51 | public function buildConfig(ArrayNodeDefinition $root) |
||
52 | 21 | { |
|
53 | 21 | $root |
|
54 | 21 | ->info('Enable/Disable namespace for queries and mutations') |
|
55 | 21 | ->canBeDisabled() |
|
56 | ->children(); |
||
57 | 21 | } |
|
58 | |||
59 | /** |
||
60 | 21 | * {@inheritdoc} |
|
61 | */ |
||
62 | public function configure(DefinitionInterface $definition, Endpoint $endpoint, array $config) |
||
63 | 21 | { |
|
64 | $node = null; |
||
65 | $nodeClass = null; |
||
66 | |||
67 | 21 | if (!($config['enabled'] ?? true)) { |
|
68 | return; |
||
69 | } |
||
70 | |||
71 | 21 | if (($this->globalConfig['nodes']['enabled'] ?? false) && $definition instanceof NodeAwareDefinitionInterface && $definition->getNode()) { |
|
|
|||
72 | 21 | $node = $definition->getNode(); |
|
73 | |||
74 | if (class_exists($node)) { |
||
75 | $nodeClass = $node; |
||
76 | 21 | } else { |
|
77 | 21 | $nodeClass = $endpoint->getClassForType($node); |
|
78 | 21 | } |
|
79 | 21 | ||
80 | 21 | if (!is_a($nodeClass, NodeInterface::class, true)) { |
|
81 | 21 | return; |
|
82 | 21 | } |
|
83 | |||
84 | if (isset($this->globalConfig['nodes']['aliases'][$node])) { |
||
85 | 21 | $node = $this->globalConfig['nodes']['aliases'][$node]; |
|
86 | } |
||
87 | |||
88 | if ($node && in_array($node, $this->globalConfig['nodes']['ignore'])) { |
||
89 | 21 | $node = null; |
|
90 | 21 | } |
|
91 | } |
||
92 | |||
93 | 21 | $bundle = null; |
|
94 | if ($this->globalConfig['bundles']['enabled'] ?? false) { |
||
95 | if ($node && $nodeClass) { |
||
96 | if ($endpoint->hasType($node) && $nodeClass) { |
||
97 | preg_match_all('/\\\\?(\w+Bundle)\\\\/', $nodeClass, $matches); |
||
98 | if ($matches) { |
||
99 | $bundle = current(array_reverse($matches[1])); |
||
100 | 21 | } |
|
101 | 21 | ||
102 | if (isset($this->globalConfig['bundles']['aliases'][$bundle])) { |
||
103 | 21 | $bundle = $this->globalConfig['bundles']['aliases'][$bundle]; |
|
104 | } |
||
105 | |||
106 | if ($bundle && in_array($bundle, $this->globalConfig['bundles']['ignore'])) { |
||
107 | $bundle = null; |
||
108 | 21 | } |
|
109 | |||
110 | 21 | if ($bundle) { |
|
111 | 21 | $bundle = preg_replace('/Bundle$/', null, $bundle); |
|
112 | 21 | } |
|
113 | 21 | } |
|
114 | 21 | } |
|
115 | } |
||
116 | 21 | ||
117 | if ($bundle || $node) { |
||
118 | $definition->setMeta('namespace', ['bundle' => $bundle, 'node' => $node]); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * {@inheritDoc} |
||
124 | 21 | */ |
|
125 | public function configureEndpoint(Endpoint $endpoint) |
||
126 | 21 | { |
|
127 | $groupByBundle = $this->globalConfig['bundles']['enabled'] ?? false; |
||
128 | 21 | $groupByNode = $this->globalConfig['bundles']['enabled'] ?? false; |
|
129 | 21 | if ($groupByBundle || $groupByNode) { |
|
130 | 21 | $endpoint->setQueries($this->namespaceDefinitions($endpoint->allQueries(), $endpoint)); |
|
131 | 21 | $endpoint->setMutations($this->namespaceDefinitions($endpoint->allMutations(), $endpoint)); |
|
132 | } |
||
133 | } |
||
134 | 21 | ||
135 | 21 | /** |
|
136 | 21 | * @param array $definitions |
|
137 | 21 | * @param Endpoint $endpoint |
|
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | private function namespaceDefinitions($definitions, Endpoint $endpoint) |
||
142 | { |
||
143 | $namespacedDefinitions = []; |
||
144 | /** @var DefinitionInterface $definition */ |
||
145 | 21 | foreach ($definitions as $definition) { |
|
146 | 21 | if (!$definition->hasMeta('namespace') || !$definition->getMeta('namespace')) { |
|
147 | $namespacedDefinitions[] = $definition; |
||
148 | continue; |
||
149 | } |
||
150 | 21 | ||
151 | $root = null; |
||
152 | 21 | $parent = null; |
|
153 | 21 | $namespace = $definition->getMeta('namespace'); |
|
154 | if ($bundle = $namespace['bundle'] ?? null) { |
||
155 | 21 | $bundleSuffix = $this->globalConfig['bundles']['suffix'] ?? 'Bundle'; |
|
156 | 21 | $name = lcfirst($bundle); |
|
157 | 21 | $typeName = ucfirst($name).$bundleSuffix; |
|
158 | 21 | $root = $this->createRootNamespace(get_class($definition), $name, $typeName, $endpoint); |
|
159 | $parent = $endpoint->getType($root->getType()); |
||
160 | } |
||
161 | |||
162 | if ($nodeName = $namespace['node'] ?? null) { |
||
163 | if ($endpoint->hasTypeForClass($nodeName)) { |
||
164 | 21 | $nodeName = $endpoint->getTypeForClass($nodeName); |
|
165 | 21 | } |
|
166 | |||
167 | $name = Inflector::pluralize(lcfirst($nodeName)); |
||
168 | |||
169 | 21 | $querySuffix = $this->globalConfig['nodes']['query_suffix'] ?? 'Query'; |
|
170 | 21 | $mutationSuffix = $this->globalConfig['nodes']['mutation_suffix'] ?? 'Mutation'; |
|
171 | 21 | ||
172 | $typeName = ucfirst($nodeName).(($definition instanceof MutationDefinition) ? $mutationSuffix : $querySuffix); |
||
173 | if (!$root) { |
||
174 | $root = $this->createRootNamespace(get_class($definition), $name, $typeName, $endpoint); |
||
175 | 21 | $parent = $endpoint->getType($root->getType()); |
|
176 | } elseif ($parent) { |
||
177 | $parent = $this->createChildNamespace($parent, $name, $typeName, $endpoint); |
||
178 | } |
||
179 | |||
180 | //remove node suffix on namespaced definitions |
||
181 | $definition->setName(preg_replace(sprintf("/(\w+)%s$/", $nodeName), '$1', $definition->getName())); |
||
182 | 21 | $definition->setName(preg_replace(sprintf("/(\w+)%s$/", Inflector::pluralize($nodeName)), '$1', $definition->getName())); |
|
183 | |||
184 | 21 | } |
|
185 | 21 | ||
186 | 21 | if ($root && $parent) { |
|
187 | 21 | $this->addDefinitionToNamespace($parent, $definition); |
|
188 | 21 | $namespacedDefinitions[] = $root; |
|
189 | 21 | } |
|
190 | 21 | } |
|
191 | 21 | ||
192 | 21 | return $namespacedDefinitions; |
|
193 | 21 | } |
|
194 | |||
195 | /** |
||
196 | * @param FieldsAwareDefinitionInterface $fieldsAwareDefinition |
||
197 | * @param ExecutableDefinitionInterface $definition |
||
198 | */ |
||
199 | private function addDefinitionToNamespace(FieldsAwareDefinitionInterface $fieldsAwareDefinition, ExecutableDefinitionInterface $definition) |
||
200 | { |
||
201 | $field = new FieldDefinition(); |
||
202 | $field->setName($definition->getName()); |
||
203 | $field->setType($definition->getType()); |
||
204 | $field->setResolver($definition->getResolver()); |
||
205 | $field->setArguments($definition->getArguments()); |
||
206 | $field->setList($definition->isList()); |
||
207 | $field->setMetas($definition->getMetas()); |
||
208 | $field->setNode($definition->getNode()); |
||
209 | $fieldsAwareDefinition->addField($field); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param ObjectDefinitionInterface $parent parent definition to add a child field |
||
214 | * @param string $name name of the field |
||
215 | * @param string $typeName name of the type to create |
||
216 | * @param Endpoint $endpoint Endpoint instance to extract definitions |
||
217 | * |
||
218 | * @return ObjectDefinition |
||
219 | */ |
||
220 | View Code Duplication | private function createChildNamespace(ObjectDefinitionInterface $parent, $name, $typeName, Endpoint $endpoint): ObjectDefinition |
|
221 | { |
||
222 | $child = new FieldDefinition(); |
||
223 | $child->setName($name); |
||
224 | $child->setResolver(EmptyObjectResolver::class); |
||
225 | |||
226 | $type = new ObjectDefinition(); |
||
227 | $type->setName($typeName); |
||
228 | if ($endpoint->hasType($type->getName())) { |
||
229 | $type = $endpoint->getType($type->getName()); |
||
230 | } else { |
||
231 | 21 | $endpoint->add($type); |
|
232 | } |
||
233 | |||
234 | 21 | $child->setType($type->getName()); |
|
235 | 21 | $parent->addField($child); |
|
236 | 21 | ||
237 | return $type; |
||
238 | 21 | } |
|
239 | 21 | ||
240 | 21 | /** |
|
241 | 21 | * @param string $rootType Class of the root type to create QueryDefinition or MutationDefinition |
|
242 | * @param string $name name of the root field |
||
243 | 21 | * @param string $typeName name for the root type |
|
244 | * @param Endpoint $endpoint Endpoint interface to extract existent definitions |
||
245 | * |
||
246 | 21 | * @return ExecutableDefinitionInterface |
|
247 | */ |
||
248 | 21 | View Code Duplication | private function createRootNamespace($rootType, $name, $typeName, Endpoint $endpoint): ExecutableDefinitionInterface |
266 | } |
||
267 | } |
||
268 |
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: