PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / lib / packages / GraphQL / Validator / Rules / UniqueVariableNames.php
UniqueVariableNames.php
43 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules;
4
5 use Automattic\WooCommerce\Vendor\GraphQL\Error\Error;
6 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NameNode;
7 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind;
8 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\VariableDefinitionNode;
9 use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext;
10
11 class UniqueVariableNames extends ValidationRule
12 {
13 /** @var array<string, NameNode> */
14 protected array $knownVariableNames;
15
16 public function getVisitor(QueryValidationContext $context): array
17 {
18 $this->knownVariableNames = [];
19
20 return [
21 NodeKind::OPERATION_DEFINITION => function (): void {
22 $this->knownVariableNames = [];
23 },
24 NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use ($context): void {
25 $variableName = $node->variable->name->value;
26 if (! isset($this->knownVariableNames[$variableName])) {
27 $this->knownVariableNames[$variableName] = $node->variable->name;
28 } else {
29 $context->reportError(new Error(
30 static::duplicateVariableMessage($variableName),
31 [$this->knownVariableNames[$variableName], $node->variable->name]
32 ));
33 }
34 },
35 ];
36 }
37
38 public static function duplicateVariableMessage(string $variableName): string
39 {
40 return "There can be only one variable named \"{$variableName}\".";
41 }
42 }
43