| Total Complexity | 42 |
| Total Lines | 219 |
| Duplicated Lines | 16.89 % |
| 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 |
||
| 30 | class NamespaceDefinitionExtension extends AbstractDefinitionExtension |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $globalConfig = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * PaginationDefinitionExtension constructor. |
||
| 39 | * |
||
| 40 | * @param array $config |
||
| 41 | */ |
||
| 42 | 21 | public function __construct($config = []) |
|
| 43 | { |
||
| 44 | 21 | $this->globalConfig = $config; |
|
| 45 | 21 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | 21 | public function configure(DefinitionInterface $definition, Endpoint $endpoint, array $config) |
|
| 102 | } |
||
| 103 | 21 | } |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | */ |
||
| 108 | 21 | public function configureEndpoint(Endpoint $endpoint) |
|
| 115 | } |
||
| 116 | 21 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param array $definitions |
||
| 120 | * @param Endpoint $endpoint |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 21 | private function namespaceDefinitions($definitions, Endpoint $endpoint) |
|
| 125 | { |
||
| 126 | 21 | $namespacedDefinitions = []; |
|
| 127 | /** @var DefinitionInterface $definition */ |
||
| 128 | 21 | foreach ($definitions as $definition) { |
|
| 129 | 21 | if (!$definition->hasMeta('namespace') || !$definition->getMeta('namespace')) { |
|
| 130 | 21 | $namespacedDefinitions[] = $definition; |
|
| 131 | 21 | continue; |
|
| 132 | } |
||
| 133 | |||
| 134 | 21 | $root = null; |
|
| 135 | 21 | $parent = null; |
|
| 136 | 21 | $namespace = $definition->getMeta('namespace'); |
|
| 137 | 21 | if ($bundle = $namespace['bundle'] ?? null) { |
|
| 138 | $bundleSuffix = $this->globalConfig['bundles']['suffix'] ?? 'Bundle'; |
||
| 139 | $name = lcfirst($bundle); |
||
| 140 | $typeName = ucfirst($name).$bundleSuffix; |
||
| 141 | $root = $this->createRootNamespace(get_class($definition), $name, $typeName, $endpoint); |
||
| 142 | $parent = $endpoint->getType($root->getType()); |
||
| 143 | } |
||
| 144 | |||
| 145 | 21 | if ($nodeName = $namespace['node'] ?? null) { |
|
| 146 | 21 | if ($endpoint->hasTypeForClass($nodeName)) { |
|
| 147 | $nodeName = $endpoint->getTypeForClass($nodeName); |
||
| 148 | } |
||
| 149 | |||
| 150 | 21 | $name = Inflector::pluralize(lcfirst($nodeName)); |
|
| 151 | |||
| 152 | 21 | $querySuffix = $this->globalConfig['nodes']['query_suffix'] ?? 'Query'; |
|
| 153 | 21 | $mutationSuffix = $this->globalConfig['nodes']['mutation_suffix'] ?? 'Mutation'; |
|
| 154 | |||
| 155 | 21 | $typeName = ucfirst($nodeName).(($definition instanceof MutationDefinition) ? $mutationSuffix : $querySuffix); |
|
| 156 | 21 | if (!$root) { |
|
| 157 | 21 | $root = $this->createRootNamespace(get_class($definition), $name, $typeName, $endpoint); |
|
| 158 | 21 | $parent = $endpoint->getType($root->getType()); |
|
| 159 | } elseif ($parent) { |
||
| 160 | $parent = $this->createChildNamespace($parent, $name, $typeName, $endpoint); |
||
| 161 | } |
||
| 162 | |||
| 163 | //remove node suffix on namespaced definitions |
||
| 164 | 21 | $definition->setName(preg_replace(sprintf("/(\w+)%s$/", $nodeName), '$1', $definition->getName())); |
|
| 165 | 21 | $definition->setName(preg_replace(sprintf("/(\w+)%s$/", Inflector::pluralize($nodeName)), '$1', $definition->getName())); |
|
| 166 | |||
| 167 | } |
||
| 168 | |||
| 169 | 21 | if ($root && $parent) { |
|
| 170 | 21 | $this->addDefinitionToNamespace($parent, $definition); |
|
| 171 | 21 | $namespacedDefinitions[] = $root; |
|
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | 21 | return $namespacedDefinitions; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param FieldsAwareDefinitionInterface $fieldsAwareDefinition |
||
| 180 | * @param ExecutableDefinitionInterface $definition |
||
| 181 | */ |
||
| 182 | 21 | private function addDefinitionToNamespace(FieldsAwareDefinitionInterface $fieldsAwareDefinition, ExecutableDefinitionInterface $definition) |
|
| 183 | { |
||
| 184 | 21 | $field = new FieldDefinition(); |
|
| 185 | 21 | $field->setName($definition->getName()); |
|
| 186 | 21 | $field->setType($definition->getType()); |
|
| 187 | 21 | $field->setResolver($definition->getResolver()); |
|
| 188 | 21 | $field->setArguments($definition->getArguments()); |
|
| 189 | 21 | $field->setList($definition->isList()); |
|
| 190 | 21 | $field->setMetas($definition->getMetas()); |
|
| 191 | 21 | $field->setNode($definition->getNode()); |
|
| 192 | 21 | $fieldsAwareDefinition->addField($field); |
|
| 193 | 21 | } |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param ObjectDefinitionInterface $parent parent definition to add a child field |
||
| 197 | * @param string $name name of the field |
||
| 198 | * @param string $typeName name of the type to create |
||
| 199 | * @param Endpoint $endpoint Endpoint instance to extract definitions |
||
| 200 | * |
||
| 201 | * @return ObjectDefinition |
||
| 202 | */ |
||
| 203 | View Code Duplication | private function createChildNamespace(ObjectDefinitionInterface $parent, $name, $typeName, Endpoint $endpoint): ObjectDefinition |
|
| 204 | { |
||
| 205 | $child = new FieldDefinition(); |
||
| 206 | $child->setName($name); |
||
| 207 | $child->setResolver(EmptyObjectResolver::class); |
||
| 208 | |||
| 209 | $type = new ObjectDefinition(); |
||
| 210 | $type->setName($typeName); |
||
| 211 | if ($endpoint->hasType($type->getName())) { |
||
| 212 | $type = $endpoint->getType($type->getName()); |
||
| 213 | } else { |
||
| 214 | $endpoint->add($type); |
||
| 215 | } |
||
| 216 | |||
| 217 | $child->setType($type->getName()); |
||
| 218 | $parent->addField($child); |
||
| 219 | |||
| 220 | return $type; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $rootType Class of the root type to create QueryDefinition or MutationDefinition |
||
| 225 | * @param string $name name of the root field |
||
| 226 | * @param string $typeName name for the root type |
||
| 227 | * @param Endpoint $endpoint Endpoint interface to extract existent definitions |
||
| 228 | * |
||
| 229 | * @return ExecutableDefinitionInterface |
||
| 230 | */ |
||
| 231 | 21 | View Code Duplication | private function createRootNamespace($rootType, $name, $typeName, Endpoint $endpoint): ExecutableDefinitionInterface |
| 249 | } |
||
| 250 | } |
||
| 251 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: