Conditions | 6 |
Paths | 6 |
Total Lines | 58 |
Code Lines | 31 |
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 |
||
18 | public function validate(ValidationContext $context): void |
||
19 | { |
||
20 | $directives = $context->getSchema()->getDirectives(); |
||
21 | |||
22 | foreach ($directives as $directive) { |
||
23 | if (!($directive instanceof Directive)) { |
||
24 | $context->reportError( |
||
25 | new ValidationException( |
||
26 | \sprintf('Expected directive but got: %s.', $directive->getAstNode()) |
||
27 | ) |
||
28 | ); |
||
29 | |||
30 | return; |
||
31 | } |
||
32 | |||
33 | // Ensure they are named correctly. |
||
34 | $this->validateName($context, $directive); |
||
35 | |||
36 | // TODO: Ensure proper locations. |
||
37 | |||
38 | // Ensure the arguments are valid. |
||
39 | $argumentNames = []; |
||
40 | |||
41 | foreach ($directive->getArguments() as $argument) { |
||
42 | $argumentName = $argument->getName(); |
||
43 | |||
44 | // Ensure they are named correctly. |
||
45 | $this->validateName($context, $argument); |
||
46 | |||
47 | // Ensure they are unique per directive. |
||
48 | if (isset($argumentNames[$argumentName])) { |
||
49 | $context->reportError( |
||
50 | new ValidationException( |
||
51 | \sprintf( |
||
52 | 'Argument @%s(%s:) can only be defined once.', |
||
53 | $directive->getName(), |
||
54 | $argumentName |
||
55 | ), |
||
56 | $this->getAllDirectiveArgumentNodes($directive, $argumentName) |
||
57 | ) |
||
58 | ); |
||
59 | |||
60 | continue; |
||
61 | } |
||
62 | |||
63 | $argumentNames[$argumentName] = true; |
||
64 | |||
65 | // Ensure the type is an input type. |
||
66 | if (!isInputType($argument->getType())) { |
||
67 | $context->reportError( |
||
68 | new ValidationException( |
||
69 | \sprintf( |
||
70 | 'The type of @%s(%s:) must be Input Type but got: %s.', |
||
71 | $directive->getName(), |
||
72 | $argumentName, |
||
73 | (string)$argument->getType() |
||
74 | ), |
||
75 | $this->getAllDirectiveArgumentNodes($directive, $argumentName) |
||
76 | ) |
||
106 |