| Conditions | 9 |
| Paths | 9 |
| Total Lines | 53 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 138 | protected function addAssociations(array &$fields): void |
||
| 139 | { |
||
| 140 | $classMetadata = $this->entityManager->getClassMetadata($this->getEntityClass()); |
||
| 141 | |||
| 142 | foreach ($classMetadata->getAssociationNames() as $associationName) { |
||
| 143 | if (! in_array($associationName, array_keys($this->metadataConfig['fields']))) { |
||
| 144 | continue; |
||
| 145 | } |
||
| 146 | |||
| 147 | $associationMetadata = $classMetadata->getAssociationMapping($associationName); |
||
| 148 | |||
| 149 | switch ($associationMetadata['type']) { |
||
| 150 | case ClassMetadataInfo::ONE_TO_ONE: |
||
| 151 | case ClassMetadataInfo::MANY_TO_ONE: |
||
| 152 | case ClassMetadataInfo::TO_ONE: |
||
| 153 | $targetEntity = $associationMetadata['targetEntity']; |
||
| 154 | $fields[$associationName] = function () use ($targetEntity) { |
||
| 155 | $entity = $this->metadata->get($targetEntity); |
||
| 156 | |||
| 157 | return [ |
||
| 158 | 'type' => $entity->getGraphQLType(), |
||
| 159 | 'description' => $entity->getDescription(), |
||
| 160 | ]; |
||
| 161 | }; |
||
| 162 | break; |
||
| 163 | case ClassMetadataInfo::ONE_TO_MANY: |
||
| 164 | case ClassMetadataInfo::MANY_TO_MANY: |
||
| 165 | case ClassMetadataInfo::TO_MANY: |
||
| 166 | $targetEntity = $associationMetadata['targetEntity']; |
||
| 167 | $fields[$associationName] = function () use ($targetEntity, $associationName) { |
||
| 168 | $entity = $this->metadata->get($targetEntity); |
||
| 169 | $shortName = $this->getTypeName() . '_' . $associationName; |
||
| 170 | |||
| 171 | return [ |
||
| 172 | 'type' => $this->typeManager->build( |
||
| 173 | Connection::class, |
||
| 174 | $shortName . '_Connection', |
||
| 175 | $entity->getGraphQLType(), |
||
| 176 | ), |
||
| 177 | 'args' => [ |
||
| 178 | 'filter' => $this->criteriaFactory->get( |
||
| 179 | $entity, |
||
| 180 | $this, |
||
| 181 | $associationName, |
||
| 182 | $this->metadataConfig['fields'][$associationName], |
||
| 183 | ), |
||
| 184 | 'pagination' => $this->typeManager->get('pagination'), |
||
| 185 | ], |
||
| 186 | 'description' => $this->metadataConfig['fields'][$associationName]['description'], |
||
| 187 | 'resolve' => $this->collectionFactory->get($entity), |
||
| 188 | ]; |
||
| 189 | }; |
||
| 190 | break; |
||
| 191 | } |
||
| 195 |