diff --git a/src/Validator/DocumentValidator.php b/src/Validator/DocumentValidator.php index 5e5540a..a3cde92 100644 --- a/src/Validator/DocumentValidator.php +++ b/src/Validator/DocumentValidator.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace GraphQL\Validator; +use Exception; use GraphQL\Error\Error; use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\Visitor; @@ -41,6 +42,7 @@ use GraphQL\Validator\Rules\ValuesOfCorrectType; use GraphQL\Validator\Rules\VariablesAreInputTypes; use GraphQL\Validator\Rules\VariablesDefaultValueAllowed; use GraphQL\Validator\Rules\VariablesInAllowedPosition; +use Throwable; use function array_filter; use function array_merge; use function count; @@ -82,9 +84,11 @@ class DocumentValidator /** * Primary method for query validation. See class description for details. * - * @api * @param ValidationRule[]|null $rules + * * @return Error[] + * + * @api */ public static function validate( Schema $schema, @@ -109,8 +113,9 @@ class DocumentValidator /** * Returns all global validation rules. * - * @api * @return ValidationRule[] + * + * @api */ public static function allRules() { @@ -183,6 +188,7 @@ class DocumentValidator * while maintaining the visitor skip and break API. * * @param ValidationRule[] $rules + * * @return Error[] */ public static function visitUsingRules(Schema $schema, TypeInfo $typeInfo, DocumentNode $documentNode, array $rules) @@ -203,9 +209,11 @@ class DocumentValidator * * $rule = DocumentValidator::getRule(GraphQL\Validator\Rules\QueryComplexity::class); * - * @api * @param string $name + * * @return ValidationRule + * + * @api */ public static function getRule($name) { @@ -235,11 +243,11 @@ class DocumentValidator return is_array($value) ? count(array_filter( $value, - function ($item) { - return $item instanceof \Exception || $item instanceof \Throwable; + static function ($item) { + return $item instanceof Exception || $item instanceof Throwable; } )) === count($value) - : ($value instanceof \Exception || $value instanceof \Throwable); + : ($value instanceof Exception || $value instanceof Throwable); } public static function append(&$arr, $items) @@ -259,6 +267,7 @@ class DocumentValidator * Deprecated. Rely on validation for documents containing literal values. * * @deprecated + * * @return Error[] */ public static function isValidLiteralValue(Type $type, $valueNode) diff --git a/src/Validator/Rules/DisableIntrospection.php b/src/Validator/Rules/DisableIntrospection.php index bdb7a7c..bb2dc3e 100644 --- a/src/Validator/Rules/DisableIntrospection.php +++ b/src/Validator/Rules/DisableIntrospection.php @@ -31,7 +31,7 @@ class DisableIntrospection extends QuerySecurityRule return $this->invokeIfNeeded( $context, [ - NodeKind::FIELD => function (FieldNode $node) use ($context) { + NodeKind::FIELD => static function (FieldNode $node) use ($context) { if ($node->name->value !== '__type' && $node->name->value !== '__schema') { return; } diff --git a/src/Validator/Rules/ExecutableDefinitions.php b/src/Validator/Rules/ExecutableDefinitions.php index 325b320..e626861 100644 --- a/src/Validator/Rules/ExecutableDefinitions.php +++ b/src/Validator/Rules/ExecutableDefinitions.php @@ -25,7 +25,7 @@ class ExecutableDefinitions extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::DOCUMENT => function (DocumentNode $node) use ($context) { + NodeKind::DOCUMENT => static function (DocumentNode $node) use ($context) { /** @var Node $definition */ foreach ($node->definitions as $definition) { if ($definition instanceof OperationDefinitionNode || diff --git a/src/Validator/Rules/FieldsOnCorrectType.php b/src/Validator/Rules/FieldsOnCorrectType.php index 104849c..1053255 100644 --- a/src/Validator/Rules/FieldsOnCorrectType.php +++ b/src/Validator/Rules/FieldsOnCorrectType.php @@ -74,6 +74,7 @@ class FieldsOnCorrectType extends ValidationRule * * @param ObjectType|InterfaceType $type * @param string $fieldName + * * @return string[] */ private function getSuggestedTypeNames(Schema $schema, $type, $fieldName) @@ -120,6 +121,7 @@ class FieldsOnCorrectType extends ValidationRule * * @param ObjectType|InterfaceType $type * @param string $fieldName + * * @return array|string[] */ private function getSuggestedFieldNames(Schema $schema, $type, $fieldName) @@ -139,6 +141,7 @@ class FieldsOnCorrectType extends ValidationRule * @param string $type * @param string[] $suggestedTypeNames * @param string[] $suggestedFieldNames + * * @return string */ public static function undefinedFieldMessage( diff --git a/src/Validator/Rules/FragmentsOnCompositeTypes.php b/src/Validator/Rules/FragmentsOnCompositeTypes.php index e6f8f16..c5ca807 100644 --- a/src/Validator/Rules/FragmentsOnCompositeTypes.php +++ b/src/Validator/Rules/FragmentsOnCompositeTypes.php @@ -19,7 +19,7 @@ class FragmentsOnCompositeTypes extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) use ($context) { + NodeKind::INLINE_FRAGMENT => static function (InlineFragmentNode $node) use ($context) { if (! $node->typeCondition) { return; } @@ -34,7 +34,7 @@ class FragmentsOnCompositeTypes extends ValidationRule [$node->typeCondition] )); }, - NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) { + NodeKind::FRAGMENT_DEFINITION => static function (FragmentDefinitionNode $node) use ($context) { $type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition); if (! $type || Type::isCompositeType($type)) { diff --git a/src/Validator/Rules/KnownArgumentNames.php b/src/Validator/Rules/KnownArgumentNames.php index 05a52ef..96fd800 100644 --- a/src/Validator/Rules/KnownArgumentNames.php +++ b/src/Validator/Rules/KnownArgumentNames.php @@ -26,7 +26,7 @@ class KnownArgumentNames extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::ARGUMENT => function (ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) { + NodeKind::ARGUMENT => static function (ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) { /** @var NodeList|Node[] $ancestors */ $argDef = $context->getArgument(); if ($argDef !== null) { @@ -46,7 +46,7 @@ class KnownArgumentNames extends ValidationRule Utils::suggestionList( $node->name->value, array_map( - function ($arg) { + static function ($arg) { return $arg->name; }, $fieldDef->args @@ -66,7 +66,7 @@ class KnownArgumentNames extends ValidationRule Utils::suggestionList( $node->name->value, array_map( - function ($arg) { + static function ($arg) { return $arg->name; }, $directive->args diff --git a/src/Validator/Rules/KnownDirectives.php b/src/Validator/Rules/KnownDirectives.php index c0c220b..da233a4 100644 --- a/src/Validator/Rules/KnownDirectives.php +++ b/src/Validator/Rules/KnownDirectives.php @@ -37,7 +37,7 @@ class KnownDirectives extends ValidationRule continue; } - $locationsMap[$def->name->value] = array_map(function ($name) { + $locationsMap[$def->name->value] = array_map(static function ($name) { return $name->value; }, $def->locations); } diff --git a/src/Validator/Rules/KnownFragmentNames.php b/src/Validator/Rules/KnownFragmentNames.php index 1b2b4e1..e26e233 100644 --- a/src/Validator/Rules/KnownFragmentNames.php +++ b/src/Validator/Rules/KnownFragmentNames.php @@ -15,7 +15,7 @@ class KnownFragmentNames extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) use ($context) { + NodeKind::FRAGMENT_SPREAD => static function (FragmentSpreadNode $node) use ($context) { $fragmentName = $node->name->value; $fragment = $context->getFragment($fragmentName); if ($fragment) { diff --git a/src/Validator/Rules/KnownTypeNames.php b/src/Validator/Rules/KnownTypeNames.php index 4700103..9abaf0a 100644 --- a/src/Validator/Rules/KnownTypeNames.php +++ b/src/Validator/Rules/KnownTypeNames.php @@ -23,7 +23,7 @@ class KnownTypeNames extends ValidationRule { public function getVisitor(ValidationContext $context) { - $skip = function () { + $skip = static function () { return Visitor::skipNode(); }; @@ -35,7 +35,7 @@ class KnownTypeNames extends ValidationRule NodeKind::INTERFACE_TYPE_DEFINITION => $skip, NodeKind::UNION_TYPE_DEFINITION => $skip, NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $skip, - NodeKind::NAMED_TYPE => function (NamedTypeNode $node) use ($context) { + NodeKind::NAMED_TYPE => static function (NamedTypeNode $node) use ($context) { $schema = $context->getSchema(); $typeName = $node->name->value; $type = $schema->getType($typeName); diff --git a/src/Validator/Rules/LoneAnonymousOperation.php b/src/Validator/Rules/LoneAnonymousOperation.php index 06730f7..40ff821 100644 --- a/src/Validator/Rules/LoneAnonymousOperation.php +++ b/src/Validator/Rules/LoneAnonymousOperation.php @@ -26,17 +26,17 @@ class LoneAnonymousOperation extends ValidationRule $operationCount = 0; return [ - NodeKind::DOCUMENT => function (DocumentNode $node) use (&$operationCount) { + NodeKind::DOCUMENT => static function (DocumentNode $node) use (&$operationCount) { $tmp = Utils::filter( $node->definitions, - function (Node $definition) { + static function (Node $definition) { return $definition->kind === NodeKind::OPERATION_DEFINITION; } ); $operationCount = count($tmp); }, - NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use ( + NodeKind::OPERATION_DEFINITION => static function (OperationDefinitionNode $node) use ( &$operationCount, $context ) { diff --git a/src/Validator/Rules/NoFragmentCycles.php b/src/Validator/Rules/NoFragmentCycles.php index 0b0a1e6..1180a4e 100644 --- a/src/Validator/Rules/NoFragmentCycles.php +++ b/src/Validator/Rules/NoFragmentCycles.php @@ -43,7 +43,7 @@ class NoFragmentCycles extends ValidationRule $this->spreadPathIndexByName = []; return [ - NodeKind::OPERATION_DEFINITION => function () { + NodeKind::OPERATION_DEFINITION => static function () { return Visitor::skipNode(); }, NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) { @@ -98,7 +98,7 @@ class NoFragmentCycles extends ValidationRule $spreadName, Utils::map( $cyclePath, - function ($s) { + static function ($s) { return $s->name->value; } ) diff --git a/src/Validator/Rules/NoUndefinedVariables.php b/src/Validator/Rules/NoUndefinedVariables.php index af7bfd4..5c2d4a5 100644 --- a/src/Validator/Rules/NoUndefinedVariables.php +++ b/src/Validator/Rules/NoUndefinedVariables.php @@ -25,10 +25,10 @@ class NoUndefinedVariables extends ValidationRule return [ NodeKind::OPERATION_DEFINITION => [ - 'enter' => function () use (&$variableNameDefined) { + 'enter' => static function () use (&$variableNameDefined) { $variableNameDefined = []; }, - 'leave' => function (OperationDefinitionNode $operation) use (&$variableNameDefined, $context) { + 'leave' => static function (OperationDefinitionNode $operation) use (&$variableNameDefined, $context) { $usages = $context->getRecursiveVariableUsages($operation); foreach ($usages as $usage) { @@ -49,7 +49,7 @@ class NoUndefinedVariables extends ValidationRule } }, ], - NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $def) use (&$variableNameDefined) { + NodeKind::VARIABLE_DEFINITION => static function (VariableDefinitionNode $def) use (&$variableNameDefined) { $variableNameDefined[$def->variable->name->value] = true; }, ]; diff --git a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php index 9a4c56b..d872245 100644 --- a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php +++ b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php @@ -24,6 +24,7 @@ use GraphQL\Type\Definition\Type; use GraphQL\Utils\PairSet; use GraphQL\Utils\TypeInfo; use GraphQL\Validator\ValidationContext; +use SplObjectStorage; use function array_keys; use function array_map; use function array_merge; @@ -39,6 +40,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * A memoization for when two fragments are compared "between" each other for * conflicts. Two fragments may be compared many times, so memoizing this can * dramatically improve the performance of this validator. + * * @var PairSet */ private $comparedFragmentPairs; @@ -48,14 +50,14 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * selection set. Selection sets may be asked for this information multiple * times, so this improves the performance of this validator. * - * @var \SplObjectStorage + * @var SplObjectStorage */ private $cachedFieldsAndFragmentNames; public function getVisitor(ValidationContext $context) { $this->comparedFragmentPairs = new PairSet(); - $this->cachedFieldsAndFragmentNames = new \SplObjectStorage(); + $this->cachedFieldsAndFragmentNames = new SplObjectStorage(); return [ NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context) { @@ -83,6 +85,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * GraphQL Document. * * @param CompositeType $parentType + * * @return mixed[] */ private function findConflictsWithinSelectionSet( @@ -145,7 +148,8 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * referenced via fragment spreads. * * @param CompositeType $parentType - * @return mixed[]|\SplObjectStorage + * + * @return mixed[]|SplObjectStorage */ private function getFieldsAndFragmentNames( ValidationContext $context, @@ -224,7 +228,6 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * * J) Also, if two fragments are referenced in both selection sets, then a * comparison is made "between" the two fragments. - * */ /** @@ -333,6 +336,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * @param string $responseName * @param mixed[] $field1 * @param mixed[] $field2 + * * @return mixed[]|null */ private function findConflict( @@ -503,6 +507,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * @param bool $areMutuallyExclusive * @param CompositeType $parentType1 * @param CompositeType $parentType2 + * * @return mixed[][] */ private function findConflictsBetweenSubSelectionSets( @@ -704,7 +709,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * Given a reference to a fragment, return the represented collection of fields * as well as a list of nested fragment names referenced via fragment spreads. * - * @return mixed[]|\SplObjectStorage + * @return mixed[]|SplObjectStorage */ private function getReferencedFieldsAndFragmentNames( ValidationContext $context, @@ -818,6 +823,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule * * @param mixed[][] $conflicts * @param string $responseName + * * @return mixed[]|null */ private function subfieldConflicts( @@ -834,7 +840,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule [ $responseName, array_map( - function ($conflict) { + static function ($conflict) { return $conflict[0]; }, $conflicts @@ -842,14 +848,14 @@ class OverlappingFieldsCanBeMerged extends ValidationRule ], array_reduce( $conflicts, - function ($allFields, $conflict) { + static function ($allFields, $conflict) { return array_merge($allFields, $conflict[1]); }, [$ast1] ), array_reduce( $conflicts, - function ($allFields, $conflict) { + static function ($allFields, $conflict) { return array_merge($allFields, $conflict[2]); }, [$ast2] @@ -876,7 +882,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule { if (is_array($reason)) { $tmp = array_map( - function ($tmp) { + static function ($tmp) { [$responseName, $subReason] = $tmp; $reasonMessage = self::reasonMessage($subReason); diff --git a/src/Validator/Rules/ProvidedNonNullArguments.php b/src/Validator/Rules/ProvidedNonNullArguments.php index 56254af..976f38b 100644 --- a/src/Validator/Rules/ProvidedNonNullArguments.php +++ b/src/Validator/Rules/ProvidedNonNullArguments.php @@ -19,7 +19,7 @@ class ProvidedNonNullArguments extends ValidationRule { return [ NodeKind::FIELD => [ - 'leave' => function (FieldNode $fieldNode) use ($context) { + 'leave' => static function (FieldNode $fieldNode) use ($context) { $fieldDef = $context->getFieldDef(); if (! $fieldDef) { @@ -45,7 +45,7 @@ class ProvidedNonNullArguments extends ValidationRule }, ], NodeKind::DIRECTIVE => [ - 'leave' => function (DirectiveNode $directiveNode) use ($context) { + 'leave' => static function (DirectiveNode $directiveNode) use ($context) { $directiveDef = $context->getDirective(); if (! $directiveDef) { return Visitor::skipNode(); diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index f126238..990fcbb 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace GraphQL\Validator\Rules; +use ArrayObject; use GraphQL\Error\Error; use GraphQL\Executor\Values; use GraphQL\Language\AST\FieldNode; @@ -31,10 +32,10 @@ class QueryComplexity extends QuerySecurityRule /** @var mixed[]|null */ private $rawVariableValues = []; - /** @var \ArrayObject */ + /** @var ArrayObject */ private $variableDefs; - /** @var \ArrayObject */ + /** @var ArrayObject */ private $fieldNodeAndDefs; /** @var ValidationContext */ @@ -49,8 +50,8 @@ class QueryComplexity extends QuerySecurityRule { $this->context = $context; - $this->variableDefs = new \ArrayObject(); - $this->fieldNodeAndDefs = new \ArrayObject(); + $this->variableDefs = new ArrayObject(); + $this->fieldNodeAndDefs = new ArrayObject(); $complexity = 0; return $this->invokeIfNeeded( @@ -196,7 +197,7 @@ class QueryComplexity extends QuerySecurityRule throw new Error(implode( "\n\n", array_map( - function ($error) { + static function ($error) { return $error->getMessage(); }, $variableValuesResult['errors'] @@ -251,7 +252,7 @@ class QueryComplexity extends QuerySecurityRule throw new Error(implode( "\n\n", array_map( - function ($error) { + static function ($error) { return $error->getMessage(); }, $variableValuesResult['errors'] diff --git a/src/Validator/Rules/QuerySecurityRule.php b/src/Validator/Rules/QuerySecurityRule.php index 810a4d0..c5f0260 100644 --- a/src/Validator/Rules/QuerySecurityRule.php +++ b/src/Validator/Rules/QuerySecurityRule.php @@ -4,16 +4,18 @@ declare(strict_types=1); namespace GraphQL\Validator\Rules; -use Closure; +use ArrayObject; use GraphQL\Language\AST\FieldNode; use GraphQL\Language\AST\FragmentDefinitionNode; use GraphQL\Language\AST\FragmentSpreadNode; +use GraphQL\Language\AST\InlineFragmentNode; use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\SelectionSetNode; use GraphQL\Type\Definition\Type; use GraphQL\Type\Introspection; use GraphQL\Utils\TypeInfo; use GraphQL\Validator\ValidationContext; +use InvalidArgumentException; use function class_alias; use function method_exists; use function sprintf; @@ -34,7 +36,7 @@ abstract class QuerySecurityRule extends ValidationRule protected function checkIfGreaterOrEqualToZero($name, $value) { if ($value < 0) { - throw new \InvalidArgumentException(sprintf('$%s argument must be greater or equal to 0.', $name)); + throw new InvalidArgumentException(sprintf('$%s argument must be greater or equal to 0.', $name)); } } @@ -55,8 +57,9 @@ abstract class QuerySecurityRule extends ValidationRule } /** - * @param Closure[] $validators - * @return Closure[] + * @param callable[] $validators + * + * @return callable[] */ protected function invokeIfNeeded(ValidationContext $context, array $validators) { @@ -98,17 +101,17 @@ abstract class QuerySecurityRule extends ValidationRule * * @param Type|null $parentType * - * @return \ArrayObject + * @return ArrayObject */ protected function collectFieldASTsAndDefs( ValidationContext $context, $parentType, SelectionSetNode $selectionSet, - ?\ArrayObject $visitedFragmentNames = null, - ?\ArrayObject $astAndDefs = null + ?ArrayObject $visitedFragmentNames = null, + ?ArrayObject $astAndDefs = null ) { - $_visitedFragmentNames = $visitedFragmentNames ?: new \ArrayObject(); - $_astAndDefs = $astAndDefs ?: new \ArrayObject(); + $_visitedFragmentNames = $visitedFragmentNames ?: new ArrayObject(); + $_astAndDefs = $astAndDefs ?: new ArrayObject(); foreach ($selectionSet->selections as $selection) { switch ($selection->kind) { @@ -134,7 +137,7 @@ abstract class QuerySecurityRule extends ValidationRule } $responseName = $this->getFieldName($selection); if (! isset($_astAndDefs[$responseName])) { - $_astAndDefs[$responseName] = new \ArrayObject(); + $_astAndDefs[$responseName] = new ArrayObject(); } // create field context $_astAndDefs[$responseName][] = [$selection, $fieldDef]; diff --git a/src/Validator/Rules/ScalarLeafs.php b/src/Validator/Rules/ScalarLeafs.php index a96b5d8..1bd2515 100644 --- a/src/Validator/Rules/ScalarLeafs.php +++ b/src/Validator/Rules/ScalarLeafs.php @@ -16,7 +16,7 @@ class ScalarLeafs extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::FIELD => function (FieldNode $node) use ($context) { + NodeKind::FIELD => static function (FieldNode $node) use ($context) { $type = $context->getType(); if (! $type) { return; diff --git a/src/Validator/Rules/UniqueDirectivesPerLocation.php b/src/Validator/Rules/UniqueDirectivesPerLocation.php index c049c82..f0e8c45 100644 --- a/src/Validator/Rules/UniqueDirectivesPerLocation.php +++ b/src/Validator/Rules/UniqueDirectivesPerLocation.php @@ -15,7 +15,7 @@ class UniqueDirectivesPerLocation extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - 'enter' => function (Node $node) use ($context) { + 'enter' => static function (Node $node) use ($context) { if (! isset($node->directives)) { return; } diff --git a/src/Validator/Rules/UniqueFragmentNames.php b/src/Validator/Rules/UniqueFragmentNames.php index e29de7b..475e6b3 100644 --- a/src/Validator/Rules/UniqueFragmentNames.php +++ b/src/Validator/Rules/UniqueFragmentNames.php @@ -22,7 +22,7 @@ class UniqueFragmentNames extends ValidationRule $this->knownFragmentNames = []; return [ - NodeKind::OPERATION_DEFINITION => function () { + NodeKind::OPERATION_DEFINITION => static function () { return Visitor::skipNode(); }, NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) { diff --git a/src/Validator/Rules/UniqueOperationNames.php b/src/Validator/Rules/UniqueOperationNames.php index 232380d..969b90e 100644 --- a/src/Validator/Rules/UniqueOperationNames.php +++ b/src/Validator/Rules/UniqueOperationNames.php @@ -38,7 +38,7 @@ class UniqueOperationNames extends ValidationRule return Visitor::skipNode(); }, - NodeKind::FRAGMENT_DEFINITION => function () { + NodeKind::FRAGMENT_DEFINITION => static function () { return Visitor::skipNode(); }, ]; diff --git a/src/Validator/Rules/ValidationRule.php b/src/Validator/Rules/ValidationRule.php index c35388f..fe8504d 100644 --- a/src/Validator/Rules/ValidationRule.php +++ b/src/Validator/Rules/ValidationRule.php @@ -6,7 +6,6 @@ namespace GraphQL\Validator\Rules; use GraphQL\Validator\ValidationContext; use function class_alias; -use function get_class; abstract class ValidationRule { @@ -15,7 +14,7 @@ abstract class ValidationRule public function getName() { - return $this->name ?: get_class($this); + return $this->name ?: static::class; } public function __invoke(ValidationContext $context) @@ -27,6 +26,7 @@ abstract class ValidationRule * Returns structure suitable for GraphQL\Language\Visitor * * @see \GraphQL\Language\Visitor + * * @return mixed[] */ abstract public function getVisitor(ValidationContext $context); diff --git a/src/Validator/Rules/ValuesOfCorrectType.php b/src/Validator/Rules/ValuesOfCorrectType.php index 2eadf6d..a08d99b 100644 --- a/src/Validator/Rules/ValuesOfCorrectType.php +++ b/src/Validator/Rules/ValuesOfCorrectType.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace GraphQL\Validator\Rules; +use Exception; use GraphQL\Error\Error; use GraphQL\Language\AST\BooleanValueNode; use GraphQL\Language\AST\EnumValueNode; @@ -27,6 +28,7 @@ use GraphQL\Type\Definition\ScalarType; use GraphQL\Type\Definition\Type; use GraphQL\Utils\Utils; use GraphQL\Validator\ValidationContext; +use Throwable; use function array_combine; use function array_keys; use function array_map; @@ -45,7 +47,7 @@ class ValuesOfCorrectType extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::NULL => function (NullValueNode $node) use ($context) { + NodeKind::NULL => static function (NullValueNode $node) use ($context) { $type = $context->getInputType(); if (! ($type instanceof NonNull)) { return; @@ -82,7 +84,7 @@ class ValuesOfCorrectType extends ValidationRule $nodeFields = iterator_to_array($node->fields); $fieldNodeMap = array_combine( array_map( - function ($field) { + static function ($field) { return $field->name->value; }, $nodeFields @@ -103,7 +105,7 @@ class ValuesOfCorrectType extends ValidationRule ); } }, - NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) use ($context) { + NodeKind::OBJECT_FIELD => static function (ObjectFieldNode $node) use ($context) { $parentType = Type::getNamedType($context->getParentInputType()); $fieldType = $context->getInputType(); if ($fieldType || ! ($parentType instanceof InputObjectType)) { @@ -193,7 +195,7 @@ class ValuesOfCorrectType extends ValidationRule // may throw to indicate failure. try { $type->parseLiteral($node); - } catch (\Exception $error) { + } catch (Exception $error) { // Ensure a reference to the original error is maintained. $context->reportError( new Error( @@ -209,7 +211,7 @@ class ValuesOfCorrectType extends ValidationRule $error ) ); - } catch (\Throwable $error) { + } catch (Throwable $error) { // Ensure a reference to the original error is maintained. $context->reportError( new Error( @@ -234,7 +236,7 @@ class ValuesOfCorrectType extends ValidationRule $suggestions = Utils::suggestionList( Printer::doPrint($node), array_map( - function (EnumValueDefinition $value) { + static function (EnumValueDefinition $value) { return $value->name; }, $type->getValues() diff --git a/src/Validator/Rules/VariablesAreInputTypes.php b/src/Validator/Rules/VariablesAreInputTypes.php index 4abc2e5..5dc27b4 100644 --- a/src/Validator/Rules/VariablesAreInputTypes.php +++ b/src/Validator/Rules/VariablesAreInputTypes.php @@ -18,7 +18,7 @@ class VariablesAreInputTypes extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use ($context) { + NodeKind::VARIABLE_DEFINITION => static function (VariableDefinitionNode $node) use ($context) { $type = TypeInfo::typeFromAST($context->getSchema(), $node->type); // If the variable type is not an input type, return an error. diff --git a/src/Validator/Rules/VariablesDefaultValueAllowed.php b/src/Validator/Rules/VariablesDefaultValueAllowed.php index 4f2b5cf..a19c14d 100644 --- a/src/Validator/Rules/VariablesDefaultValueAllowed.php +++ b/src/Validator/Rules/VariablesDefaultValueAllowed.php @@ -25,7 +25,7 @@ class VariablesDefaultValueAllowed extends ValidationRule public function getVisitor(ValidationContext $context) { return [ - NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use ($context) { + NodeKind::VARIABLE_DEFINITION => static function (VariableDefinitionNode $node) use ($context) { $name = $node->variable->name->value; $defaultValue = $node->defaultValue; $type = $context->getInputType(); @@ -44,10 +44,10 @@ class VariablesDefaultValueAllowed extends ValidationRule return Visitor::skipNode(); }, - NodeKind::SELECTION_SET => function (SelectionSetNode $node) { + NodeKind::SELECTION_SET => static function (SelectionSetNode $node) { return Visitor::skipNode(); }, - NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) { + NodeKind::FRAGMENT_DEFINITION => static function (FragmentDefinitionNode $node) { return Visitor::skipNode(); }, ]; diff --git a/src/Validator/Rules/VariablesInAllowedPosition.php b/src/Validator/Rules/VariablesInAllowedPosition.php index a593610..4fa3d46 100644 --- a/src/Validator/Rules/VariablesInAllowedPosition.php +++ b/src/Validator/Rules/VariablesInAllowedPosition.php @@ -71,7 +71,7 @@ class VariablesInAllowedPosition extends ValidationRule private function effectiveType($varType, $varDef) { - return (! $varDef->defaultValue || $varType instanceof NonNull) ? $varType : new NonNull($varType); + return ! $varDef->defaultValue || $varType instanceof NonNull ? $varType : new NonNull($varType); } /** diff --git a/src/Validator/ValidationContext.php b/src/Validator/ValidationContext.php index 2f44734..8ef75e3 100644 --- a/src/Validator/ValidationContext.php +++ b/src/Validator/ValidationContext.php @@ -128,10 +128,10 @@ class ValidationContext Visitor::visitWithTypeInfo( $typeInfo, [ - NodeKind::VARIABLE_DEFINITION => function () { + NodeKind::VARIABLE_DEFINITION => static function () { return false; }, - NodeKind::VARIABLE => function (VariableNode $variable) use ( + NodeKind::VARIABLE => static function (VariableNode $variable) use ( &$newUsages, $typeInfo ) { @@ -214,6 +214,7 @@ class ValidationContext /** * @param string $name + * * @return FragmentDefinitionNode|null */ public function getFragment($name)