Skip to content

Commit

Permalink
Validation messages text in class public static properties (webonyx#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
barchenkov committed Aug 5, 2020
1 parent 5042bed commit 2cb68f0
Show file tree
Hide file tree
Showing 32 changed files with 205 additions and 52 deletions.
5 changes: 4 additions & 1 deletion src/Validator/Rules/DisableIntrospection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion src/Validator/Rules/ExecutableDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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);
}
}
15 changes: 12 additions & 3 deletions src/Validator/Rules/FieldsOnCorrectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/Validator/Rules/FragmentsOnCompositeTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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);
}
}
10 changes: 8 additions & 2 deletions src/Validator/Rules/KnownArgumentNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/Validator/Rules/KnownArgumentNamesOnDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/Validator/Rules/KnownDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -124,7 +130,7 @@ static function ($name) : string {

public static function unknownDirectiveMessage($directiveName)
{
return sprintf('Unknown directive "%s".', $directiveName);
return sprintf(static::$unknownDirectiveMessage, $directiveName);
}

/**
Expand Down Expand Up @@ -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);
}
}
5 changes: 4 additions & 1 deletion src/Validator/Rules/KnownFragmentNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

class KnownFragmentNames extends ValidationRule
{
/** @var string */
public static $unknownFragmentMessage = 'Unknown fragment "%s".';

public function getVisitor(ValidationContext $context)
{
return [
Expand All @@ -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);
}
}
10 changes: 8 additions & 2 deletions src/Validator/Rules/KnownTypeNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/Validator/Rules/LoneAnonymousOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
10 changes: 8 additions & 2 deletions src/Validator/Rules/LoneSchemaDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/Validator/Rules/NoFragmentCycles.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

class NoFragmentCycles extends ValidationRule
{
/** @var string */
public static $cycleErrorMessage = 'Cannot spread fragment "%s" within itself%s.';

/** @var bool[] */
public $visitedFrags;

Expand Down Expand Up @@ -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) : ''
);
Expand Down
10 changes: 8 additions & 2 deletions src/Validator/Rules/NoUndefinedVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
}
}
5 changes: 4 additions & 1 deletion src/Validator/Rules/NoUnusedFragments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

class NoUnusedFragments extends ValidationRule
{
/** @var string */
public static $unusedFragMessage = 'Fragment "%s" is never used.';

/** @var OperationDefinitionNode[] */
public $operationDefs;

Expand Down Expand Up @@ -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);
}
}
10 changes: 8 additions & 2 deletions src/Validator/Rules/NoUnusedVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
10 changes: 8 additions & 2 deletions src/Validator/Rules/OverlappingFieldsCanBeMerged.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand Down
Loading

0 comments on commit 2cb68f0

Please sign in to comment.