Conditions | 10 |
Paths | 10 |
Total Lines | 55 |
Code Lines | 28 |
Lines | 15 |
Ratio | 27.27 % |
Changes | 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 |
||
49 | public function resolve($value, array $args, ResolveInfo $info) { |
||
50 | $entityTypeId = $this->pluginDefinition['entity_type']; |
||
51 | $bundleName = $this->pluginDefinition['entity_bundle']; |
||
52 | $storage = $this->entityTypeManager->getStorage($entityTypeId); |
||
53 | |||
54 | /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ |
||
55 | View Code Duplication | if (!$entity = $storage->load($args['id'])) { |
|
|
|||
56 | return new EntityCrudOutputWrapper(NULL, NULL, [ |
||
57 | $this->t('The requested @bundle could not be loaded.', ['@bundle' => $bundleName]), |
||
58 | ]); |
||
59 | } |
||
60 | |||
61 | View Code Duplication | if (!$entity->bundle() === $bundleName) { |
|
62 | return new EntityCrudOutputWrapper(NULL, NULL, [ |
||
63 | $this->t('The requested entity is not of the expected type @bundle.', ['@bundle' => $bundleName]), |
||
64 | ]); |
||
65 | } |
||
66 | |||
67 | View Code Duplication | if (!$entity->access('update')) { |
|
68 | return new EntityCrudOutputWrapper(NULL, NULL, [ |
||
69 | $this->t('You do not have the necessary permissions to update this @bundle.', ['@bundle' => $bundleName]), |
||
70 | ]); |
||
71 | } |
||
72 | |||
73 | // The raw input needs to be converted to use the proper field and property |
||
74 | // keys because we usually convert them to camel case when adding them to |
||
75 | // the schema. |
||
76 | $inputArgs = $args['input']; |
||
77 | /** @var \Youshido\GraphQL\Type\Object\AbstractObjectType $type */ |
||
78 | $type = $this->config->getArgument('input')->getType(); |
||
79 | /** @var \Drupal\graphql\Plugin\GraphQL\InputTypes\InputTypePluginBase $inputType */ |
||
80 | $inputType = $type->getNamedType(); |
||
81 | $input = $this->extractEntityInput($inputArgs, $inputType); |
||
82 | |||
83 | try { |
||
84 | foreach ($input as $key => $value) { |
||
85 | $entity->get($key)->setValue($value); |
||
86 | } |
||
87 | } |
||
88 | catch (\InvalidArgumentException $exception) { |
||
89 | return new EntityCrudOutputWrapper(NULL, NULL, [ |
||
90 | $this->t('The entity update failed with exception: @exception.', ['@exception' => $exception->getMessage()]), |
||
91 | ]); |
||
92 | } |
||
93 | |||
94 | if (($violations = $entity->validate()) && $violations->count()) { |
||
95 | return new EntityCrudOutputWrapper(NULL, $violations); |
||
96 | } |
||
97 | |||
98 | if (($status = $entity->save()) && $status === SAVED_UPDATED) { |
||
99 | return new EntityCrudOutputWrapper($entity); |
||
100 | } |
||
101 | |||
102 | return NULL; |
||
103 | } |
||
104 | |||
121 |
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.