From 2cb68f06cb814049df0e72fd2eaf72093b3e225b Mon Sep 17 00:00:00 2001 From: barchenkov Date: Wed, 5 Aug 2020 19:59:04 +0300 Subject: [PATCH 1/2] Validation messages text in class public static properties (#711) --- src/Validator/Rules/DisableIntrospection.php | 5 +++- src/Validator/Rules/ExecutableDefinitions.php | 5 +++- src/Validator/Rules/FieldsOnCorrectType.php | 15 ++++++++-- .../Rules/FragmentsOnCompositeTypes.php | 10 +++++-- src/Validator/Rules/KnownArgumentNames.php | 10 +++++-- .../Rules/KnownArgumentNamesOnDirectives.php | 10 +++++-- src/Validator/Rules/KnownDirectives.php | 10 +++++-- src/Validator/Rules/KnownFragmentNames.php | 5 +++- src/Validator/Rules/KnownTypeNames.php | 10 +++++-- .../Rules/LoneAnonymousOperation.php | 5 +++- src/Validator/Rules/LoneSchemaDefinition.php | 10 +++++-- src/Validator/Rules/NoFragmentCycles.php | 5 +++- src/Validator/Rules/NoUndefinedVariables.php | 10 +++++-- src/Validator/Rules/NoUnusedFragments.php | 5 +++- src/Validator/Rules/NoUnusedVariables.php | 10 +++++-- .../Rules/OverlappingFieldsCanBeMerged.php | 10 +++++-- .../Rules/PossibleFragmentSpreads.php | 10 +++++-- .../Rules/ProvidedRequiredArguments.php | 5 +++- .../ProvidedRequiredArgumentsOnDirectives.php | 7 +++-- src/Validator/Rules/QueryComplexity.php | 5 +++- src/Validator/Rules/QueryDepth.php | 5 +++- src/Validator/Rules/ScalarLeafs.php | 10 +++++-- .../Rules/SingleFieldSubscription.php | 10 +++++-- src/Validator/Rules/UniqueArgumentNames.php | 5 +++- .../Rules/UniqueDirectivesPerLocation.php | 5 +++- src/Validator/Rules/UniqueFragmentNames.php | 5 +++- src/Validator/Rules/UniqueInputFieldNames.php | 5 +++- src/Validator/Rules/UniqueOperationNames.php | 5 +++- src/Validator/Rules/UniqueVariableNames.php | 5 +++- src/Validator/Rules/ValuesOfCorrectType.php | 30 +++++++++++++++---- .../Rules/VariablesAreInputTypes.php | 5 +++- .../Rules/VariablesInAllowedPosition.php | 5 +++- 32 files changed, 205 insertions(+), 52 deletions(-) diff --git a/src/Validator/Rules/DisableIntrospection.php b/src/Validator/Rules/DisableIntrospection.php index 01a93183b..ea7581dce 100644 --- a/src/Validator/Rules/DisableIntrospection.php +++ b/src/Validator/Rules/DisableIntrospection.php @@ -13,6 +13,9 @@ class DisableIntrospection extends QuerySecurityRule { public const ENABLED = 1; + /** @var string */ + public static $introspectionDisabledMessage = 'GraphQL introspection is not allowed, but the query contained __schema or __type'; + /** @var bool */ private $isEnabled; @@ -47,7 +50,7 @@ public function getVisitor(ValidationContext $context) public static function introspectionDisabledMessage() { - return 'GraphQL introspection is not allowed, but the query contained __schema or __type'; + return static::$introspectionDisabledMessage; } protected function isEnabled() diff --git a/src/Validator/Rules/ExecutableDefinitions.php b/src/Validator/Rules/ExecutableDefinitions.php index 5966df7ef..6da2e3bca 100644 --- a/src/Validator/Rules/ExecutableDefinitions.php +++ b/src/Validator/Rules/ExecutableDefinitions.php @@ -22,6 +22,9 @@ */ class ExecutableDefinitions extends ValidationRule { + /** @var string */ + public static $nonExecutableDefinitionMessage = 'The "%s" definition is not executable.'; + public function getVisitor(ValidationContext $context) { return [ @@ -45,6 +48,6 @@ public function getVisitor(ValidationContext $context) public static function nonExecutableDefinitionMessage($defName) { - return sprintf('The "%s" definition is not executable.', $defName); + return sprintf(static::$nonExecutableDefinitionMessage, $defName); } } diff --git a/src/Validator/Rules/FieldsOnCorrectType.php b/src/Validator/Rules/FieldsOnCorrectType.php index d4d4c7288..2fb85a36c 100644 --- a/src/Validator/Rules/FieldsOnCorrectType.php +++ b/src/Validator/Rules/FieldsOnCorrectType.php @@ -21,6 +21,15 @@ class FieldsOnCorrectType extends ValidationRule { + /** @var string */ + public static $undefinedFieldMessage = 'Cannot query field "%s" on type "%s".'; + + /** @var string */ + public static $undefinedFieldInlineFragmentMessage = ' Did you mean to use an inline fragment on %s?'; + + /** @var string */ + public static $didYouMeanMessage = ' Did you mean %s?'; + public function getVisitor(ValidationContext $context) { return [ @@ -151,16 +160,16 @@ public static function undefinedFieldMessage( array $suggestedTypeNames, array $suggestedFieldNames ) { - $message = sprintf('Cannot query field "%s" on type "%s".', $fieldName, $type); + $message = sprintf(static::$undefinedFieldMessage, $fieldName, $type); if ($suggestedTypeNames) { $suggestions = Utils::quotedOrList($suggestedTypeNames); - $message .= sprintf(' Did you mean to use an inline fragment on %s?', $suggestions); + $message .= sprintf(static::$undefinedFieldInlineFragmentMessage, $suggestions); } elseif (count($suggestedFieldNames) > 0) { $suggestions = Utils::quotedOrList($suggestedFieldNames); - $message .= sprintf(' Did you mean %s?', $suggestions); + $message .= sprintf(static::$didYouMeanMessage, $suggestions); } return $message; diff --git a/src/Validator/Rules/FragmentsOnCompositeTypes.php b/src/Validator/Rules/FragmentsOnCompositeTypes.php index db72a057b..ec5643fdc 100644 --- a/src/Validator/Rules/FragmentsOnCompositeTypes.php +++ b/src/Validator/Rules/FragmentsOnCompositeTypes.php @@ -16,6 +16,12 @@ class FragmentsOnCompositeTypes extends ValidationRule { + /** @var string */ + public static $inlineFragmentOnNonCompositeErrorMessage = 'Fragment cannot condition on non composite type "%s".'; + + /** @var string */ + public static $fragmentOnNonCompositeErrorMessage = 'Fragment "%s" cannot condition on non composite type "%s".'; + public function getVisitor(ValidationContext $context) { return [ @@ -54,11 +60,11 @@ public function getVisitor(ValidationContext $context) public static function inlineFragmentOnNonCompositeErrorMessage($type) { - return sprintf('Fragment cannot condition on non composite type "%s".', $type); + return sprintf(static::$inlineFragmentOnNonCompositeErrorMessage, $type); } public static function fragmentOnNonCompositeErrorMessage($fragName, $type) { - return sprintf('Fragment "%s" cannot condition on non composite type "%s".', $fragName, $type); + return sprintf(static::$fragmentOnNonCompositeErrorMessage, $fragName, $type); } } diff --git a/src/Validator/Rules/KnownArgumentNames.php b/src/Validator/Rules/KnownArgumentNames.php index d3013797a..9b554e6b8 100644 --- a/src/Validator/Rules/KnownArgumentNames.php +++ b/src/Validator/Rules/KnownArgumentNames.php @@ -25,6 +25,12 @@ */ class KnownArgumentNames extends ValidationRule { + /** @var string */ + public static $unknownArgMessage = 'Unknown argument "%s" on field "%s" of type "%s".'; + + /** @var string */ + public static $didYouMeanMessage = ' Did you mean %s?'; + public function getVisitor(ValidationContext $context) { $knownArgumentNamesOnDirectives = new KnownArgumentNamesOnDirectives(); @@ -70,9 +76,9 @@ static function ($arg) : string { */ public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs) { - $message = sprintf('Unknown argument "%s" on field "%s" of type "%s".', $argName, $fieldName, $typeName); + $message = sprintf(static::$unknownArgMessage, $argName, $fieldName, $typeName); if (isset($suggestedArgs[0])) { - $message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs)); + $message .= sprintf(static::$didYouMeanMessage, Utils::quotedOrList($suggestedArgs)); } return $message; diff --git a/src/Validator/Rules/KnownArgumentNamesOnDirectives.php b/src/Validator/Rules/KnownArgumentNamesOnDirectives.php index bdde5d36d..8601f800c 100644 --- a/src/Validator/Rules/KnownArgumentNamesOnDirectives.php +++ b/src/Validator/Rules/KnownArgumentNamesOnDirectives.php @@ -29,14 +29,20 @@ */ class KnownArgumentNamesOnDirectives extends ValidationRule { + /** @var string */ + public static $unknownDirectiveArgMessage = 'Unknown argument "%s" on directive "@%s".'; + + /** @var string */ + public static $didYouMeanMessage = ' Did you mean %s?'; + /** * @param string[] $suggestedArgs */ public static function unknownDirectiveArgMessage($argName, $directiveName, array $suggestedArgs) { - $message = sprintf('Unknown argument "%s" on directive "@%s".', $argName, $directiveName); + $message = sprintf(static::$unknownDirectiveArgMessage, $argName, $directiveName); if (isset($suggestedArgs[0])) { - $message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs)); + $message .= sprintf(static::$didYouMeanMessage, Utils::quotedOrList($suggestedArgs)); } return $message; diff --git a/src/Validator/Rules/KnownDirectives.php b/src/Validator/Rules/KnownDirectives.php index 6dbf8497e..da0030219 100644 --- a/src/Validator/Rules/KnownDirectives.php +++ b/src/Validator/Rules/KnownDirectives.php @@ -47,6 +47,12 @@ class KnownDirectives extends ValidationRule { + /** @var string */ + public static $unknownDirectiveMessage = 'Unknown directive "%s".'; + + /** @var string */ + public static $misplacedDirectiveMessage = 'Directive "%s" may not be used on "%s".'; + public function getVisitor(ValidationContext $context) { return $this->getASTVisitor($context); @@ -124,7 +130,7 @@ static function ($name) : string { public static function unknownDirectiveMessage($directiveName) { - return sprintf('Unknown directive "%s".', $directiveName); + return sprintf(static::$unknownDirectiveMessage, $directiveName); } /** @@ -194,6 +200,6 @@ private function getDirectiveLocationForASTPath(array $ancestors) public static function misplacedDirectiveMessage($directiveName, $location) { - return sprintf('Directive "%s" may not be used on "%s".', $directiveName, $location); + return sprintf(static::$misplacedDirectiveMessage, $directiveName, $location); } } diff --git a/src/Validator/Rules/KnownFragmentNames.php b/src/Validator/Rules/KnownFragmentNames.php index 052686f28..a2f5aab1f 100644 --- a/src/Validator/Rules/KnownFragmentNames.php +++ b/src/Validator/Rules/KnownFragmentNames.php @@ -12,6 +12,9 @@ class KnownFragmentNames extends ValidationRule { + /** @var string */ + public static $unknownFragmentMessage = 'Unknown fragment "%s".'; + public function getVisitor(ValidationContext $context) { return [ @@ -35,6 +38,6 @@ public function getVisitor(ValidationContext $context) */ public static function unknownFragmentMessage($fragName) { - return sprintf('Unknown fragment "%s".', $fragName); + return sprintf(static::$unknownFragmentMessage, $fragName); } } diff --git a/src/Validator/Rules/KnownTypeNames.php b/src/Validator/Rules/KnownTypeNames.php index b852f523b..436556f5d 100644 --- a/src/Validator/Rules/KnownTypeNames.php +++ b/src/Validator/Rules/KnownTypeNames.php @@ -23,6 +23,12 @@ */ class KnownTypeNames extends ValidationRule { + /** @var string */ + public static $unknownTypeMessage = 'Unknown type "%s".'; + + /** @var string */ + public static $didYouMeanMessage = ' Did you mean %s?'; + public function getVisitor(ValidationContext $context) { $skip = static function () : VisitorOperation { @@ -62,11 +68,11 @@ public function getVisitor(ValidationContext $context) */ public static function unknownTypeMessage($type, array $suggestedTypes) { - $message = sprintf('Unknown type "%s".', $type); + $message = sprintf(static::$unknownTypeMessage, $type); if (count($suggestedTypes) > 0) { $suggestions = Utils::quotedOrList($suggestedTypes); - $message .= sprintf(' Did you mean %s?', $suggestions); + $message .= sprintf(static::$didYouMeanMessage, $suggestions); } return $message; diff --git a/src/Validator/Rules/LoneAnonymousOperation.php b/src/Validator/Rules/LoneAnonymousOperation.php index facc895a3..7e0d7d1e7 100644 --- a/src/Validator/Rules/LoneAnonymousOperation.php +++ b/src/Validator/Rules/LoneAnonymousOperation.php @@ -21,6 +21,9 @@ */ class LoneAnonymousOperation extends ValidationRule { + /** @var string */ + public static $anonOperationNotAloneMessage = 'This anonymous operation must be the only defined operation.'; + public function getVisitor(ValidationContext $context) { $operationCount = 0; @@ -53,6 +56,6 @@ static function (Node $definition) : bool { public static function anonOperationNotAloneMessage() { - return 'This anonymous operation must be the only defined operation.'; + return static::$anonOperationNotAloneMessage; } } diff --git a/src/Validator/Rules/LoneSchemaDefinition.php b/src/Validator/Rules/LoneSchemaDefinition.php index 4ece976b6..bf2a014b8 100644 --- a/src/Validator/Rules/LoneSchemaDefinition.php +++ b/src/Validator/Rules/LoneSchemaDefinition.php @@ -16,14 +16,20 @@ */ class LoneSchemaDefinition extends ValidationRule { + /** @var string */ + public static $schemaDefinitionNotAloneMessage = 'Must provide only one schema definition.'; + + /** @var string */ + public static $canNotDefineSchemaWithinExtensionMessage = 'Cannot define a new schema within a schema extension.'; + public static function schemaDefinitionNotAloneMessage() { - return 'Must provide only one schema definition.'; + return static::$schemaDefinitionNotAloneMessage; } public static function canNotDefineSchemaWithinExtensionMessage() { - return 'Cannot define a new schema within a schema extension.'; + return static::$canNotDefineSchemaWithinExtensionMessage; } public function getSDLVisitor(SDLValidationContext $context) diff --git a/src/Validator/Rules/NoFragmentCycles.php b/src/Validator/Rules/NoFragmentCycles.php index eec546dea..1d5011cb0 100644 --- a/src/Validator/Rules/NoFragmentCycles.php +++ b/src/Validator/Rules/NoFragmentCycles.php @@ -20,6 +20,9 @@ class NoFragmentCycles extends ValidationRule { + /** @var string */ + public static $cycleErrorMessage = 'Cannot spread fragment "%s" within itself%s.'; + /** @var bool[] */ public $visitedFrags; @@ -104,7 +107,7 @@ private function detectCycleRecursive(FragmentDefinitionNode $fragment, Validati public static function cycleErrorMessage($fragName, array $spreadNames = []) { return sprintf( - 'Cannot spread fragment "%s" within itself%s.', + static::$cycleErrorMessage, $fragName, count($spreadNames) > 0 ? ' via ' . implode(', ', $spreadNames) : '' ); diff --git a/src/Validator/Rules/NoUndefinedVariables.php b/src/Validator/Rules/NoUndefinedVariables.php index 078990c71..0aad6b7c2 100644 --- a/src/Validator/Rules/NoUndefinedVariables.php +++ b/src/Validator/Rules/NoUndefinedVariables.php @@ -17,6 +17,12 @@ */ class NoUndefinedVariables extends ValidationRule { + /** @var string */ + public static $undefinedVarByOperationMessage = 'Variable "$%s" is not defined by operation "%s".'; + + /** @var string */ + public static $undefinedVarMessage = 'Variable "$%s" is not defined.'; + public function getVisitor(ValidationContext $context) { $variableNameDefined = []; @@ -58,7 +64,7 @@ public function getVisitor(ValidationContext $context) public static function undefinedVarMessage($varName, $opName = null) { return $opName - ? sprintf('Variable "$%s" is not defined by operation "%s".', $varName, $opName) - : sprintf('Variable "$%s" is not defined.', $varName); + ? sprintf(static::$undefinedVarByOperationMessage, $varName, $opName) + : sprintf(static::$undefinedVarMessage, $varName); } } diff --git a/src/Validator/Rules/NoUnusedFragments.php b/src/Validator/Rules/NoUnusedFragments.php index 4315c5471..02ba69cf6 100644 --- a/src/Validator/Rules/NoUnusedFragments.php +++ b/src/Validator/Rules/NoUnusedFragments.php @@ -15,6 +15,9 @@ class NoUnusedFragments extends ValidationRule { + /** @var string */ + public static $unusedFragMessage = 'Fragment "%s" is never used.'; + /** @var OperationDefinitionNode[] */ public $operationDefs; @@ -65,6 +68,6 @@ public function getVisitor(ValidationContext $context) public static function unusedFragMessage($fragName) { - return sprintf('Fragment "%s" is never used.', $fragName); + return sprintf(static::$unusedFragMessage, $fragName); } } diff --git a/src/Validator/Rules/NoUnusedVariables.php b/src/Validator/Rules/NoUnusedVariables.php index 343a969ba..5854d56ad 100644 --- a/src/Validator/Rules/NoUnusedVariables.php +++ b/src/Validator/Rules/NoUnusedVariables.php @@ -13,6 +13,12 @@ class NoUnusedVariables extends ValidationRule { + /** @var string */ + public static $unusedVariableInOperationMessage = 'Variable "$%s" is never used in operation "%s".'; + + /** @var string */ + public static $unusedVariableMessage = 'Variable "$%s" is never used.'; + /** @var VariableDefinitionNode[] */ public $variableDefs; @@ -60,7 +66,7 @@ public function getVisitor(ValidationContext $context) public static function unusedVariableMessage($varName, $opName = null) { return $opName - ? sprintf('Variable "$%s" is never used in operation "%s".', $varName, $opName) - : sprintf('Variable "$%s" is never used.', $varName); + ? sprintf(static::$unusedVariableInOperationMessage, $varName, $opName) + : sprintf(static::$unusedVariableMessage, $varName); } } diff --git a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php index 1817d0144..1c8c2be1b 100644 --- a/src/Validator/Rules/OverlappingFieldsCanBeMerged.php +++ b/src/Validator/Rules/OverlappingFieldsCanBeMerged.php @@ -35,6 +35,12 @@ class OverlappingFieldsCanBeMerged extends ValidationRule { + /** @var string */ + public static $fieldsConflictMessage = 'Fields "%s" conflict because %s. Use different aliases on the fields to fetch both if this was intentional.'; + + /** @var string */ + public static $reasonMessage = 'subfields "%s" conflict because %s'; + /** * A memoization for when two fragments are compared "between" each other for * conflicts. Two fragments may be compared many times, so memoizing this can @@ -869,7 +875,7 @@ public static function fieldsConflictMessage($responseName, $reason) $reasonMessage = self::reasonMessage($reason); return sprintf( - 'Fields "%s" conflict because %s. Use different aliases on the fields to fetch both if this was intentional.', + static::$fieldsConflictMessage, $responseName, $reasonMessage ); @@ -884,7 +890,7 @@ static function ($tmp) : string { $reasonMessage = self::reasonMessage($subReason); - return sprintf('subfields "%s" conflict because %s', $responseName, $reasonMessage); + return sprintf(static::$reasonMessage, $responseName, $reasonMessage); }, $reason ); diff --git a/src/Validator/Rules/PossibleFragmentSpreads.php b/src/Validator/Rules/PossibleFragmentSpreads.php index 184f58bec..a3ec5ff52 100644 --- a/src/Validator/Rules/PossibleFragmentSpreads.php +++ b/src/Validator/Rules/PossibleFragmentSpreads.php @@ -20,6 +20,12 @@ class PossibleFragmentSpreads extends ValidationRule { + /** @var string */ + public static $typeIncompatibleAnonSpreadMessage = 'Fragment cannot be spread here as objects of type "%s" can never be of type "%s".'; + + /** @var string */ + public static $typeIncompatibleSpreadMessage = 'Fragment "%s" cannot be spread here as objects of type "%s" can never be of type "%s".'; + public function getVisitor(ValidationContext $context) { return [ @@ -129,7 +135,7 @@ private function doTypesOverlap(Schema $schema, CompositeType $fragType, Composi public static function typeIncompatibleAnonSpreadMessage($parentType, $fragType) { return sprintf( - 'Fragment cannot be spread here as objects of type "%s" can never be of type "%s".', + static::$typeIncompatibleAnonSpreadMessage, $parentType, $fragType ); @@ -151,7 +157,7 @@ private function getFragmentType(ValidationContext $context, $name) public static function typeIncompatibleSpreadMessage($fragName, $parentType, $fragType) { return sprintf( - 'Fragment "%s" cannot be spread here as objects of type "%s" can never be of type "%s".', + static::$typeIncompatibleSpreadMessage, $fragName, $parentType, $fragType diff --git a/src/Validator/Rules/ProvidedRequiredArguments.php b/src/Validator/Rules/ProvidedRequiredArguments.php index 77a4aa5a8..920230e9c 100644 --- a/src/Validator/Rules/ProvidedRequiredArguments.php +++ b/src/Validator/Rules/ProvidedRequiredArguments.php @@ -14,6 +14,9 @@ class ProvidedRequiredArguments extends ValidationRule { + /** @var string */ + public static $missingFieldArgMessage = 'Field "%s" argument "%s" of type "%s" is required but not provided.'; + public function getVisitor(ValidationContext $context) { $providedRequiredArgumentsOnDirectives = new ProvidedRequiredArgumentsOnDirectives(); @@ -53,7 +56,7 @@ public function getVisitor(ValidationContext $context) public static function missingFieldArgMessage($fieldName, $argName, $type) { return sprintf( - 'Field "%s" argument "%s" of type "%s" is required but not provided.', + static::$missingFieldArgMessage, $fieldName, $argName, $type diff --git a/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php b/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php index f8b34c713..915b30584 100644 --- a/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php +++ b/src/Validator/Rules/ProvidedRequiredArgumentsOnDirectives.php @@ -19,6 +19,7 @@ use GraphQL\Validator\SDLValidationContext; use GraphQL\Validator\ValidationContext; use function array_filter; +use function sprintf; /** * Provided required arguments on directives @@ -28,10 +29,12 @@ */ class ProvidedRequiredArgumentsOnDirectives extends ValidationRule { + /** @var string */ + public static $missingDirectiveArgMessage = 'Directive "@%s" argument "%s" of type "%s" is required but not provided.'; + public static function missingDirectiveArgMessage(string $directiveName, string $argName, string $type) { - return 'Directive "@' . $directiveName . '" argument "' . $argName - . '" of type "' . $type . '" is required but not provided.'; + return sprintf(static::$missingDirectiveArgMessage, $directiveName, $argName, $type); } public function getSDLVisitor(SDLValidationContext $context) diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index f95fe61e5..fe75bb8e4 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -28,6 +28,9 @@ class QueryComplexity extends QuerySecurityRule { + /** @var string */ + public static $maxQueryComplexityErrorMessage = 'Max query complexity should be %d but got %d.'; + /** @var int */ private $maxQueryComplexity; @@ -291,7 +294,7 @@ public function setMaxQueryComplexity($maxQueryComplexity) public static function maxQueryComplexityErrorMessage($max, $count) { - return sprintf('Max query complexity should be %d but got %d.', $max, $count); + return sprintf(static::$maxQueryComplexityErrorMessage, $max, $count); } protected function isEnabled() diff --git a/src/Validator/Rules/QueryDepth.php b/src/Validator/Rules/QueryDepth.php index 9b83d061e..3977ca0c9 100644 --- a/src/Validator/Rules/QueryDepth.php +++ b/src/Validator/Rules/QueryDepth.php @@ -17,6 +17,9 @@ class QueryDepth extends QuerySecurityRule { + /** @var string */ + public static $maxQueryDepthErrorMessage = 'Max query depth should be %d but got %d.'; + /** @var int */ private $maxQueryDepth; @@ -108,7 +111,7 @@ public function setMaxQueryDepth($maxQueryDepth) public static function maxQueryDepthErrorMessage($max, $count) { - return sprintf('Max query depth should be %d but got %d.', $max, $count); + return sprintf(static::$maxQueryDepthErrorMessage, $max, $count); } protected function isEnabled() diff --git a/src/Validator/Rules/ScalarLeafs.php b/src/Validator/Rules/ScalarLeafs.php index f1714239e..65037a601 100644 --- a/src/Validator/Rules/ScalarLeafs.php +++ b/src/Validator/Rules/ScalarLeafs.php @@ -13,6 +13,12 @@ class ScalarLeafs extends ValidationRule { + /** @var string */ + public static $noSubselectionAllowedMessage = 'Field "%s" of type "%s" must not have a sub selection.'; + + /** @var string */ + public static $requiredSubselectionMessage = 'Field "%s" of type "%s" must have a sub selection.'; + public function getVisitor(ValidationContext $context) { return [ @@ -41,11 +47,11 @@ public function getVisitor(ValidationContext $context) public static function noSubselectionAllowedMessage($field, $type) { - return sprintf('Field "%s" of type "%s" must not have a sub selection.', $field, $type); + return sprintf(static::$noSubselectionAllowedMessage, $field, $type); } public static function requiredSubselectionMessage($field, $type) { - return sprintf('Field "%s" of type "%s" must have a sub selection.', $field, $type); + return sprintf(static::$requiredSubselectionMessage, $field, $type); } } diff --git a/src/Validator/Rules/SingleFieldSubscription.php b/src/Validator/Rules/SingleFieldSubscription.php index b98ee95ed..cbba74955 100644 --- a/src/Validator/Rules/SingleFieldSubscription.php +++ b/src/Validator/Rules/SingleFieldSubscription.php @@ -17,6 +17,12 @@ class SingleFieldSubscription extends ValidationRule { + /** @var string */ + public static $multipleFieldsInOperationAnonymousMessage = 'Anonymous Subscription must select only one top level field.'; + + /** @var string */ + public static $multipleFieldsInOperationMessage = 'Subscription "%s" must select only one top level field.'; + /** * @return array */ @@ -49,9 +55,9 @@ public function getVisitor(ValidationContext $context) : array public static function multipleFieldsInOperation(?string $operationName) : string { if ($operationName === null) { - return sprintf('Anonymous Subscription must select only one top level field.'); + return sprintf(static::$multipleFieldsInOperationAnonymousMessage); } - return sprintf('Subscription "%s" must select only one top level field.', $operationName); + return sprintf(static::$multipleFieldsInOperationMessage, $operationName); } } diff --git a/src/Validator/Rules/UniqueArgumentNames.php b/src/Validator/Rules/UniqueArgumentNames.php index 58daecf68..43085db86 100644 --- a/src/Validator/Rules/UniqueArgumentNames.php +++ b/src/Validator/Rules/UniqueArgumentNames.php @@ -17,6 +17,9 @@ class UniqueArgumentNames extends ValidationRule { + /** @var string */ + public static $duplicateArgMessage = 'There can be only one argument named "%s".'; + /** @var NameNode[] */ public $knownArgNames; @@ -59,6 +62,6 @@ public function getASTVisitor(ASTValidationContext $context) public static function duplicateArgMessage($argName) { - return sprintf('There can be only one argument named "%s".', $argName); + return sprintf(static::$duplicateArgMessage, $argName); } } diff --git a/src/Validator/Rules/UniqueDirectivesPerLocation.php b/src/Validator/Rules/UniqueDirectivesPerLocation.php index 02844af73..97ee031a7 100644 --- a/src/Validator/Rules/UniqueDirectivesPerLocation.php +++ b/src/Validator/Rules/UniqueDirectivesPerLocation.php @@ -22,6 +22,9 @@ */ class UniqueDirectivesPerLocation extends ValidationRule { + /** @var string */ + public static $duplicateDirectiveMessage = 'The directive "%s" can only be used once at this location.'; + public function getVisitor(ValidationContext $context) { return $this->getASTVisitor($context); @@ -84,6 +87,6 @@ public function getASTVisitor(ASTValidationContext $context) public static function duplicateDirectiveMessage($directiveName) { - return sprintf('The directive "%s" can only be used once at this location.', $directiveName); + return sprintf(static::$duplicateDirectiveMessage, $directiveName); } } diff --git a/src/Validator/Rules/UniqueFragmentNames.php b/src/Validator/Rules/UniqueFragmentNames.php index e9dba8a92..64d83ec4e 100644 --- a/src/Validator/Rules/UniqueFragmentNames.php +++ b/src/Validator/Rules/UniqueFragmentNames.php @@ -15,6 +15,9 @@ class UniqueFragmentNames extends ValidationRule { + /** @var string */ + public static $duplicateFragmentNameMessage = 'There can be only one fragment named "%s".'; + /** @var NameNode[] */ public $knownFragmentNames; @@ -44,6 +47,6 @@ public function getVisitor(ValidationContext $context) public static function duplicateFragmentNameMessage($fragName) { - return sprintf('There can be only one fragment named "%s".', $fragName); + return sprintf(static::$duplicateFragmentNameMessage, $fragName); } } diff --git a/src/Validator/Rules/UniqueInputFieldNames.php b/src/Validator/Rules/UniqueInputFieldNames.php index 541a37293..b53b9f398 100644 --- a/src/Validator/Rules/UniqueInputFieldNames.php +++ b/src/Validator/Rules/UniqueInputFieldNames.php @@ -18,6 +18,9 @@ class UniqueInputFieldNames extends ValidationRule { + /** @var string */ + public static $duplicateInputFieldMessage = 'There can be only one input field named "%s".'; + /** @var array */ public $knownNames; @@ -68,6 +71,6 @@ public function getASTVisitor(ASTValidationContext $context) public static function duplicateInputFieldMessage($fieldName) { - return sprintf('There can be only one input field named "%s".', $fieldName); + return sprintf(static::$duplicateInputFieldMessage, $fieldName); } } diff --git a/src/Validator/Rules/UniqueOperationNames.php b/src/Validator/Rules/UniqueOperationNames.php index c4a0777fb..277e2e62a 100644 --- a/src/Validator/Rules/UniqueOperationNames.php +++ b/src/Validator/Rules/UniqueOperationNames.php @@ -15,6 +15,9 @@ class UniqueOperationNames extends ValidationRule { + /** @var string */ + public static $duplicateOperationNameMessage = 'There can be only one operation named "%s".'; + /** @var NameNode[] */ public $knownOperationNames; @@ -47,6 +50,6 @@ public function getVisitor(ValidationContext $context) public static function duplicateOperationNameMessage($operationName) { - return sprintf('There can be only one operation named "%s".', $operationName); + return sprintf(static::$duplicateOperationNameMessage, $operationName); } } diff --git a/src/Validator/Rules/UniqueVariableNames.php b/src/Validator/Rules/UniqueVariableNames.php index f6db14e65..4a6450893 100644 --- a/src/Validator/Rules/UniqueVariableNames.php +++ b/src/Validator/Rules/UniqueVariableNames.php @@ -13,6 +13,9 @@ class UniqueVariableNames extends ValidationRule { + /** @var string */ + public static $duplicateVariableMessage = 'There can be only one variable named "%s".'; + /** @var NameNode[] */ public $knownVariableNames; @@ -40,6 +43,6 @@ public function getVisitor(ValidationContext $context) public static function duplicateVariableMessage($variableName) { - return sprintf('There can be only one variable named "%s".', $variableName); + return sprintf(static::$duplicateVariableMessage, $variableName); } } diff --git a/src/Validator/Rules/ValuesOfCorrectType.php b/src/Validator/Rules/ValuesOfCorrectType.php index 9c48c1253..3c3365fcc 100644 --- a/src/Validator/Rules/ValuesOfCorrectType.php +++ b/src/Validator/Rules/ValuesOfCorrectType.php @@ -46,6 +46,24 @@ */ class ValuesOfCorrectType extends ValidationRule { + /** @var string */ + public static $didYouMeanMessage = 'Did you mean '; + + /** @var string */ + public static $badValueMessage = 'Expected type %s, found %s'; + + /** @var string */ + public static $enumTypeSuggestionMessage = 'Did you mean the enum value '; + + /** @var string */ + public static $badArgumentValueMessage = 'Field "%s" argument "%s" requires type %s, found %s'; + + /** @var string */ + public static $requiredFieldMessage = 'Field %s.%s of required type %s was not provided.'; + + /** @var string */ + public static $unknownFieldMessage = 'Field "%s" is not defined by type %s'; + public function getVisitor(ValidationContext $context) { $fieldName = ''; @@ -130,7 +148,7 @@ static function ($field) : string { array_keys($parentType->getFields()) ); $didYouMean = $suggestions - ? 'Did you mean ' . Utils::orList($suggestions) . '?' + ? static::$didYouMeanMessage . Utils::orList($suggestions) . '?' : null; $context->reportError( @@ -176,7 +194,7 @@ static function ($field) : string { public static function badValueMessage($typeName, $valueName, $message = null) { - return sprintf('Expected type %s, found %s', $typeName, $valueName) . + return sprintf(static::$badValueMessage, $typeName, $valueName) . ($message ? "; ${message}" : '.'); } @@ -253,24 +271,24 @@ static function (EnumValueDefinition $value) : string { ) ); - return $suggestions ? 'Did you mean the enum value ' . Utils::orList($suggestions) . '?' : null; + return $suggestions ? static::$enumTypeSuggestionMessage . Utils::orList($suggestions) . '?' : null; } } public static function badArgumentValueMessage($typeName, $valueName, $fieldName, $argName, $message = null) { - return sprintf('Field "%s" argument "%s" requires type %s, found %s', $fieldName, $argName, $typeName, $valueName) . + return sprintf(static::$badArgumentValueMessage, $fieldName, $argName, $typeName, $valueName) . ($message ? sprintf('; %s', $message) : '.'); } public static function requiredFieldMessage($typeName, $fieldName, $fieldTypeName) { - return sprintf('Field %s.%s of required type %s was not provided.', $typeName, $fieldName, $fieldTypeName); + return sprintf(static::$requiredFieldMessage, $typeName, $fieldName, $fieldTypeName); } public static function unknownFieldMessage($typeName, $fieldName, $message = null) { - return sprintf('Field "%s" is not defined by type %s', $fieldName, $typeName) . + return sprintf(static::$unknownFieldMessage, $fieldName, $typeName) . ($message ? sprintf('; %s', $message) : '.'); } diff --git a/src/Validator/Rules/VariablesAreInputTypes.php b/src/Validator/Rules/VariablesAreInputTypes.php index a1daafff0..19fee1d9c 100644 --- a/src/Validator/Rules/VariablesAreInputTypes.php +++ b/src/Validator/Rules/VariablesAreInputTypes.php @@ -15,6 +15,9 @@ class VariablesAreInputTypes extends ValidationRule { + /** @var string */ + public static $nonInputTypeOnVarMessage = 'Variable "$%s" cannot be non-input type "%s".'; + public function getVisitor(ValidationContext $context) { return [ @@ -37,6 +40,6 @@ public function getVisitor(ValidationContext $context) public static function nonInputTypeOnVarMessage($variableName, $typeName) { - return sprintf('Variable "$%s" cannot be non-input type "%s".', $variableName, $typeName); + return sprintf(static::$nonInputTypeOnVarMessage, $variableName, $typeName); } } diff --git a/src/Validator/Rules/VariablesInAllowedPosition.php b/src/Validator/Rules/VariablesInAllowedPosition.php index 717bd6563..5df5bb3c4 100644 --- a/src/Validator/Rules/VariablesInAllowedPosition.php +++ b/src/Validator/Rules/VariablesInAllowedPosition.php @@ -21,6 +21,9 @@ class VariablesInAllowedPosition extends ValidationRule { + /** @var string */ + public static $badVarPosMessage = 'Variable "$%s" of type "%s" used in position expecting type "%s".'; + /** * A map from variable names to their definition nodes. * @@ -83,7 +86,7 @@ public function getVisitor(ValidationContext $context) public static function badVarPosMessage($varName, $varType, $expectedType) { return sprintf( - 'Variable "$%s" of type "%s" used in position expecting type "%s".', + static::$badVarPosMessage, $varName, $varType, $expectedType From 867e73c6b75321eff34b0c85a12c357692c34200 Mon Sep 17 00:00:00 2001 From: barchenkov Date: Thu, 6 Aug 2020 11:17:11 +0300 Subject: [PATCH 2/2] sprintf function for --- src/Validator/Rules/ValuesOfCorrectType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Validator/Rules/ValuesOfCorrectType.php b/src/Validator/Rules/ValuesOfCorrectType.php index 3c3365fcc..93ca13e16 100644 --- a/src/Validator/Rules/ValuesOfCorrectType.php +++ b/src/Validator/Rules/ValuesOfCorrectType.php @@ -53,7 +53,7 @@ class ValuesOfCorrectType extends ValidationRule public static $badValueMessage = 'Expected type %s, found %s'; /** @var string */ - public static $enumTypeSuggestionMessage = 'Did you mean the enum value '; + public static $enumTypeSuggestionMessage = 'Did you mean the enum value %s?'; /** @var string */ public static $badArgumentValueMessage = 'Field "%s" argument "%s" requires type %s, found %s'; @@ -271,7 +271,7 @@ static function (EnumValueDefinition $value) : string { ) ); - return $suggestions ? static::$enumTypeSuggestionMessage . Utils::orList($suggestions) . '?' : null; + return $suggestions ? sprintf(static::$enumTypeSuggestionMessage, Utils::orList($suggestions)) : null; } }