webonyx /
graphql-php
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace GraphQL\Validator\Rules; |
||||
| 6 | |||||
| 7 | use GraphQL\Error\Error; |
||||
| 8 | use GraphQL\Language\AST\FragmentDefinitionNode; |
||||
| 9 | use GraphQL\Language\AST\NodeKind; |
||||
| 10 | use GraphQL\Language\AST\SelectionSetNode; |
||||
| 11 | use GraphQL\Language\AST\VariableDefinitionNode; |
||||
| 12 | use GraphQL\Language\Visitor; |
||||
| 13 | use GraphQL\Type\Definition\NonNull; |
||||
| 14 | use GraphQL\Validator\ValidationContext; |
||||
| 15 | use function sprintf; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * Variable's default value is allowed |
||||
| 19 | * |
||||
| 20 | * A GraphQL document is only valid if all variable default values are allowed |
||||
| 21 | * due to a variable not being required. |
||||
| 22 | */ |
||||
| 23 | class VariablesDefaultValueAllowed extends ValidationRule |
||||
| 24 | { |
||||
| 25 | 110 | public function getVisitor(ValidationContext $context) |
|||
| 26 | { |
||||
| 27 | return [ |
||||
| 28 | NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use ($context) { |
||||
| 29 | 17 | $name = $node->variable->name->value; |
|||
| 30 | 17 | $defaultValue = $node->defaultValue; |
|||
| 31 | 17 | $type = $context->getInputType(); |
|||
| 32 | 17 | if ($type instanceof NonNull && $defaultValue) { |
|||
| 33 | 2 | $context->reportError( |
|||
| 34 | 2 | new Error( |
|||
| 35 | 2 | self::defaultForRequiredVarMessage( |
|||
| 36 | 2 | $name, |
|||
| 37 | 2 | $type, |
|||
| 38 | 2 | $type->getWrappedType() |
|||
| 39 | ), |
||||
| 40 | 2 | [$defaultValue] |
|||
| 41 | ) |
||||
| 42 | ); |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | 17 | return Visitor::skipNode(); |
|||
| 46 | 110 | }, |
|||
| 47 | NodeKind::SELECTION_SET => function (SelectionSetNode $node) { |
||||
|
0 ignored issues
–
show
|
|||||
| 48 | 109 | return Visitor::skipNode(); |
|||
| 49 | 110 | }, |
|||
| 50 | NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) { |
||||
|
0 ignored issues
–
show
The parameter
$node is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 51 | 9 | return Visitor::skipNode(); |
|||
| 52 | 110 | }, |
|||
| 53 | ]; |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | 2 | public static function defaultForRequiredVarMessage($varName, $type, $guessType) |
|||
| 57 | { |
||||
| 58 | 2 | return sprintf( |
|||
| 59 | 2 | 'Variable "$%s" of type "%s" is required and will not use the default value. Perhaps you meant to use type "%s".', |
|||
| 60 | 2 | $varName, |
|||
| 61 | 2 | $type, |
|||
| 62 | 2 | $guessType |
|||
| 63 | ); |
||||
| 64 | } |
||||
| 65 | } |
||||
| 66 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.