Fix CS in src/Validator

This commit is contained in:
Simon Podlipsky 2018-09-26 11:01:29 +02:00
parent e664c4455e
commit dd4a5076f6
No known key found for this signature in database
GPG Key ID: 725C2BD962B42663
26 changed files with 96 additions and 71 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace GraphQL\Validator; namespace GraphQL\Validator;
use Exception;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\Visitor; use GraphQL\Language\Visitor;
@ -41,6 +42,7 @@ use GraphQL\Validator\Rules\ValuesOfCorrectType;
use GraphQL\Validator\Rules\VariablesAreInputTypes; use GraphQL\Validator\Rules\VariablesAreInputTypes;
use GraphQL\Validator\Rules\VariablesDefaultValueAllowed; use GraphQL\Validator\Rules\VariablesDefaultValueAllowed;
use GraphQL\Validator\Rules\VariablesInAllowedPosition; use GraphQL\Validator\Rules\VariablesInAllowedPosition;
use Throwable;
use function array_filter; use function array_filter;
use function array_merge; use function array_merge;
use function count; use function count;
@ -82,9 +84,11 @@ class DocumentValidator
/** /**
* Primary method for query validation. See class description for details. * Primary method for query validation. See class description for details.
* *
* @api
* @param ValidationRule[]|null $rules * @param ValidationRule[]|null $rules
*
* @return Error[] * @return Error[]
*
* @api
*/ */
public static function validate( public static function validate(
Schema $schema, Schema $schema,
@ -109,8 +113,9 @@ class DocumentValidator
/** /**
* Returns all global validation rules. * Returns all global validation rules.
* *
* @api
* @return ValidationRule[] * @return ValidationRule[]
*
* @api
*/ */
public static function allRules() public static function allRules()
{ {
@ -183,6 +188,7 @@ class DocumentValidator
* while maintaining the visitor skip and break API. * while maintaining the visitor skip and break API.
* *
* @param ValidationRule[] $rules * @param ValidationRule[] $rules
*
* @return Error[] * @return Error[]
*/ */
public static function visitUsingRules(Schema $schema, TypeInfo $typeInfo, DocumentNode $documentNode, array $rules) 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); * $rule = DocumentValidator::getRule(GraphQL\Validator\Rules\QueryComplexity::class);
* *
* @api
* @param string $name * @param string $name
*
* @return ValidationRule * @return ValidationRule
*
* @api
*/ */
public static function getRule($name) public static function getRule($name)
{ {
@ -235,11 +243,11 @@ class DocumentValidator
return is_array($value) return is_array($value)
? count(array_filter( ? count(array_filter(
$value, $value,
function ($item) { static function ($item) {
return $item instanceof \Exception || $item instanceof \Throwable; return $item instanceof Exception || $item instanceof Throwable;
} }
)) === count($value) )) === count($value)
: ($value instanceof \Exception || $value instanceof \Throwable); : ($value instanceof Exception || $value instanceof Throwable);
} }
public static function append(&$arr, $items) public static function append(&$arr, $items)
@ -259,6 +267,7 @@ class DocumentValidator
* Deprecated. Rely on validation for documents containing literal values. * Deprecated. Rely on validation for documents containing literal values.
* *
* @deprecated * @deprecated
*
* @return Error[] * @return Error[]
*/ */
public static function isValidLiteralValue(Type $type, $valueNode) public static function isValidLiteralValue(Type $type, $valueNode)

View File

@ -31,7 +31,7 @@ class DisableIntrospection extends QuerySecurityRule
return $this->invokeIfNeeded( return $this->invokeIfNeeded(
$context, $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') { if ($node->name->value !== '__type' && $node->name->value !== '__schema') {
return; return;
} }

View File

@ -25,7 +25,7 @@ class ExecutableDefinitions extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ return [
NodeKind::DOCUMENT => function (DocumentNode $node) use ($context) { NodeKind::DOCUMENT => static function (DocumentNode $node) use ($context) {
/** @var Node $definition */ /** @var Node $definition */
foreach ($node->definitions as $definition) { foreach ($node->definitions as $definition) {
if ($definition instanceof OperationDefinitionNode || if ($definition instanceof OperationDefinitionNode ||

View File

@ -74,6 +74,7 @@ class FieldsOnCorrectType extends ValidationRule
* *
* @param ObjectType|InterfaceType $type * @param ObjectType|InterfaceType $type
* @param string $fieldName * @param string $fieldName
*
* @return string[] * @return string[]
*/ */
private function getSuggestedTypeNames(Schema $schema, $type, $fieldName) private function getSuggestedTypeNames(Schema $schema, $type, $fieldName)
@ -120,6 +121,7 @@ class FieldsOnCorrectType extends ValidationRule
* *
* @param ObjectType|InterfaceType $type * @param ObjectType|InterfaceType $type
* @param string $fieldName * @param string $fieldName
*
* @return array|string[] * @return array|string[]
*/ */
private function getSuggestedFieldNames(Schema $schema, $type, $fieldName) private function getSuggestedFieldNames(Schema $schema, $type, $fieldName)
@ -139,6 +141,7 @@ class FieldsOnCorrectType extends ValidationRule
* @param string $type * @param string $type
* @param string[] $suggestedTypeNames * @param string[] $suggestedTypeNames
* @param string[] $suggestedFieldNames * @param string[] $suggestedFieldNames
*
* @return string * @return string
*/ */
public static function undefinedFieldMessage( public static function undefinedFieldMessage(

View File

@ -19,7 +19,7 @@ class FragmentsOnCompositeTypes extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ return [
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) use ($context) { NodeKind::INLINE_FRAGMENT => static function (InlineFragmentNode $node) use ($context) {
if (! $node->typeCondition) { if (! $node->typeCondition) {
return; return;
} }
@ -34,7 +34,7 @@ class FragmentsOnCompositeTypes extends ValidationRule
[$node->typeCondition] [$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); $type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition);
if (! $type || Type::isCompositeType($type)) { if (! $type || Type::isCompositeType($type)) {

View File

@ -26,7 +26,7 @@ class KnownArgumentNames extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ 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 */ /** @var NodeList|Node[] $ancestors */
$argDef = $context->getArgument(); $argDef = $context->getArgument();
if ($argDef !== null) { if ($argDef !== null) {
@ -46,7 +46,7 @@ class KnownArgumentNames extends ValidationRule
Utils::suggestionList( Utils::suggestionList(
$node->name->value, $node->name->value,
array_map( array_map(
function ($arg) { static function ($arg) {
return $arg->name; return $arg->name;
}, },
$fieldDef->args $fieldDef->args
@ -66,7 +66,7 @@ class KnownArgumentNames extends ValidationRule
Utils::suggestionList( Utils::suggestionList(
$node->name->value, $node->name->value,
array_map( array_map(
function ($arg) { static function ($arg) {
return $arg->name; return $arg->name;
}, },
$directive->args $directive->args

View File

@ -37,7 +37,7 @@ class KnownDirectives extends ValidationRule
continue; continue;
} }
$locationsMap[$def->name->value] = array_map(function ($name) { $locationsMap[$def->name->value] = array_map(static function ($name) {
return $name->value; return $name->value;
}, $def->locations); }, $def->locations);
} }

View File

@ -15,7 +15,7 @@ class KnownFragmentNames extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ return [
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) use ($context) { NodeKind::FRAGMENT_SPREAD => static function (FragmentSpreadNode $node) use ($context) {
$fragmentName = $node->name->value; $fragmentName = $node->name->value;
$fragment = $context->getFragment($fragmentName); $fragment = $context->getFragment($fragmentName);
if ($fragment) { if ($fragment) {

View File

@ -23,7 +23,7 @@ class KnownTypeNames extends ValidationRule
{ {
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
$skip = function () { $skip = static function () {
return Visitor::skipNode(); return Visitor::skipNode();
}; };
@ -35,7 +35,7 @@ class KnownTypeNames extends ValidationRule
NodeKind::INTERFACE_TYPE_DEFINITION => $skip, NodeKind::INTERFACE_TYPE_DEFINITION => $skip,
NodeKind::UNION_TYPE_DEFINITION => $skip, NodeKind::UNION_TYPE_DEFINITION => $skip,
NodeKind::INPUT_OBJECT_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(); $schema = $context->getSchema();
$typeName = $node->name->value; $typeName = $node->name->value;
$type = $schema->getType($typeName); $type = $schema->getType($typeName);

View File

@ -26,17 +26,17 @@ class LoneAnonymousOperation extends ValidationRule
$operationCount = 0; $operationCount = 0;
return [ return [
NodeKind::DOCUMENT => function (DocumentNode $node) use (&$operationCount) { NodeKind::DOCUMENT => static function (DocumentNode $node) use (&$operationCount) {
$tmp = Utils::filter( $tmp = Utils::filter(
$node->definitions, $node->definitions,
function (Node $definition) { static function (Node $definition) {
return $definition->kind === NodeKind::OPERATION_DEFINITION; return $definition->kind === NodeKind::OPERATION_DEFINITION;
} }
); );
$operationCount = count($tmp); $operationCount = count($tmp);
}, },
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use ( NodeKind::OPERATION_DEFINITION => static function (OperationDefinitionNode $node) use (
&$operationCount, &$operationCount,
$context $context
) { ) {

View File

@ -43,7 +43,7 @@ class NoFragmentCycles extends ValidationRule
$this->spreadPathIndexByName = []; $this->spreadPathIndexByName = [];
return [ return [
NodeKind::OPERATION_DEFINITION => function () { NodeKind::OPERATION_DEFINITION => static function () {
return Visitor::skipNode(); return Visitor::skipNode();
}, },
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) { NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) {
@ -98,7 +98,7 @@ class NoFragmentCycles extends ValidationRule
$spreadName, $spreadName,
Utils::map( Utils::map(
$cyclePath, $cyclePath,
function ($s) { static function ($s) {
return $s->name->value; return $s->name->value;
} }
) )

View File

@ -25,10 +25,10 @@ class NoUndefinedVariables extends ValidationRule
return [ return [
NodeKind::OPERATION_DEFINITION => [ NodeKind::OPERATION_DEFINITION => [
'enter' => function () use (&$variableNameDefined) { 'enter' => static function () use (&$variableNameDefined) {
$variableNameDefined = []; $variableNameDefined = [];
}, },
'leave' => function (OperationDefinitionNode $operation) use (&$variableNameDefined, $context) { 'leave' => static function (OperationDefinitionNode $operation) use (&$variableNameDefined, $context) {
$usages = $context->getRecursiveVariableUsages($operation); $usages = $context->getRecursiveVariableUsages($operation);
foreach ($usages as $usage) { 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; $variableNameDefined[$def->variable->name->value] = true;
}, },
]; ];

View File

@ -24,6 +24,7 @@ use GraphQL\Type\Definition\Type;
use GraphQL\Utils\PairSet; use GraphQL\Utils\PairSet;
use GraphQL\Utils\TypeInfo; use GraphQL\Utils\TypeInfo;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
use SplObjectStorage;
use function array_keys; use function array_keys;
use function array_map; use function array_map;
use function array_merge; use function array_merge;
@ -39,6 +40,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* A memoization for when two fragments are compared "between" each other for * A memoization for when two fragments are compared "between" each other for
* conflicts. Two fragments may be compared many times, so memoizing this can * conflicts. Two fragments may be compared many times, so memoizing this can
* dramatically improve the performance of this validator. * dramatically improve the performance of this validator.
*
* @var PairSet * @var PairSet
*/ */
private $comparedFragmentPairs; private $comparedFragmentPairs;
@ -48,14 +50,14 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* selection set. Selection sets may be asked for this information multiple * selection set. Selection sets may be asked for this information multiple
* times, so this improves the performance of this validator. * times, so this improves the performance of this validator.
* *
* @var \SplObjectStorage * @var SplObjectStorage
*/ */
private $cachedFieldsAndFragmentNames; private $cachedFieldsAndFragmentNames;
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
$this->comparedFragmentPairs = new PairSet(); $this->comparedFragmentPairs = new PairSet();
$this->cachedFieldsAndFragmentNames = new \SplObjectStorage(); $this->cachedFieldsAndFragmentNames = new SplObjectStorage();
return [ return [
NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context) { NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context) {
@ -83,6 +85,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* GraphQL Document. * GraphQL Document.
* *
* @param CompositeType $parentType * @param CompositeType $parentType
*
* @return mixed[] * @return mixed[]
*/ */
private function findConflictsWithinSelectionSet( private function findConflictsWithinSelectionSet(
@ -145,7 +148,8 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* referenced via fragment spreads. * referenced via fragment spreads.
* *
* @param CompositeType $parentType * @param CompositeType $parentType
* @return mixed[]|\SplObjectStorage *
* @return mixed[]|SplObjectStorage
*/ */
private function getFieldsAndFragmentNames( private function getFieldsAndFragmentNames(
ValidationContext $context, ValidationContext $context,
@ -224,7 +228,6 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* *
* J) Also, if two fragments are referenced in both selection sets, then a * J) Also, if two fragments are referenced in both selection sets, then a
* comparison is made "between" the two fragments. * comparison is made "between" the two fragments.
*
*/ */
/** /**
@ -333,6 +336,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* @param string $responseName * @param string $responseName
* @param mixed[] $field1 * @param mixed[] $field1
* @param mixed[] $field2 * @param mixed[] $field2
*
* @return mixed[]|null * @return mixed[]|null
*/ */
private function findConflict( private function findConflict(
@ -503,6 +507,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* @param bool $areMutuallyExclusive * @param bool $areMutuallyExclusive
* @param CompositeType $parentType1 * @param CompositeType $parentType1
* @param CompositeType $parentType2 * @param CompositeType $parentType2
*
* @return mixed[][] * @return mixed[][]
*/ */
private function findConflictsBetweenSubSelectionSets( private function findConflictsBetweenSubSelectionSets(
@ -704,7 +709,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* Given a reference to a fragment, return the represented collection of fields * 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. * as well as a list of nested fragment names referenced via fragment spreads.
* *
* @return mixed[]|\SplObjectStorage * @return mixed[]|SplObjectStorage
*/ */
private function getReferencedFieldsAndFragmentNames( private function getReferencedFieldsAndFragmentNames(
ValidationContext $context, ValidationContext $context,
@ -818,6 +823,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
* *
* @param mixed[][] $conflicts * @param mixed[][] $conflicts
* @param string $responseName * @param string $responseName
*
* @return mixed[]|null * @return mixed[]|null
*/ */
private function subfieldConflicts( private function subfieldConflicts(
@ -834,7 +840,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
[ [
$responseName, $responseName,
array_map( array_map(
function ($conflict) { static function ($conflict) {
return $conflict[0]; return $conflict[0];
}, },
$conflicts $conflicts
@ -842,14 +848,14 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
], ],
array_reduce( array_reduce(
$conflicts, $conflicts,
function ($allFields, $conflict) { static function ($allFields, $conflict) {
return array_merge($allFields, $conflict[1]); return array_merge($allFields, $conflict[1]);
}, },
[$ast1] [$ast1]
), ),
array_reduce( array_reduce(
$conflicts, $conflicts,
function ($allFields, $conflict) { static function ($allFields, $conflict) {
return array_merge($allFields, $conflict[2]); return array_merge($allFields, $conflict[2]);
}, },
[$ast2] [$ast2]
@ -876,7 +882,7 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
{ {
if (is_array($reason)) { if (is_array($reason)) {
$tmp = array_map( $tmp = array_map(
function ($tmp) { static function ($tmp) {
[$responseName, $subReason] = $tmp; [$responseName, $subReason] = $tmp;
$reasonMessage = self::reasonMessage($subReason); $reasonMessage = self::reasonMessage($subReason);

View File

@ -19,7 +19,7 @@ class ProvidedNonNullArguments extends ValidationRule
{ {
return [ return [
NodeKind::FIELD => [ NodeKind::FIELD => [
'leave' => function (FieldNode $fieldNode) use ($context) { 'leave' => static function (FieldNode $fieldNode) use ($context) {
$fieldDef = $context->getFieldDef(); $fieldDef = $context->getFieldDef();
if (! $fieldDef) { if (! $fieldDef) {
@ -45,7 +45,7 @@ class ProvidedNonNullArguments extends ValidationRule
}, },
], ],
NodeKind::DIRECTIVE => [ NodeKind::DIRECTIVE => [
'leave' => function (DirectiveNode $directiveNode) use ($context) { 'leave' => static function (DirectiveNode $directiveNode) use ($context) {
$directiveDef = $context->getDirective(); $directiveDef = $context->getDirective();
if (! $directiveDef) { if (! $directiveDef) {
return Visitor::skipNode(); return Visitor::skipNode();

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace GraphQL\Validator\Rules; namespace GraphQL\Validator\Rules;
use ArrayObject;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Executor\Values; use GraphQL\Executor\Values;
use GraphQL\Language\AST\FieldNode; use GraphQL\Language\AST\FieldNode;
@ -31,10 +32,10 @@ class QueryComplexity extends QuerySecurityRule
/** @var mixed[]|null */ /** @var mixed[]|null */
private $rawVariableValues = []; private $rawVariableValues = [];
/** @var \ArrayObject */ /** @var ArrayObject */
private $variableDefs; private $variableDefs;
/** @var \ArrayObject */ /** @var ArrayObject */
private $fieldNodeAndDefs; private $fieldNodeAndDefs;
/** @var ValidationContext */ /** @var ValidationContext */
@ -49,8 +50,8 @@ class QueryComplexity extends QuerySecurityRule
{ {
$this->context = $context; $this->context = $context;
$this->variableDefs = new \ArrayObject(); $this->variableDefs = new ArrayObject();
$this->fieldNodeAndDefs = new \ArrayObject(); $this->fieldNodeAndDefs = new ArrayObject();
$complexity = 0; $complexity = 0;
return $this->invokeIfNeeded( return $this->invokeIfNeeded(
@ -196,7 +197,7 @@ class QueryComplexity extends QuerySecurityRule
throw new Error(implode( throw new Error(implode(
"\n\n", "\n\n",
array_map( array_map(
function ($error) { static function ($error) {
return $error->getMessage(); return $error->getMessage();
}, },
$variableValuesResult['errors'] $variableValuesResult['errors']
@ -251,7 +252,7 @@ class QueryComplexity extends QuerySecurityRule
throw new Error(implode( throw new Error(implode(
"\n\n", "\n\n",
array_map( array_map(
function ($error) { static function ($error) {
return $error->getMessage(); return $error->getMessage();
}, },
$variableValuesResult['errors'] $variableValuesResult['errors']

View File

@ -4,16 +4,18 @@ declare(strict_types=1);
namespace GraphQL\Validator\Rules; namespace GraphQL\Validator\Rules;
use Closure; use ArrayObject;
use GraphQL\Language\AST\FieldNode; use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode; use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode; use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\SelectionSetNode; use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Type\Introspection; use GraphQL\Type\Introspection;
use GraphQL\Utils\TypeInfo; use GraphQL\Utils\TypeInfo;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
use InvalidArgumentException;
use function class_alias; use function class_alias;
use function method_exists; use function method_exists;
use function sprintf; use function sprintf;
@ -34,7 +36,7 @@ abstract class QuerySecurityRule extends ValidationRule
protected function checkIfGreaterOrEqualToZero($name, $value) protected function checkIfGreaterOrEqualToZero($name, $value)
{ {
if ($value < 0) { 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 * @param callable[] $validators
* @return Closure[] *
* @return callable[]
*/ */
protected function invokeIfNeeded(ValidationContext $context, array $validators) protected function invokeIfNeeded(ValidationContext $context, array $validators)
{ {
@ -98,17 +101,17 @@ abstract class QuerySecurityRule extends ValidationRule
* *
* @param Type|null $parentType * @param Type|null $parentType
* *
* @return \ArrayObject * @return ArrayObject
*/ */
protected function collectFieldASTsAndDefs( protected function collectFieldASTsAndDefs(
ValidationContext $context, ValidationContext $context,
$parentType, $parentType,
SelectionSetNode $selectionSet, SelectionSetNode $selectionSet,
?\ArrayObject $visitedFragmentNames = null, ?ArrayObject $visitedFragmentNames = null,
?\ArrayObject $astAndDefs = null ?ArrayObject $astAndDefs = null
) { ) {
$_visitedFragmentNames = $visitedFragmentNames ?: new \ArrayObject(); $_visitedFragmentNames = $visitedFragmentNames ?: new ArrayObject();
$_astAndDefs = $astAndDefs ?: new \ArrayObject(); $_astAndDefs = $astAndDefs ?: new ArrayObject();
foreach ($selectionSet->selections as $selection) { foreach ($selectionSet->selections as $selection) {
switch ($selection->kind) { switch ($selection->kind) {
@ -134,7 +137,7 @@ abstract class QuerySecurityRule extends ValidationRule
} }
$responseName = $this->getFieldName($selection); $responseName = $this->getFieldName($selection);
if (! isset($_astAndDefs[$responseName])) { if (! isset($_astAndDefs[$responseName])) {
$_astAndDefs[$responseName] = new \ArrayObject(); $_astAndDefs[$responseName] = new ArrayObject();
} }
// create field context // create field context
$_astAndDefs[$responseName][] = [$selection, $fieldDef]; $_astAndDefs[$responseName][] = [$selection, $fieldDef];

View File

@ -16,7 +16,7 @@ class ScalarLeafs extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ return [
NodeKind::FIELD => function (FieldNode $node) use ($context) { NodeKind::FIELD => static function (FieldNode $node) use ($context) {
$type = $context->getType(); $type = $context->getType();
if (! $type) { if (! $type) {
return; return;

View File

@ -15,7 +15,7 @@ class UniqueDirectivesPerLocation extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ return [
'enter' => function (Node $node) use ($context) { 'enter' => static function (Node $node) use ($context) {
if (! isset($node->directives)) { if (! isset($node->directives)) {
return; return;
} }

View File

@ -22,7 +22,7 @@ class UniqueFragmentNames extends ValidationRule
$this->knownFragmentNames = []; $this->knownFragmentNames = [];
return [ return [
NodeKind::OPERATION_DEFINITION => function () { NodeKind::OPERATION_DEFINITION => static function () {
return Visitor::skipNode(); return Visitor::skipNode();
}, },
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) { NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) {

View File

@ -38,7 +38,7 @@ class UniqueOperationNames extends ValidationRule
return Visitor::skipNode(); return Visitor::skipNode();
}, },
NodeKind::FRAGMENT_DEFINITION => function () { NodeKind::FRAGMENT_DEFINITION => static function () {
return Visitor::skipNode(); return Visitor::skipNode();
}, },
]; ];

View File

@ -6,7 +6,6 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
use function class_alias; use function class_alias;
use function get_class;
abstract class ValidationRule abstract class ValidationRule
{ {
@ -15,7 +14,7 @@ abstract class ValidationRule
public function getName() public function getName()
{ {
return $this->name ?: get_class($this); return $this->name ?: static::class;
} }
public function __invoke(ValidationContext $context) public function __invoke(ValidationContext $context)
@ -27,6 +26,7 @@ abstract class ValidationRule
* Returns structure suitable for GraphQL\Language\Visitor * Returns structure suitable for GraphQL\Language\Visitor
* *
* @see \GraphQL\Language\Visitor * @see \GraphQL\Language\Visitor
*
* @return mixed[] * @return mixed[]
*/ */
abstract public function getVisitor(ValidationContext $context); abstract public function getVisitor(ValidationContext $context);

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace GraphQL\Validator\Rules; namespace GraphQL\Validator\Rules;
use Exception;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Language\AST\BooleanValueNode; use GraphQL\Language\AST\BooleanValueNode;
use GraphQL\Language\AST\EnumValueNode; use GraphQL\Language\AST\EnumValueNode;
@ -27,6 +28,7 @@ use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
use Throwable;
use function array_combine; use function array_combine;
use function array_keys; use function array_keys;
use function array_map; use function array_map;
@ -45,7 +47,7 @@ class ValuesOfCorrectType extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ return [
NodeKind::NULL => function (NullValueNode $node) use ($context) { NodeKind::NULL => static function (NullValueNode $node) use ($context) {
$type = $context->getInputType(); $type = $context->getInputType();
if (! ($type instanceof NonNull)) { if (! ($type instanceof NonNull)) {
return; return;
@ -82,7 +84,7 @@ class ValuesOfCorrectType extends ValidationRule
$nodeFields = iterator_to_array($node->fields); $nodeFields = iterator_to_array($node->fields);
$fieldNodeMap = array_combine( $fieldNodeMap = array_combine(
array_map( array_map(
function ($field) { static function ($field) {
return $field->name->value; return $field->name->value;
}, },
$nodeFields $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()); $parentType = Type::getNamedType($context->getParentInputType());
$fieldType = $context->getInputType(); $fieldType = $context->getInputType();
if ($fieldType || ! ($parentType instanceof InputObjectType)) { if ($fieldType || ! ($parentType instanceof InputObjectType)) {
@ -193,7 +195,7 @@ class ValuesOfCorrectType extends ValidationRule
// may throw to indicate failure. // may throw to indicate failure.
try { try {
$type->parseLiteral($node); $type->parseLiteral($node);
} catch (\Exception $error) { } catch (Exception $error) {
// Ensure a reference to the original error is maintained. // Ensure a reference to the original error is maintained.
$context->reportError( $context->reportError(
new Error( new Error(
@ -209,7 +211,7 @@ class ValuesOfCorrectType extends ValidationRule
$error $error
) )
); );
} catch (\Throwable $error) { } catch (Throwable $error) {
// Ensure a reference to the original error is maintained. // Ensure a reference to the original error is maintained.
$context->reportError( $context->reportError(
new Error( new Error(
@ -234,7 +236,7 @@ class ValuesOfCorrectType extends ValidationRule
$suggestions = Utils::suggestionList( $suggestions = Utils::suggestionList(
Printer::doPrint($node), Printer::doPrint($node),
array_map( array_map(
function (EnumValueDefinition $value) { static function (EnumValueDefinition $value) {
return $value->name; return $value->name;
}, },
$type->getValues() $type->getValues()

View File

@ -18,7 +18,7 @@ class VariablesAreInputTypes extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ 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); $type = TypeInfo::typeFromAST($context->getSchema(), $node->type);
// If the variable type is not an input type, return an error. // If the variable type is not an input type, return an error.

View File

@ -25,7 +25,7 @@ class VariablesDefaultValueAllowed extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
return [ return [
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use ($context) { NodeKind::VARIABLE_DEFINITION => static function (VariableDefinitionNode $node) use ($context) {
$name = $node->variable->name->value; $name = $node->variable->name->value;
$defaultValue = $node->defaultValue; $defaultValue = $node->defaultValue;
$type = $context->getInputType(); $type = $context->getInputType();
@ -44,10 +44,10 @@ class VariablesDefaultValueAllowed extends ValidationRule
return Visitor::skipNode(); return Visitor::skipNode();
}, },
NodeKind::SELECTION_SET => function (SelectionSetNode $node) { NodeKind::SELECTION_SET => static function (SelectionSetNode $node) {
return Visitor::skipNode(); return Visitor::skipNode();
}, },
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) { NodeKind::FRAGMENT_DEFINITION => static function (FragmentDefinitionNode $node) {
return Visitor::skipNode(); return Visitor::skipNode();
}, },
]; ];

View File

@ -71,7 +71,7 @@ class VariablesInAllowedPosition extends ValidationRule
private function effectiveType($varType, $varDef) 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);
} }
/** /**

View File

@ -128,10 +128,10 @@ class ValidationContext
Visitor::visitWithTypeInfo( Visitor::visitWithTypeInfo(
$typeInfo, $typeInfo,
[ [
NodeKind::VARIABLE_DEFINITION => function () { NodeKind::VARIABLE_DEFINITION => static function () {
return false; return false;
}, },
NodeKind::VARIABLE => function (VariableNode $variable) use ( NodeKind::VARIABLE => static function (VariableNode $variable) use (
&$newUsages, &$newUsages,
$typeInfo $typeInfo
) { ) {
@ -214,6 +214,7 @@ class ValidationContext
/** /**
* @param string $name * @param string $name
*
* @return FragmentDefinitionNode|null * @return FragmentDefinitionNode|null
*/ */
public function getFragment($name) public function getFragment($name)