Conditions | 11 |
Paths | 7 |
Total Lines | 66 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
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 |
||
26 | public function leaveNode(NodeInterface $node): ?NodeInterface |
||
27 | { |
||
28 | // Validate on leave to allow for deeper errors to appear first. |
||
29 | if ($node instanceof FieldNode) { |
||
30 | $fieldDefinition = $this->validationContext->getFieldDefinition(); |
||
31 | |||
32 | if (null === $fieldDefinition) { |
||
33 | return null; |
||
34 | } |
||
35 | |||
36 | $argumentNodes = $node->getArguments(); |
||
37 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $argument) { |
||
38 | return $argument->getNameValue(); |
||
39 | }); |
||
40 | |||
41 | foreach ($fieldDefinition->getArguments() as $argumentDefinition) { |
||
42 | $argumentNode = $argumentNodeMap[$argumentDefinition->getName()] ?? null; |
||
43 | $argumentType = $argumentDefinition->getType(); |
||
44 | |||
45 | if (null === $argumentNode && $argumentType instanceof NonNullType) { |
||
46 | $this->validationContext->reportError( |
||
47 | new ValidationException( |
||
48 | missingFieldArgumentMessage( |
||
49 | (string)$node, |
||
50 | (string)$argumentDefinition, |
||
51 | (string)$argumentType |
||
52 | ), |
||
53 | [$node] |
||
54 | ) |
||
55 | ); |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if ($node instanceof DirectiveNode) { |
||
61 | $directiveDefinition = $this->validationContext->getDirective(); |
||
62 | |||
63 | if (null === $directiveDefinition) { |
||
64 | return null; |
||
65 | } |
||
66 | |||
67 | $argumentNodes = $node->getArguments(); |
||
68 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $argument) { |
||
69 | return $argument->getNameValue(); |
||
70 | }); |
||
71 | |||
72 | foreach ($directiveDefinition->getArguments() as $argumentDefinition) { |
||
73 | $argumentNode = $argumentNodeMap[$argumentDefinition->getName()] ?? null; |
||
74 | $argumentType = $argumentDefinition->getType(); |
||
75 | |||
76 | if (null === $argumentNode && $argumentType instanceof NonNullType) { |
||
77 | $this->validationContext->reportError( |
||
78 | new ValidationException( |
||
79 | missingDirectiveArgumentMessage( |
||
80 | (string)$node, |
||
81 | (string)$argumentDefinition, |
||
82 | (string)$argumentType |
||
83 | ), |
||
84 | [$node] |
||
85 | ) |
||
86 | ); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | return $node; |
||
92 | } |
||
94 |