Fix CS in src/Language

This commit is contained in:
Simon Podlipsky 2018-08-13 10:36:03 +02:00
parent cc39b3ecbf
commit ad8693cb8a
No known key found for this signature in database
GPG Key ID: 725C2BD962B42663
74 changed files with 2232 additions and 1943 deletions

View File

@ -10,6 +10,7 @@
], ],
"require": { "require": {
"php": "^7.1", "php": "^7.1",
"ext-json": "*",
"ext-mbstring": "*" "ext-mbstring": "*"
}, },
"require-dev": { "require-dev": {

View File

@ -90,4 +90,11 @@
/> />
</properties> </properties>
</rule> </rule>
<!-- IDEs sort by PSR12, Slevomat coding standard uses old sorting for BC -->
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
<properties>
<property name="psr12Compatible" type="bool" value="true" />
</properties>
</rule>
</ruleset> </ruleset>

View File

@ -1,17 +1,19 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ArgumentNode extends Node class ArgumentNode extends Node
{ {
/** @var string */
public $kind = NodeKind::ARGUMENT; public $kind = NodeKind::ARGUMENT;
/** /** @var ValueNode */
* @var ValueNode
*/
public $value; public $value;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
} }

View File

@ -1,13 +1,15 @@
<?php <?php
namespace GraphQL\Language\AST;
declare(strict_types=1);
namespace GraphQL\Language\AST;
class BooleanValueNode extends Node implements ValueNode class BooleanValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::BOOLEAN; public $kind = NodeKind::BOOLEAN;
/** /** @var string */
* @var string
*/
public $value; public $value;
} }

View File

@ -1,11 +1,14 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
interface DefinitionNode
{
/** /**
* export type DefinitionNode = * export type DefinitionNode =
* | ExecutableDefinitionNode * | ExecutableDefinitionNode
* | TypeSystemDefinitionNode; // experimental non-spec addition. * | TypeSystemDefinitionNode; // experimental non-spec addition.
*/ */
interface DefinitionNode
{
} }

View File

@ -1,30 +1,24 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class DirectiveDefinitionNode extends Node implements TypeSystemDefinitionNode class DirectiveDefinitionNode extends Node implements TypeSystemDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::DIRECTIVE_DEFINITION; public $kind = NodeKind::DIRECTIVE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var ArgumentNode[] */
* @var ArgumentNode[]
*/
public $arguments; public $arguments;
/** /** @var NameNode[] */
* @var NameNode[]
*/
public $locations; public $locations;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,17 +1,18 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class DirectiveNode extends Node class DirectiveNode extends Node
{ {
/** @var string */
public $kind = NodeKind::DIRECTIVE; public $kind = NodeKind::DIRECTIVE;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var ArgumentNode[] */
* @var ArgumentNode[]
*/
public $arguments; public $arguments;
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class DocumentNode extends Node class DocumentNode extends Node
{ {
/** @var string */
public $kind = NodeKind::DOCUMENT; public $kind = NodeKind::DOCUMENT;
/** /** @var DefinitionNode[] */
* @var DefinitionNode[]
*/
public $definitions; public $definitions;
} }

View File

@ -1,30 +1,25 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class EnumTypeDefinitionNode extends Node implements TypeDefinitionNode class EnumTypeDefinitionNode extends Node implements TypeDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::ENUM_TYPE_DEFINITION; public $kind = NodeKind::ENUM_TYPE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var EnumValueDefinitionNode[]|null|NodeList */
* @var EnumValueDefinitionNode[]|null|NodeList
*/
public $values; public $values;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,25 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class EnumTypeExtensionNode extends Node implements TypeExtensionNode class EnumTypeExtensionNode extends Node implements TypeExtensionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::ENUM_TYPE_EXTENSION; public $kind = NodeKind::ENUM_TYPE_EXTENSION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var EnumValueDefinitionNode[]|null */
* @var EnumValueDefinitionNode[]|null
*/
public $values; public $values;
} }

View File

@ -1,25 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class EnumValueDefinitionNode extends Node class EnumValueDefinitionNode extends Node
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::ENUM_VALUE_DEFINITION; public $kind = NodeKind::ENUM_VALUE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class EnumValueNode extends Node implements ValueNode class EnumValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::ENUM; public $kind = NodeKind::ENUM;
/** /** @var string */
* @var string
*/
public $value; public $value;
} }

View File

@ -1,11 +1,14 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
interface ExecutableDefinitionNode extends DefinitionNode
{
/** /**
* export type ExecutableDefinitionNode = * export type ExecutableDefinitionNode =
* | OperationDefinitionNode * | OperationDefinitionNode
* | FragmentDefinitionNode; * | FragmentDefinitionNode;
*/ */
interface ExecutableDefinitionNode extends DefinitionNode
{
} }

View File

@ -1,35 +1,27 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class FieldDefinitionNode extends Node class FieldDefinitionNode extends Node
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::FIELD_DEFINITION; public $kind = NodeKind::FIELD_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var InputValueDefinitionNode[]|NodeList */
* @var InputValueDefinitionNode[]|NodeList
*/
public $arguments; public $arguments;
/** /** @var TypeNode */
* @var TypeNode
*/
public $type; public $type;
/** /** @var DirectiveNode[]|NodeList */
* @var DirectiveNode[]|NodeList
*/
public $directives; public $directives;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -6,35 +6,22 @@ namespace GraphQL\Language\AST;
class FieldNode extends Node implements SelectionNode class FieldNode extends Node implements SelectionNode
{ {
/** @var string */
public $kind = NodeKind::FIELD; public $kind = NodeKind::FIELD;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var NameNode|null */
* @var NameNode|null
*/
public $alias; public $alias;
/** /** @var ArgumentNode[]|null */
* @var ArgumentNode[]|null
*/
public $arguments; public $arguments;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var SelectionSetNode|null */
* @var SelectionSetNode|null
*/
public $selectionSet; public $selectionSet;
public function getKind() : string
{
return NodeKind::FIELD;
}
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class FloatValueNode extends Node implements ValueNode class FloatValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::FLOAT; public $kind = NodeKind::FLOAT;
/** /** @var string */
* @var string
*/
public $value; public $value;
} }

View File

@ -1,13 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class FragmentDefinitionNode extends Node implements ExecutableDefinitionNode, HasSelectionSet class FragmentDefinitionNode extends Node implements ExecutableDefinitionNode, HasSelectionSet
{ {
/** @var string */
public $kind = NodeKind::FRAGMENT_DEFINITION; public $kind = NodeKind::FRAGMENT_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /**
@ -18,18 +20,14 @@ class FragmentDefinitionNode extends Node implements ExecutableDefinitionNode, H
*/ */
public $variableDefinitions; public $variableDefinitions;
/** /** @var NamedTypeNode */
* @var NamedTypeNode
*/
public $typeCondition; public $typeCondition;
/** /** @var DirectiveNode[]|NodeList */
* @var DirectiveNode[]|NodeList
*/
public $directives; public $directives;
/** /** @var SelectionSetNode */
* @var SelectionSetNode
*/
public $selectionSet; public $selectionSet;
} }

View File

@ -1,23 +1,18 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class FragmentSpreadNode extends Node implements SelectionNode class FragmentSpreadNode extends Node implements SelectionNode
{ {
/** @var string */
public $kind = NodeKind::FRAGMENT_SPREAD; public $kind = NodeKind::FRAGMENT_SPREAD;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
public function getKind() : string
{
return NodeKind::FRAGMENT_SPREAD;
}
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
interface HasSelectionSet interface HasSelectionSet

View File

@ -1,28 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class InlineFragmentNode extends Node implements SelectionNode class InlineFragmentNode extends Node implements SelectionNode
{ {
/** @var string */
public $kind = NodeKind::INLINE_FRAGMENT; public $kind = NodeKind::INLINE_FRAGMENT;
/** /** @var NamedTypeNode */
* @var NamedTypeNode
*/
public $typeCondition; public $typeCondition;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var SelectionSetNode */
* @var SelectionSetNode
*/
public $selectionSet; public $selectionSet;
public function getKind() : string
{
return NodeKind::INLINE_FRAGMENT;
}
} }

View File

@ -1,30 +1,24 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class InputObjectTypeDefinitionNode extends Node implements TypeDefinitionNode class InputObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::INPUT_OBJECT_TYPE_DEFINITION; public $kind = NodeKind::INPUT_OBJECT_TYPE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var InputValueDefinitionNode[]|null */
* @var InputValueDefinitionNode[]|null
*/
public $fields; public $fields;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,25 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class InputObjectTypeExtensionNode extends Node implements TypeExtensionNode class InputObjectTypeExtensionNode extends Node implements TypeExtensionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::INPUT_OBJECT_TYPE_EXTENSION; public $kind = NodeKind::INPUT_OBJECT_TYPE_EXTENSION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var InputValueDefinitionNode[]|null */
* @var InputValueDefinitionNode[]|null
*/
public $fields; public $fields;
} }

View File

@ -1,35 +1,27 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class InputValueDefinitionNode extends Node class InputValueDefinitionNode extends Node
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::INPUT_VALUE_DEFINITION; public $kind = NodeKind::INPUT_VALUE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var TypeNode */
* @var TypeNode
*/
public $type; public $type;
/** /** @var ValueNode */
* @var ValueNode
*/
public $defaultValue; public $defaultValue;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class IntValueNode extends Node implements ValueNode class IntValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::INT; public $kind = NodeKind::INT;
/** /** @var string */
* @var string
*/
public $value; public $value;
} }

View File

@ -1,30 +1,24 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class InterfaceTypeDefinitionNode extends Node implements TypeDefinitionNode class InterfaceTypeDefinitionNode extends Node implements TypeDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::INTERFACE_TYPE_DEFINITION; public $kind = NodeKind::INTERFACE_TYPE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var FieldDefinitionNode[]|null */
* @var FieldDefinitionNode[]|null
*/
public $fields; public $fields;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,25 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class InterfaceTypeExtensionNode extends Node implements TypeExtensionNode class InterfaceTypeExtensionNode extends Node implements TypeExtensionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::INTERFACE_TYPE_EXTENSION; public $kind = NodeKind::INTERFACE_TYPE_EXTENSION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var FieldDefinitionNode[]|null */
* @var FieldDefinitionNode[]|null
*/
public $fields; public $fields;
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ListTypeNode extends Node implements TypeNode class ListTypeNode extends Node implements TypeNode
{ {
/** @var string */
public $kind = NodeKind::LIST_TYPE; public $kind = NodeKind::LIST_TYPE;
/** /** @var Node */
* @var Node
*/
public $type; public $type;
} }

View File

@ -1,13 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ListValueNode extends Node implements ValueNode class ListValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::LST; public $kind = NodeKind::LST;
/** /** @var ValueNode[]|NodeList */
* @var ValueNode[]|NodeList
*/
public $values; public $values;
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
use GraphQL\Language\Source; use GraphQL\Language\Source;
@ -46,8 +49,8 @@ class Location
public $source; public $source;
/** /**
* @param $start * @param int $start
* @param $end * @param int $end
* @return static * @return static
*/ */
public static function create($start, $end) public static function create($start, $end)
@ -58,15 +61,17 @@ class Location
return $tmp; return $tmp;
} }
public function __construct(Token $startToken = null, Token $endToken = null, Source $source = null) public function __construct(?Token $startToken = null, ?Token $endToken = null, ?Source $source = null)
{ {
$this->startToken = $startToken; $this->startToken = $startToken;
$this->endToken = $endToken; $this->endToken = $endToken;
$this->source = $source; $this->source = $source;
if ($startToken && $endToken) { if (! $startToken || ! $endToken) {
return;
}
$this->start = $startToken->start; $this->start = $startToken->start;
$this->end = $endToken->end; $this->end = $endToken->end;
} }
} }
}

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class NameNode extends Node implements TypeNode class NameNode extends Node implements TypeNode
{ {
/** @var string */
public $kind = NodeKind::NAME; public $kind = NodeKind::NAME;
/** /** @var string */
* @var string
*/
public $value; public $value;
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class NamedTypeNode extends Node implements TypeNode class NamedTypeNode extends Node implements TypeNode
{ {
/** @var string */
public $kind = NodeKind::NAMED_TYPE; public $kind = NodeKind::NAMED_TYPE;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
} }

View File

@ -1,50 +1,54 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use function get_object_vars;
use function is_array;
use function is_scalar;
use function json_encode;
/**
* type Node = NameNode
* | DocumentNode
* | OperationDefinitionNode
* | VariableDefinitionNode
* | VariableNode
* | SelectionSetNode
* | FieldNode
* | ArgumentNode
* | FragmentSpreadNode
* | InlineFragmentNode
* | FragmentDefinitionNode
* | IntValueNode
* | FloatValueNode
* | StringValueNode
* | BooleanValueNode
* | EnumValueNode
* | ListValueNode
* | ObjectValueNode
* | ObjectFieldNode
* | DirectiveNode
* | ListTypeNode
* | NonNullTypeNode
*/
abstract class Node abstract class Node
{ {
/** /** @var Location */
type Node = NameNode
| DocumentNode
| OperationDefinitionNode
| VariableDefinitionNode
| VariableNode
| SelectionSetNode
| FieldNode
| ArgumentNode
| FragmentSpreadNode
| InlineFragmentNode
| FragmentDefinitionNode
| IntValueNode
| FloatValueNode
| StringValueNode
| BooleanValueNode
| EnumValueNode
| ListValueNode
| ObjectValueNode
| ObjectFieldNode
| DirectiveNode
| ListTypeNode
| NonNullTypeNode
*/
public $kind;
/**
* @var Location
*/
public $loc; public $loc;
/** /**
* @param array $vars * @param (string|NameNode|NodeList|SelectionSetNode|Location|null)[] $vars
*/ */
public function __construct(array $vars) public function __construct(array $vars)
{ {
if (!empty($vars)) { if (empty($vars)) {
Utils::assign($this, $vars); return;
} }
Utils::assign($this, $vars);
} }
/** /**
@ -56,8 +60,8 @@ abstract class Node
} }
/** /**
* @param $value * @param string|NodeList|Location|Node|(Node|NodeList|Location)[] $value
* @return array|Node * @return string|NodeList|Location|Node
*/ */
private function cloneValue($value) private function cloneValue($value)
{ {
@ -66,7 +70,7 @@ abstract class Node
foreach ($value as $key => $arrValue) { foreach ($value as $key => $arrValue) {
$cloned[$key] = $this->cloneValue($arrValue); $cloned[$key] = $this->cloneValue($arrValue);
} }
} else if ($value instanceof Node) { } elseif ($value instanceof self) {
$cloned = clone $value; $cloned = clone $value;
foreach (get_object_vars($cloned) as $prop => $propValue) { foreach (get_object_vars($cloned) as $prop => $propValue) {
$cloned->{$prop} = $this->cloneValue($propValue); $cloned->{$prop} = $this->cloneValue($propValue);
@ -84,34 +88,34 @@ abstract class Node
public function __toString() public function __toString()
{ {
$tmp = $this->toArray(true); $tmp = $this->toArray(true);
return (string) json_encode($tmp); return (string) json_encode($tmp);
} }
/** /**
* @param bool $recursive * @param bool $recursive
* @return array * @return mixed[]
*/ */
public function toArray($recursive = false) public function toArray($recursive = false)
{ {
if ($recursive) { if ($recursive) {
return $this->recursiveToArray($this); return $this->recursiveToArray($this);
} else { }
$tmp = (array) $this; $tmp = (array) $this;
if ($this->loc) { if ($this->loc) {
$tmp['loc'] = [ $tmp['loc'] = [
'start' => $this->loc->start, 'start' => $this->loc->start,
'end' => $this->loc->end 'end' => $this->loc->end,
]; ];
} }
return $tmp; return $tmp;
} }
}
/** /**
* @param Node $node * @return mixed[]
* @return array
*/ */
private function recursiveToArray(Node $node) private function recursiveToArray(Node $node)
{ {
@ -122,16 +126,18 @@ abstract class Node
if ($node->loc) { if ($node->loc) {
$result['loc'] = [ $result['loc'] = [
'start' => $node->loc->start, 'start' => $node->loc->start,
'end' => $node->loc->end 'end' => $node->loc->end,
]; ];
} }
foreach (get_object_vars($node) as $prop => $propValue) { foreach (get_object_vars($node) as $prop => $propValue) {
if (isset($result[$prop])) if (isset($result[$prop])) {
continue; continue;
}
if ($propValue === null) if ($propValue === null) {
continue; continue;
}
if (is_array($propValue) || $propValue instanceof NodeList) { if (is_array($propValue) || $propValue instanceof NodeList) {
$tmp = []; $tmp = [];
@ -140,7 +146,7 @@ abstract class Node
} }
} elseif ($propValue instanceof Node) { } elseif ($propValue instanceof Node) {
$tmp = $this->recursiveToArray($propValue); $tmp = $this->recursiveToArray($propValue);
} else if (is_scalar($propValue) || null === $propValue) { } elseif (is_scalar($propValue) || $propValue === null) {
$tmp = $propValue; $tmp = $propValue;
} else { } else {
$tmp = null; $tmp = null;
@ -148,6 +154,7 @@ abstract class Node
$result[$prop] = $tmp; $result[$prop] = $tmp;
} }
return $result; return $result;
} }
} }

View File

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class NodeKind class NodeKind
@ -7,7 +9,6 @@ class NodeKind
// constants from language/kinds.js: // constants from language/kinds.js:
const NAME = 'Name'; const NAME = 'Name';
// Document // Document
const DOCUMENT = 'Document'; const DOCUMENT = 'Document';
@ -17,13 +18,11 @@ class NodeKind
const SELECTION_SET = 'SelectionSet'; const SELECTION_SET = 'SelectionSet';
const FIELD = 'Field'; const FIELD = 'Field';
const ARGUMENT = 'Argument'; const ARGUMENT = 'Argument';
// Fragments // Fragments
const FRAGMENT_SPREAD = 'FragmentSpread'; const FRAGMENT_SPREAD = 'FragmentSpread';
const INLINE_FRAGMENT = 'InlineFragment'; const INLINE_FRAGMENT = 'InlineFragment';
const FRAGMENT_DEFINITION = 'FragmentDefinition'; const FRAGMENT_DEFINITION = 'FragmentDefinition';
// Values // Values
const INT = 'IntValue'; const INT = 'IntValue';
@ -35,22 +34,18 @@ class NodeKind
const LST = 'ListValue'; const LST = 'ListValue';
const OBJECT = 'ObjectValue'; const OBJECT = 'ObjectValue';
const OBJECT_FIELD = 'ObjectField'; const OBJECT_FIELD = 'ObjectField';
// Directives // Directives
const DIRECTIVE = 'Directive'; const DIRECTIVE = 'Directive';
// Types // Types
const NAMED_TYPE = 'NamedType'; const NAMED_TYPE = 'NamedType';
const LIST_TYPE = 'ListType'; const LIST_TYPE = 'ListType';
const NON_NULL_TYPE = 'NonNullType'; const NON_NULL_TYPE = 'NonNullType';
// Type System Definitions // Type System Definitions
const SCHEMA_DEFINITION = 'SchemaDefinition'; const SCHEMA_DEFINITION = 'SchemaDefinition';
const OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition'; const OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition';
// Type Definitions // Type Definitions
const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition'; const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
@ -62,7 +57,6 @@ class NodeKind
const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition'; const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
const ENUM_VALUE_DEFINITION = 'EnumValueDefinition'; const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition'; const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition';
// Type Extensions // Type Extensions
const SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension'; const SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension';
@ -71,75 +65,71 @@ class NodeKind
const UNION_TYPE_EXTENSION = 'UnionTypeExtension'; const UNION_TYPE_EXTENSION = 'UnionTypeExtension';
const ENUM_TYPE_EXTENSION = 'EnumTypeExtension'; const ENUM_TYPE_EXTENSION = 'EnumTypeExtension';
const INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension'; const INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension';
// Directive Definitions // Directive Definitions
const DIRECTIVE_DEFINITION = 'DirectiveDefinition'; const DIRECTIVE_DEFINITION = 'DirectiveDefinition';
/** /** @var string[] */
* @todo conver to const array when moving to PHP5.6
* @var array
*/
public static $classMap = [ public static $classMap = [
NodeKind::NAME => NameNode::class, self::NAME => NameNode::class,
// Document // Document
NodeKind::DOCUMENT => DocumentNode::class, self::DOCUMENT => DocumentNode::class,
NodeKind::OPERATION_DEFINITION => OperationDefinitionNode::class, self::OPERATION_DEFINITION => OperationDefinitionNode::class,
NodeKind::VARIABLE_DEFINITION => VariableDefinitionNode::class, self::VARIABLE_DEFINITION => VariableDefinitionNode::class,
NodeKind::VARIABLE => VariableNode::class, self::VARIABLE => VariableNode::class,
NodeKind::SELECTION_SET => SelectionSetNode::class, self::SELECTION_SET => SelectionSetNode::class,
NodeKind::FIELD => FieldNode::class, self::FIELD => FieldNode::class,
NodeKind::ARGUMENT => ArgumentNode::class, self::ARGUMENT => ArgumentNode::class,
// Fragments // Fragments
NodeKind::FRAGMENT_SPREAD => FragmentSpreadNode::class, self::FRAGMENT_SPREAD => FragmentSpreadNode::class,
NodeKind::INLINE_FRAGMENT => InlineFragmentNode::class, self::INLINE_FRAGMENT => InlineFragmentNode::class,
NodeKind::FRAGMENT_DEFINITION => FragmentDefinitionNode::class, self::FRAGMENT_DEFINITION => FragmentDefinitionNode::class,
// Values // Values
NodeKind::INT => IntValueNode::class, self::INT => IntValueNode::class,
NodeKind::FLOAT => FloatValueNode::class, self::FLOAT => FloatValueNode::class,
NodeKind::STRING => StringValueNode::class, self::STRING => StringValueNode::class,
NodeKind::BOOLEAN => BooleanValueNode::class, self::BOOLEAN => BooleanValueNode::class,
NodeKind::ENUM => EnumValueNode::class, self::ENUM => EnumValueNode::class,
NodeKind::NULL => NullValueNode::class, self::NULL => NullValueNode::class,
NodeKind::LST => ListValueNode::class, self::LST => ListValueNode::class,
NodeKind::OBJECT => ObjectValueNode::class, self::OBJECT => ObjectValueNode::class,
NodeKind::OBJECT_FIELD => ObjectFieldNode::class, self::OBJECT_FIELD => ObjectFieldNode::class,
// Directives // Directives
NodeKind::DIRECTIVE => DirectiveNode::class, self::DIRECTIVE => DirectiveNode::class,
// Types // Types
NodeKind::NAMED_TYPE => NamedTypeNode::class, self::NAMED_TYPE => NamedTypeNode::class,
NodeKind::LIST_TYPE => ListTypeNode::class, self::LIST_TYPE => ListTypeNode::class,
NodeKind::NON_NULL_TYPE => NonNullTypeNode::class, self::NON_NULL_TYPE => NonNullTypeNode::class,
// Type System Definitions // Type System Definitions
NodeKind::SCHEMA_DEFINITION => SchemaDefinitionNode::class, self::SCHEMA_DEFINITION => SchemaDefinitionNode::class,
NodeKind::OPERATION_TYPE_DEFINITION => OperationTypeDefinitionNode::class, self::OPERATION_TYPE_DEFINITION => OperationTypeDefinitionNode::class,
// Type Definitions // Type Definitions
NodeKind::SCALAR_TYPE_DEFINITION => ScalarTypeDefinitionNode::class, self::SCALAR_TYPE_DEFINITION => ScalarTypeDefinitionNode::class,
NodeKind::OBJECT_TYPE_DEFINITION => ObjectTypeDefinitionNode::class, self::OBJECT_TYPE_DEFINITION => ObjectTypeDefinitionNode::class,
NodeKind::FIELD_DEFINITION => FieldDefinitionNode::class, self::FIELD_DEFINITION => FieldDefinitionNode::class,
NodeKind::INPUT_VALUE_DEFINITION => InputValueDefinitionNode::class, self::INPUT_VALUE_DEFINITION => InputValueDefinitionNode::class,
NodeKind::INTERFACE_TYPE_DEFINITION => InterfaceTypeDefinitionNode::class, self::INTERFACE_TYPE_DEFINITION => InterfaceTypeDefinitionNode::class,
NodeKind::UNION_TYPE_DEFINITION => UnionTypeDefinitionNode::class, self::UNION_TYPE_DEFINITION => UnionTypeDefinitionNode::class,
NodeKind::ENUM_TYPE_DEFINITION => EnumTypeDefinitionNode::class, self::ENUM_TYPE_DEFINITION => EnumTypeDefinitionNode::class,
NodeKind::ENUM_VALUE_DEFINITION => EnumValueDefinitionNode::class, self::ENUM_VALUE_DEFINITION => EnumValueDefinitionNode::class,
NodeKind::INPUT_OBJECT_TYPE_DEFINITION =>InputObjectTypeDefinitionNode::class, self::INPUT_OBJECT_TYPE_DEFINITION => InputObjectTypeDefinitionNode::class,
// Type Extensions // Type Extensions
NodeKind::SCALAR_TYPE_EXTENSION => ScalarTypeExtensionNode::class, self::SCALAR_TYPE_EXTENSION => ScalarTypeExtensionNode::class,
NodeKind::OBJECT_TYPE_EXTENSION => ObjectTypeExtensionNode::class, self::OBJECT_TYPE_EXTENSION => ObjectTypeExtensionNode::class,
NodeKind::INTERFACE_TYPE_EXTENSION => InterfaceTypeExtensionNode::class, self::INTERFACE_TYPE_EXTENSION => InterfaceTypeExtensionNode::class,
NodeKind::UNION_TYPE_EXTENSION => UnionTypeExtensionNode::class, self::UNION_TYPE_EXTENSION => UnionTypeExtensionNode::class,
NodeKind::ENUM_TYPE_EXTENSION => EnumTypeExtensionNode::class, self::ENUM_TYPE_EXTENSION => EnumTypeExtensionNode::class,
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => InputObjectTypeExtensionNode::class, self::INPUT_OBJECT_TYPE_EXTENSION => InputObjectTypeExtensionNode::class,
// Directive Definitions // Directive Definitions
NodeKind::DIRECTIVE_DEFINITION => DirectiveDefinitionNode::class self::DIRECTIVE_DEFINITION => DirectiveDefinitionNode::class,
]; ];
} }

View File

@ -1,22 +1,22 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
use GraphQL\Utils\AST; use GraphQL\Utils\AST;
use function array_merge;
use function array_splice;
use function count;
use function is_array;
/**
* Class NodeList
*
* @package GraphQL\Utils
*/
class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
{ {
/** /** @var Node[]|mixed[] */
* @var array
*/
private $nodes; private $nodes;
/** /**
* @param array $nodes * @param Node[]|mixed[] $nodes
* @return static * @return static
*/ */
public static function create(array $nodes) public static function create(array $nodes)
@ -25,8 +25,8 @@ class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
} }
/** /**
* NodeList constructor. *
* @param array $nodes * @param Node[]|mixed[] $nodes
*/ */
public function __construct(array $nodes) public function __construct(array $nodes)
{ {
@ -89,12 +89,12 @@ class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
} }
/** /**
* @param $list * @param NodeList|Node[] $list
* @return NodeList * @return NodeList
*/ */
public function merge($list) public function merge($list)
{ {
if ($list instanceof NodeList) { if ($list instanceof self) {
$list = $list->nodes; $list = $list->nodes;
} }
return new NodeList(array_merge($this->nodes, $list)); return new NodeList(array_merge($this->nodes, $list));

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class NonNullTypeNode extends Node implements TypeNode class NonNullTypeNode extends Node implements TypeNode
{ {
/** @var string */
public $kind = NodeKind::NON_NULL_TYPE; public $kind = NodeKind::NON_NULL_TYPE;
/** /** @var NameNode | ListTypeNode */
* @var NameNode | ListTypeNode
*/
public $type; public $type;
} }

View File

@ -1,7 +1,12 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class NullValueNode extends Node implements ValueNode class NullValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::NULL; public $kind = NodeKind::NULL;
} }

View File

@ -1,18 +1,18 @@
<?php <?php
namespace GraphQL\Language\AST;
declare(strict_types=1);
namespace GraphQL\Language\AST;
class ObjectFieldNode extends Node class ObjectFieldNode extends Node
{ {
/** @var string */
public $kind = NodeKind::OBJECT_FIELD; public $kind = NodeKind::OBJECT_FIELD;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var ValueNode */
* @var ValueNode
*/
public $value; public $value;
} }

View File

@ -1,35 +1,28 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ObjectTypeDefinitionNode extends Node implements TypeDefinitionNode class ObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::OBJECT_TYPE_DEFINITION; public $kind = NodeKind::OBJECT_TYPE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var NamedTypeNode[] */
* @var NamedTypeNode[]
*/
public $interfaces = []; public $interfaces = [];
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var FieldDefinitionNode[]|null */
* @var FieldDefinitionNode[]|null
*/
public $fields; public $fields;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,30 +1,24 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ObjectTypeExtensionNode extends Node implements TypeExtensionNode class ObjectTypeExtensionNode extends Node implements TypeExtensionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::OBJECT_TYPE_EXTENSION; public $kind = NodeKind::OBJECT_TYPE_EXTENSION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var NamedTypeNode[] */
* @var NamedTypeNode[]
*/
public $interfaces = []; public $interfaces = [];
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var FieldDefinitionNode[] */
* @var FieldDefinitionNode[]
*/
public $fields; public $fields;
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ObjectValueNode extends Node implements ValueNode class ObjectValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::OBJECT; public $kind = NodeKind::OBJECT;
/** /** @var ObjectFieldNode[]|NodeList */
* @var ObjectFieldNode[]|NodeList
*/
public $fields; public $fields;
} }

View File

@ -1,35 +1,27 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class OperationDefinitionNode extends Node implements ExecutableDefinitionNode, HasSelectionSet class OperationDefinitionNode extends Node implements ExecutableDefinitionNode, HasSelectionSet
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::OPERATION_DEFINITION; public $kind = NodeKind::OPERATION_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var string (oneOf 'query', 'mutation')) */
* @var string (oneOf 'query', 'mutation'))
*/
public $operation; public $operation;
/** /** @var VariableDefinitionNode[] */
* @var VariableDefinitionNode[]
*/
public $variableDefinitions; public $variableDefinitions;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var SelectionSetNode */
* @var SelectionSetNode
*/
public $selectionSet; public $selectionSet;
} }

View File

@ -1,11 +1,12 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class OperationTypeDefinitionNode extends Node class OperationTypeDefinitionNode extends Node
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::OPERATION_TYPE_DEFINITION; public $kind = NodeKind::OPERATION_TYPE_DEFINITION;
/** /**
@ -15,8 +16,7 @@ class OperationTypeDefinitionNode extends Node
*/ */
public $operation; public $operation;
/** /** @var NamedTypeNode */
* @var NamedTypeNode
*/
public $type; public $type;
} }

View File

@ -1,25 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ScalarTypeDefinitionNode extends Node implements TypeDefinitionNode class ScalarTypeDefinitionNode extends Node implements TypeDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::SCALAR_TYPE_DEFINITION; public $kind = NodeKind::SCALAR_TYPE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,20 +1,18 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class ScalarTypeExtensionNode extends Node implements TypeExtensionNode class ScalarTypeExtensionNode extends Node implements TypeExtensionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::SCALAR_TYPE_EXTENSION; public $kind = NodeKind::SCALAR_TYPE_EXTENSION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
} }

View File

@ -1,20 +1,18 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class SchemaDefinitionNode extends Node implements TypeSystemDefinitionNode class SchemaDefinitionNode extends Node implements TypeSystemDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::SCHEMA_DEFINITION; public $kind = NodeKind::SCHEMA_DEFINITION;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var OperationTypeDefinitionNode[] */
* @var OperationTypeDefinitionNode[]
*/
public $operationTypes; public $operationTypes;
} }

View File

@ -4,10 +4,9 @@ declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
interface SelectionNode
{
/** /**
* export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode * export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode
*/ */
public function getKind() : string; interface SelectionNode
{
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class SelectionSetNode extends Node class SelectionSetNode extends Node
{ {
/** @var string */
public $kind = NodeKind::SELECTION_SET; public $kind = NodeKind::SELECTION_SET;
/** /** @var SelectionNode[] */
* @var SelectionNode[]
*/
public $selections; public $selections;
} }

View File

@ -1,17 +1,18 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class StringValueNode extends Node implements ValueNode class StringValueNode extends Node implements ValueNode
{ {
/** @var string */
public $kind = NodeKind::STRING; public $kind = NodeKind::STRING;
/** /** @var string */
* @var string
*/
public $value; public $value;
/** /** @var bool|null */
* @var boolean|null
*/
public $block; public $block;
} }

View File

@ -1,14 +1,17 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
/**
* export type TypeDefinitionNode = ScalarTypeDefinitionNode
* | ObjectTypeDefinitionNode
* | InterfaceTypeDefinitionNode
* | UnionTypeDefinitionNode
* | EnumTypeDefinitionNode
* | InputObjectTypeDefinitionNode
*/
interface TypeDefinitionNode extends TypeSystemDefinitionNode interface TypeDefinitionNode extends TypeSystemDefinitionNode
{ {
/**
export type TypeDefinitionNode = ScalarTypeDefinitionNode
| ObjectTypeDefinitionNode
| InterfaceTypeDefinitionNode
| UnionTypeDefinitionNode
| EnumTypeDefinitionNode
| InputObjectTypeDefinitionNode
*/
} }

View File

@ -1,15 +1,18 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
/**
* export type TypeExtensionNode =
* | ScalarTypeExtensionNode
* | ObjectTypeExtensionNode
* | InterfaceTypeExtensionNode
* | UnionTypeExtensionNode
* | EnumTypeExtensionNode
* | InputObjectTypeExtensionNode;
*/
interface TypeExtensionNode extends TypeSystemDefinitionNode interface TypeExtensionNode extends TypeSystemDefinitionNode
{ {
/**
export type TypeExtensionNode =
| ScalarTypeExtensionNode
| ObjectTypeExtensionNode
| InterfaceTypeExtensionNode
| UnionTypeExtensionNode
| EnumTypeExtensionNode
| InputObjectTypeExtensionNode;
*/
} }

View File

@ -1,13 +1,14 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
/**
* export type TypeNode = NamedTypeNode
* | ListTypeNode
* | NonNullTypeNode
*/
interface TypeNode interface TypeNode
{ {
/**
export type TypeNode = NamedTypeNode
| ListTypeNode
| NonNullTypeNode
*/
} }

View File

@ -1,13 +1,16 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
/**
* export type TypeSystemDefinitionNode =
* | SchemaDefinitionNode
* | TypeDefinitionNode
* | TypeExtensionNode
* | DirectiveDefinitionNode
*/
interface TypeSystemDefinitionNode extends DefinitionNode interface TypeSystemDefinitionNode extends DefinitionNode
{ {
/**
export type TypeSystemDefinitionNode =
| SchemaDefinitionNode
| TypeDefinitionNode
| TypeExtensionNode
| DirectiveDefinitionNode
*/
} }

View File

@ -1,30 +1,24 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class UnionTypeDefinitionNode extends Node implements TypeDefinitionNode class UnionTypeDefinitionNode extends Node implements TypeDefinitionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::UNION_TYPE_DEFINITION; public $kind = NodeKind::UNION_TYPE_DEFINITION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[] */
* @var DirectiveNode[]
*/
public $directives; public $directives;
/** /** @var NamedTypeNode[]|null */
* @var NamedTypeNode[]|null
*/
public $types; public $types;
/** /** @var StringValueNode|null */
* @var StringValueNode|null
*/
public $description; public $description;
} }

View File

@ -1,25 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class UnionTypeExtensionNode extends Node implements TypeExtensionNode class UnionTypeExtensionNode extends Node implements TypeExtensionNode
{ {
/** /** @var string */
* @var string
*/
public $kind = NodeKind::UNION_TYPE_EXTENSION; public $kind = NodeKind::UNION_TYPE_EXTENSION;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
/** /** @var DirectiveNode[]|null */
* @var DirectiveNode[]|null
*/
public $directives; public $directives;
/** /** @var NamedTypeNode[]|null */
* @var NamedTypeNode[]|null
*/
public $types; public $types;
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
/** /**
@ -14,5 +17,4 @@ export type ValueNode = VariableNode
*/ */
interface ValueNode interface ValueNode
{ {
} }

View File

@ -1,22 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class VariableDefinitionNode extends Node implements DefinitionNode class VariableDefinitionNode extends Node implements DefinitionNode
{ {
/** @var string */
public $kind = NodeKind::VARIABLE_DEFINITION; public $kind = NodeKind::VARIABLE_DEFINITION;
/** /** @var VariableNode */
* @var VariableNode
*/
public $variable; public $variable;
/** /** @var TypeNode */
* @var TypeNode
*/
public $type; public $type;
/** /** @var ValueNode|null */
* @var ValueNode|null
*/
public $defaultValue; public $defaultValue;
} }

View File

@ -1,12 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language\AST; namespace GraphQL\Language\AST;
class VariableNode extends Node class VariableNode extends Node
{ {
/** @var string */
public $kind = NodeKind::VARIABLE; public $kind = NodeKind::VARIABLE;
/** /** @var NameNode */
* @var NameNode
*/
public $name; public $name;
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language; namespace GraphQL\Language;
/** /**
@ -28,6 +31,7 @@ class DirectiveLocation
const INPUT_OBJECT = 'INPUT_OBJECT'; const INPUT_OBJECT = 'INPUT_OBJECT';
const INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION'; const INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION';
/** @var string[] */
private static $locations = [ private static $locations = [
self::QUERY => self::QUERY, self::QUERY => self::QUERY,
self::MUTATION => self::MUTATION, self::MUTATION => self::MUTATION,

View File

@ -1,9 +1,16 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language; namespace GraphQL\Language;
use GraphQL\Error\SyntaxError; use GraphQL\Error\SyntaxError;
use GraphQL\Utils\Utils;
use GraphQL\Utils\BlockString; use GraphQL\Utils\BlockString;
use GraphQL\Utils\Utils;
use function chr;
use function hexdec;
use function ord;
use function preg_match;
/** /**
* A Lexer is a stateful stream generator in that every time * A Lexer is a stateful stream generator in that every time
@ -16,14 +23,10 @@ use GraphQL\Utils\BlockString;
*/ */
class Lexer class Lexer
{ {
/** /** @var Source */
* @var Source
*/
public $source; public $source;
/** /** @var bool[] */
* @var array
*/
public $options; public $options;
/** /**
@ -69,10 +72,7 @@ class Lexer
private $byteStreamPosition; private $byteStreamPosition;
/** /**
* Lexer constructor. * @param bool[] $options
*
* @param Source $source
* @param array $options
*/ */
public function __construct(Source $source, array $options = []) public function __construct(Source $source, array $options = [])
{ {
@ -94,6 +94,7 @@ class Lexer
{ {
$this->lastToken = $this->token; $this->lastToken = $this->token;
$token = $this->token = $this->lookahead(); $token = $this->token = $this->lookahead();
return $token; return $token;
} }
@ -105,11 +106,11 @@ class Lexer
$token = $token->next ?: ($token->next = $this->readToken($token)); $token = $token->next ?: ($token->next = $this->readToken($token));
} while ($token->kind === Token::COMMENT); } while ($token->kind === Token::COMMENT);
} }
return $token; return $token;
} }
/** /**
* @param Token $prev
* @return Token * @return Token
* @throws SyntaxError * @throws SyntaxError
*/ */
@ -144,6 +145,7 @@ class Lexer
return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev); return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev);
case 35: // # case 35: // #
$this->moveStringCursor(-1, -1 * $bytes); $this->moveStringCursor(-1, -1 * $bytes);
return $this->readComment($line, $col, $prev); return $this->readComment($line, $col, $prev);
case 36: // $ case 36: // $
return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev); return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev);
@ -178,24 +180,76 @@ class Lexer
case 125: // } case 125: // }
return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev); return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev);
// A-Z // A-Z
case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72: case 65:
case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80: case 66:
case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 67:
case 89: case 90: case 68:
case 69:
case 70:
case 71:
case 72:
case 73:
case 74:
case 75:
case 76:
case 77:
case 78:
case 79:
case 80:
case 81:
case 82:
case 83:
case 84:
case 85:
case 86:
case 87:
case 88:
case 89:
case 90:
// _ // _
case 95: case 95:
// a-z // a-z
case 97: case 98: case 99: case 100: case 101: case 102: case 103: case 104: case 97:
case 105: case 106: case 107: case 108: case 109: case 110: case 111: case 98:
case 112: case 113: case 114: case 115: case 116: case 117: case 118: case 99:
case 119: case 120: case 121: case 122: case 100:
case 101:
case 102:
case 103:
case 104:
case 105:
case 106:
case 107:
case 108:
case 109:
case 110:
case 111:
case 112:
case 113:
case 114:
case 115:
case 116:
case 117:
case 118:
case 119:
case 120:
case 121:
case 122:
return $this->moveStringCursor(-1, -1 * $bytes) return $this->moveStringCursor(-1, -1 * $bytes)
->readName($line, $col, $prev); ->readName($line, $col, $prev);
// - // -
case 45: case 45:
// 0-9 // 0-9
case 48: case 49: case 50: case 51: case 52: case 48:
case 53: case 54: case 55: case 56: case 57: case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
return $this->moveStringCursor(-1, -1 * $bytes) return $this->moveStringCursor(-1, -1 * $bytes)
->readNumber($line, $col, $prev); ->readNumber($line, $col, $prev);
// " // "
@ -230,7 +284,6 @@ class Lexer
* *
* @param int $line * @param int $line
* @param int $col * @param int $col
* @param Token $prev
* @return Token * @return Token
*/ */
private function readName($line, $col, Token $prev) private function readName($line, $col, Token $prev)
@ -248,6 +301,7 @@ class Lexer
$value .= $char; $value .= $char;
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar(); list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
} }
return new Token( return new Token(
Token::NAME, Token::NAME,
$start, $start,
@ -268,7 +322,6 @@ class Lexer
* *
* @param int $line * @param int $line
* @param int $col * @param int $col
* @param Token $prev
* @return Token * @return Token
* @throws SyntaxError * @throws SyntaxError
*/ */
@ -291,7 +344,11 @@ class Lexer
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar(); list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
if ($code >= 48 && $code <= 57) { if ($code >= 48 && $code <= 57) {
throw new SyntaxError($this->source, $this->position, "Invalid number, unexpected digit after 0: " . Utils::printCharCode($code)); throw new SyntaxError(
$this->source,
$this->position,
'Invalid number, unexpected digit after 0: ' . Utils::printCharCode($code)
);
} }
} else { } else {
$value .= $this->readDigits(); $value .= $this->readDigits();
@ -362,7 +419,6 @@ class Lexer
/** /**
* @param int $line * @param int $line
* @param int $col * @param int $col
* @param Token $prev
* @return Token * @return Token
* @throws SyntaxError * @throws SyntaxError
*/ */
@ -371,13 +427,12 @@ class Lexer
$start = $this->position; $start = $this->position;
// Skip leading quote and read first string char: // Skip leading quote and read first string char:
list ($char, $code, $bytes) = $this->moveStringCursor(1, 1)->readChar(); [$char, $code, $bytes] = $this->moveStringCursor(1, 1)->readChar();
$chunk = ''; $chunk = '';
$value = ''; $value = '';
while ( while ($code !== null &&
$code !== null &&
// not LineTerminator // not LineTerminator
$code !== 10 && $code !== 13 $code !== 10 && $code !== 13
) { ) {
@ -407,14 +462,30 @@ class Lexer
list (, $code) = $this->readChar(true); list (, $code) = $this->readChar(true);
switch ($code) { switch ($code) {
case 34: $value .= '"'; break; case 34:
case 47: $value .= '/'; break; $value .= '"';
case 92: $value .= '\\'; break; break;
case 98: $value .= chr(8); break; // \b (backspace) case 47:
case 102: $value .= "\f"; break; $value .= '/';
case 110: $value .= "\n"; break; break;
case 114: $value .= "\r"; break; case 92:
case 116: $value .= "\t"; break; $value .= '\\';
break;
case 98:
$value .= chr(8);
break; // \b (backspace)
case 102:
$value .= "\f";
break;
case 110:
$value .= "\n";
break;
case 114:
$value .= "\r";
break;
case 116:
$value .= "\t";
break;
case 117: case 117:
$position = $this->position; $position = $this->position;
list ($hex) = $this->readChars(4, true); list ($hex) = $this->readChars(4, true);
@ -584,9 +655,8 @@ class Lexer
* *
* #[\u0009\u0020-\uFFFF]* * #[\u0009\u0020-\uFFFF]*
* *
* @param $line * @param int $line
* @param $col * @param int $col
* @param Token $prev
* @return Token * @return Token
*/ */
private function readComment($line, $col, Token $prev) private function readComment($line, $col, Token $prev)
@ -598,8 +668,7 @@ class Lexer
do { do {
list ($char, $code, $bytes) = $this->moveStringCursor(1, $bytes)->readChar(); list ($char, $code, $bytes) = $this->moveStringCursor(1, $bytes)->readChar();
$value .= $char; $value .= $char;
} while ( } while ($code &&
$code &&
// SourceCharacter but not LineTerminator // SourceCharacter but not LineTerminator
($code > 0x001F || $code === 0x0009) ($code > 0x001F || $code === 0x0009)
); );
@ -620,7 +689,7 @@ class Lexer
* *
* @param bool $advance * @param bool $advance
* @param int $byteStreamPosition * @param int $byteStreamPosition
* @return array * @return (string|int)[]
*/ */
private function readChar($advance = false, $byteStreamPosition = null) private function readChar($advance = false, $byteStreamPosition = null)
{ {
@ -664,40 +733,42 @@ class Lexer
/** /**
* Reads next $numberOfChars UTF8 characters from the byte stream, starting from $byteStreamPosition. * Reads next $numberOfChars UTF8 characters from the byte stream, starting from $byteStreamPosition.
* *
* @param $numberOfChars * @param int $charCount
* @param bool $advance * @param bool $advance
* @param null $byteStreamPosition * @param null $byteStreamPosition
* @return array * @return (string|int)[]
*/ */
private function readChars($numberOfChars, $advance = false, $byteStreamPosition = null) private function readChars($charCount, $advance = false, $byteStreamPosition = null)
{ {
$result = ''; $result = '';
$totalBytes = 0; $totalBytes = 0;
$byteOffset = $byteStreamPosition ?: $this->byteStreamPosition; $byteOffset = $byteStreamPosition ?: $this->byteStreamPosition;
for ($i = 0; $i < $numberOfChars; $i++) { for ($i = 0; $i < $charCount; $i++) {
list ($char, $code, $bytes) = $this->readChar(false, $byteOffset); list ($char, $code, $bytes) = $this->readChar(false, $byteOffset);
$totalBytes += $bytes; $totalBytes += $bytes;
$byteOffset += $bytes; $byteOffset += $bytes;
$result .= $char; $result .= $char;
} }
if ($advance) { if ($advance) {
$this->moveStringCursor($numberOfChars, $totalBytes); $this->moveStringCursor($charCount, $totalBytes);
} }
return [$result, $totalBytes]; return [$result, $totalBytes];
} }
/** /**
* Moves internal string cursor position * Moves internal string cursor position
* *
* @param $positionOffset * @param int $positionOffset
* @param $byteStreamOffset * @param int $byteStreamOffset
* @return self * @return self
*/ */
private function moveStringCursor($positionOffset, $byteStreamOffset) private function moveStringCursor($positionOffset, $byteStreamOffset)
{ {
$this->position += $positionOffset; $this->position += $positionOffset;
$this->byteStreamPosition += $byteStreamOffset; $this->byteStreamPosition += $byteStreamOffset;
return $this; return $this;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,29 +1,32 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language; namespace GraphQL\Language;
use GraphQL\Language\AST\ArgumentNode; use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\BooleanValueNode;
use GraphQL\Language\AST\DirectiveDefinitionNode; use GraphQL\Language\AST\DirectiveDefinitionNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode; use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\EnumTypeExtensionNode; use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\EnumValueDefinitionNode; use GraphQL\Language\AST\EnumValueDefinitionNode;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\BooleanValueNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\EnumValueNode; use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\FieldNode; use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FloatValueNode; use GraphQL\Language\AST\FloatValueNode;
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\InlineFragmentNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Language\AST\IntValueNode; use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\ListTypeNode; use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\NamedTypeNode; use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
@ -31,6 +34,7 @@ use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\NullValueNode; use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectFieldNode; use GraphQL\Language\AST\ObjectFieldNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use GraphQL\Language\AST\ObjectValueNode; use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Language\AST\OperationDefinitionNode; use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\OperationTypeDefinitionNode; use GraphQL\Language\AST\OperationTypeDefinitionNode;
@ -39,11 +43,17 @@ use GraphQL\Language\AST\ScalarTypeExtensionNode;
use GraphQL\Language\AST\SchemaDefinitionNode; use GraphQL\Language\AST\SchemaDefinitionNode;
use GraphQL\Language\AST\SelectionSetNode; use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode; use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\AST\UnionTypeExtensionNode; use GraphQL\Language\AST\UnionTypeExtensionNode;
use GraphQL\Language\AST\VariableDefinitionNode; use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use function count;
use function implode;
use function json_encode;
use function preg_replace;
use function sprintf;
use function str_replace;
use function strpos;
/** /**
* Prints AST to string. Capable of printing GraphQL queries and Type definition language. * Prints AST to string. Capable of printing GraphQL queries and Type definition language.
@ -70,25 +80,32 @@ class Printer
{ {
static $instance; static $instance;
$instance = $instance ?: new static(); $instance = $instance ?: new static();
return $instance->printAST($ast); return $instance->printAST($ast);
} }
protected function __construct() protected function __construct()
{} {
}
public function printAST($ast) public function printAST($ast)
{ {
return Visitor::visit($ast, [ return Visitor::visit(
$ast,
[
'leave' => [ 'leave' => [
NodeKind::NAME => function (Node $node) { NodeKind::NAME => function (Node $node) {
return '' . $node->value; return '' . $node->value;
}, },
NodeKind::VARIABLE => function ($node) { NodeKind::VARIABLE => function ($node) {
return '$' . $node->name; return '$' . $node->name;
}, },
NodeKind::DOCUMENT => function (DocumentNode $node) { NodeKind::DOCUMENT => function (DocumentNode $node) {
return $this->join($node->definitions, "\n\n") . "\n"; return $this->join($node->definitions, "\n\n") . "\n";
}, },
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) { NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) {
$op = $node->operation; $op = $node->operation;
$name = $node->name; $name = $node->name;
@ -101,101 +118,126 @@ class Printer
? $selectionSet ? $selectionSet
: $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' '); : $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' ');
}, },
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) { NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) {
return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue); return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue);
}, },
NodeKind::SELECTION_SET => function (SelectionSetNode $node) { NodeKind::SELECTION_SET => function (SelectionSetNode $node) {
return $this->block($node->selections); return $this->block($node->selections);
}, },
NodeKind::FIELD => function (FieldNode $node) { NodeKind::FIELD => function (FieldNode $node) {
return $this->join([ return $this->join(
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'), [
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap(
'(',
$this->join($node->arguments, ', '),
')'
),
$this->join($node->directives, ' '), $this->join($node->directives, ' '),
$node->selectionSet $node->selectionSet,
], ' '); ],
' '
);
}, },
NodeKind::ARGUMENT => function (ArgumentNode $node) { NodeKind::ARGUMENT => function (ArgumentNode $node) {
return $node->name . ': ' . $node->value; return $node->name . ': ' . $node->value;
}, },
// Fragments
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) { NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) {
return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' ')); return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' '));
}, },
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) { NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) {
return $this->join([ return $this->join(
"...", [
'...',
$this->wrap('on ', $node->typeCondition), $this->wrap('on ', $node->typeCondition),
$this->join($node->directives, ' '), $this->join($node->directives, ' '),
$node->selectionSet $node->selectionSet,
], ' '); ],
' '
);
}, },
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) { NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) {
// Note: fragment variable definitions are experimental and may be changed // Note: fragment variable definitions are experimental and may be changed or removed in the future.
// or removed in the future. return sprintf('fragment %s', $node->name)
return "fragment {$node->name}"
. $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')') . $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')')
. " on {$node->typeCondition} " . sprintf(' on %s ', $node->typeCondition)
. $this->wrap('', $this->join($node->directives, ' '), ' ') . $this->wrap('', $this->join($node->directives, ' '), ' ')
. $node->selectionSet; . $node->selectionSet;
}, },
// Value
NodeKind::INT => function (IntValueNode $node) { NodeKind::INT => function (IntValueNode $node) {
return $node->value; return $node->value;
}, },
NodeKind::FLOAT => function (FloatValueNode $node) { NodeKind::FLOAT => function (FloatValueNode $node) {
return $node->value; return $node->value;
}, },
NodeKind::STRING => function (StringValueNode $node, $key) { NodeKind::STRING => function (StringValueNode $node, $key) {
if ($node->block) { if ($node->block) {
return $this->printBlockString($node->value, $key === 'description'); return $this->printBlockString($node->value, $key === 'description');
} }
return json_encode($node->value); return json_encode($node->value);
}, },
NodeKind::BOOLEAN => function (BooleanValueNode $node) { NodeKind::BOOLEAN => function (BooleanValueNode $node) {
return $node->value ? 'true' : 'false'; return $node->value ? 'true' : 'false';
}, },
NodeKind::NULL => function (NullValueNode $node) { NodeKind::NULL => function (NullValueNode $node) {
return 'null'; return 'null';
}, },
NodeKind::ENUM => function (EnumValueNode $node) { NodeKind::ENUM => function (EnumValueNode $node) {
return $node->value; return $node->value;
}, },
NodeKind::LST => function (ListValueNode $node) { NodeKind::LST => function (ListValueNode $node) {
return '[' . $this->join($node->values, ', ') . ']'; return '[' . $this->join($node->values, ', ') . ']';
}, },
NodeKind::OBJECT => function (ObjectValueNode $node) { NodeKind::OBJECT => function (ObjectValueNode $node) {
return '{' . $this->join($node->fields, ', ') . '}'; return '{' . $this->join($node->fields, ', ') . '}';
}, },
NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) { NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) {
return $node->name . ': ' . $node->value; return $node->name . ': ' . $node->value;
}, },
// DirectiveNode
NodeKind::DIRECTIVE => function (DirectiveNode $node) { NodeKind::DIRECTIVE => function (DirectiveNode $node) {
return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'); return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')');
}, },
// Type
NodeKind::NAMED_TYPE => function (NamedTypeNode $node) { NodeKind::NAMED_TYPE => function (NamedTypeNode $node) {
return $node->name; return $node->name;
}, },
NodeKind::LIST_TYPE => function (ListTypeNode $node) { NodeKind::LIST_TYPE => function (ListTypeNode $node) {
return '[' . $node->type . ']'; return '[' . $node->type . ']';
}, },
NodeKind::NON_NULL_TYPE => function (NonNullTypeNode $node) { NodeKind::NON_NULL_TYPE => function (NonNullTypeNode $node) {
return $node->type . '!'; return $node->type . '!';
}, },
// Type System Definitions
NodeKind::SCHEMA_DEFINITION => function (SchemaDefinitionNode $def) { NodeKind::SCHEMA_DEFINITION => function (SchemaDefinitionNode $def) {
return $this->join([ return $this->join(
[
'schema', 'schema',
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->operationTypes) $this->block($def->operationTypes),
], ' '); ],
' '
);
}, },
NodeKind::OPERATION_TYPE_DEFINITION => function (OperationTypeDefinitionNode $def) { NodeKind::OPERATION_TYPE_DEFINITION => function (OperationTypeDefinitionNode $def) {
return $def->operation . ': ' . $def->type; return $def->operation . ': ' . $def->type;
}, },
@ -203,123 +245,179 @@ class Printer
NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function (ScalarTypeDefinitionNode $def) { NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function (ScalarTypeDefinitionNode $def) {
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' '); return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ');
}), }),
NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function (ObjectTypeDefinitionNode $def) { NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function (ObjectTypeDefinitionNode $def) {
return $this->join([ return $this->join(
[
'type', 'type',
$def->name, $def->name,
$this->wrap('implements ', $this->join($def->interfaces, ' & ')), $this->wrap('implements ', $this->join($def->interfaces, ' & ')),
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields) $this->block($def->fields),
], ' '); ],
' '
);
}), }),
NodeKind::FIELD_DEFINITION => $this->addDescription(function (FieldDefinitionNode $def) { NodeKind::FIELD_DEFINITION => $this->addDescription(function (FieldDefinitionNode $def) {
return $def->name return $def->name
. $this->wrap('(', $this->join($def->arguments, ', '), ')') . $this->wrap('(', $this->join($def->arguments, ', '), ')')
. ': ' . $def->type . ': ' . $def->type
. $this->wrap(' ', $this->join($def->directives, ' ')); . $this->wrap(' ', $this->join($def->directives, ' '));
}), }),
NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function (InputValueDefinitionNode $def) { NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function (InputValueDefinitionNode $def) {
return $this->join([ return $this->join(
[
$def->name . ': ' . $def->type, $def->name . ': ' . $def->type,
$this->wrap('= ', $def->defaultValue), $this->wrap('= ', $def->defaultValue),
$this->join($def->directives, ' ') $this->join($def->directives, ' '),
], ' '); ],
' '
);
}), }),
NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription(function(InterfaceTypeDefinitionNode $def) {
return $this->join([ NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription(
function (InterfaceTypeDefinitionNode $def) {
return $this->join(
[
'interface', 'interface',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields) $this->block($def->fields),
], ' '); ],
}), ' '
);
}
),
NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function (UnionTypeDefinitionNode $def) { NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function (UnionTypeDefinitionNode $def) {
return $this->join([ return $this->join(
[
'union', 'union',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$def->types $def->types
? '= ' . $this->join($def->types, ' | ') ? '= ' . $this->join($def->types, ' | ')
: '' : '',
], ' '); ],
' '
);
}), }),
NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function (EnumTypeDefinitionNode $def) { NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function (EnumTypeDefinitionNode $def) {
return $this->join([ return $this->join(
[
'enum', 'enum',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->values) $this->block($def->values),
], ' '); ],
' '
);
}), }),
NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function (EnumValueDefinitionNode $def) { NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function (EnumValueDefinitionNode $def) {
return $this->join([$def->name, $this->join($def->directives, ' ')], ' '); return $this->join([$def->name, $this->join($def->directives, ' ')], ' ');
}), }),
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function(InputObjectTypeDefinitionNode $def) {
return $this->join([ NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function (
InputObjectTypeDefinitionNode $def
) {
return $this->join(
[
'input', 'input',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields) $this->block($def->fields),
], ' '); ],
' '
);
}), }),
NodeKind::SCALAR_TYPE_EXTENSION => function (ScalarTypeExtensionNode $def) { NodeKind::SCALAR_TYPE_EXTENSION => function (ScalarTypeExtensionNode $def) {
return $this->join([ return $this->join(
[
'extend scalar', 'extend scalar',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
], ' '); ],
' '
);
}, },
NodeKind::OBJECT_TYPE_EXTENSION => function (ObjectTypeExtensionNode $def) { NodeKind::OBJECT_TYPE_EXTENSION => function (ObjectTypeExtensionNode $def) {
return $this->join([ return $this->join(
[
'extend type', 'extend type',
$def->name, $def->name,
$this->wrap('implements ', $this->join($def->interfaces, ' & ')), $this->wrap('implements ', $this->join($def->interfaces, ' & ')),
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields), $this->block($def->fields),
], ' '); ],
' '
);
}, },
NodeKind::INTERFACE_TYPE_EXTENSION => function (InterfaceTypeExtensionNode $def) { NodeKind::INTERFACE_TYPE_EXTENSION => function (InterfaceTypeExtensionNode $def) {
return $this->join([ return $this->join(
[
'extend interface', 'extend interface',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields), $this->block($def->fields),
], ' '); ],
' '
);
}, },
NodeKind::UNION_TYPE_EXTENSION => function (UnionTypeExtensionNode $def) { NodeKind::UNION_TYPE_EXTENSION => function (UnionTypeExtensionNode $def) {
return $this->join([ return $this->join(
[
'extend union', 'extend union',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$def->types $def->types
? '= ' . $this->join($def->types, ' | ') ? '= ' . $this->join($def->types, ' | ')
: '' : '',
], ' '); ],
' '
);
}, },
NodeKind::ENUM_TYPE_EXTENSION => function (EnumTypeExtensionNode $def) { NodeKind::ENUM_TYPE_EXTENSION => function (EnumTypeExtensionNode $def) {
return $this->join([ return $this->join(
[
'extend enum', 'extend enum',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->values), $this->block($def->values),
], ' '); ],
' '
);
}, },
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => function (InputObjectTypeExtensionNode $def) { NodeKind::INPUT_OBJECT_TYPE_EXTENSION => function (InputObjectTypeExtensionNode $def) {
return $this->join([ return $this->join(
[
'extend input', 'extend input',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields), $this->block($def->fields),
], ' '); ],
' '
);
}, },
NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function (DirectiveDefinitionNode $def) { NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function (DirectiveDefinitionNode $def) {
return 'directive @' return 'directive @'
. $def->name . $def->name
. $this->wrap('(', $this->join($def->arguments, ', '), ')') . $this->wrap('(', $this->join($def->arguments, ', '), ')')
. ' on ' . $this->join($def->locations, ' | '); . ' on ' . $this->join($def->locations, ' | ');
}) }),
],
] ]
]); );
} }
public function addDescription(\Closure $cb) public function addDescription(\Closure $cb)
@ -371,7 +469,9 @@ class Printer
$separator, $separator,
Utils::filter( Utils::filter(
$maybeArray, $maybeArray,
function($x) { return !!$x;} function ($x) {
return ! ! $x;
}
) )
) )
: ''; : '';
@ -382,8 +482,10 @@ class Printer
* trailing blank line. However, if a block string starts with whitespace and is * trailing blank line. However, if a block string starts with whitespace and is
* a single-line, adding a leading blank line would strip that whitespace. * a single-line, adding a leading blank line would strip that whitespace.
*/ */
private function printBlockString($value, $isDescription) { private function printBlockString($value, $isDescription)
{
$escaped = str_replace('"""', '\\"""', $value); $escaped = str_replace('"""', '\\"""', $value);
return (($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false) return (($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false)
? ('"""' . preg_replace('/"$/', "\"\n", $escaped) . '"""') ? ('"""' . preg_replace('/"$/', "\"\n", $escaped) . '"""')
: ("\"\"\"\n" . ($isDescription ? $escaped : $this->indent($escaped)) . "\n\"\"\""); : ("\"\"\"\n" . ($isDescription ? $escaped : $this->indent($escaped)) . "\n\"\"\"");

View File

@ -1,36 +1,33 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language; namespace GraphQL\Language;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use function is_string;
use function json_decode;
use function mb_strlen;
use function mb_substr;
use function preg_match_all;
use const PREG_OFFSET_CAPTURE;
/**
* Class Source
* @package GraphQL\Language
*/
class Source class Source
{ {
/** /** @var string */
* @var string
*/
public $body; public $body;
/** /** @var int */
* @var int
*/
public $length; public $length;
/** /** @var string */
* @var string
*/
public $name; public $name;
/** /** @var SourceLocation */
* @var SourceLocation
*/
public $locationOffset; public $locationOffset;
/** /**
* Source constructor. *
* *
* A representation of source input to GraphQL. * A representation of source input to GraphQL.
* `name` and `locationOffset` are optional. They are useful for clients who * `name` and `locationOffset` are optional. They are useful for clients who
@ -41,9 +38,8 @@ class Source
* *
* @param string $body * @param string $body
* @param string|null $name * @param string|null $name
* @param SourceLocation|null $location
*/ */
public function __construct($body, $name = null, SourceLocation $location = null) public function __construct($body, $name = null, ?SourceLocation $location = null)
{ {
Utils::invariant( Utils::invariant(
is_string($body), is_string($body),
@ -66,7 +62,7 @@ class Source
} }
/** /**
* @param $position * @param int $position
* @return SourceLocation * @return SourceLocation
*/ */
public function getLocation($position) public function getLocation($position)
@ -81,6 +77,7 @@ class Source
foreach ($matches[0] as $index => $match) { foreach ($matches[0] as $index => $match) {
$line += 1; $line += 1;
$column = $position + 1 - ($match[1] + mb_strlen($match[0], 'UTF-8')); $column = $position + 1 - ($match[1] + mb_strlen($match[0], 'UTF-8'));
} }

View File

@ -1,11 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language; namespace GraphQL\Language;
class SourceLocation implements \JsonSerializable class SourceLocation implements \JsonSerializable
{ {
/** @var int */
public $line; public $line;
/** @var int */
public $column; public $column;
/**
* @param int $line
* @param int $col
*/
public function __construct($line, $col) public function __construct($line, $col)
{ {
$this->line = $line; $this->line = $line;
@ -13,18 +23,18 @@ class SourceLocation implements \JsonSerializable
} }
/** /**
* @return array * @return int[]
*/ */
public function toArray() public function toArray()
{ {
return [ return [
'line' => $this->line, 'line' => $this->line,
'column' => $this->column 'column' => $this->column,
]; ];
} }
/** /**
* @return array * @return int[]
*/ */
public function toSerializableArray() public function toSerializableArray()
{ {
@ -32,13 +42,9 @@ class SourceLocation implements \JsonSerializable
} }
/** /**
* Specify data which should be serialized to JSON * @return int[]
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/ */
function jsonSerialize() public function jsonSerialize()
{ {
return $this->toSerializableArray(); return $this->toSerializableArray();
} }

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language; namespace GraphQL\Language;
/** /**
@ -66,9 +69,7 @@ class Token
*/ */
public $column; public $column;
/** /** @var string|null */
* @var string|null
*/
public $value; public $value;
/** /**
@ -80,28 +81,25 @@ class Token
*/ */
public $prev; public $prev;
/** /** @var Token */
* @var Token
*/
public $next; public $next;
/** /**
* Token constructor. *
* @param $kind * @param string $kind
* @param $start * @param int $start
* @param $end * @param int $end
* @param $line * @param int $line
* @param $column * @param int $column
* @param Token $previous * @param mixed|null $value
* @param null $value
*/ */
public function __construct($kind, $start, $end, $line, $column, Token $previous = null, $value = null) public function __construct($kind, $start, $end, $line, $column, ?Token $previous = null, $value = null)
{ {
$this->kind = $kind; $this->kind = $kind;
$this->start = (int) $start; $this->start = $start;
$this->end = (int) $end; $this->end = $end;
$this->line = (int) $line; $this->line = $line;
$this->column = (int) $column; $this->column = $column;
$this->prev = $previous; $this->prev = $previous;
$this->next = null; $this->next = null;
$this->value = $value; $this->value = $value;
@ -116,7 +114,7 @@ class Token
} }
/** /**
* @return array * @return (string|int|null)[]
*/ */
public function toArray() public function toArray()
{ {
@ -124,7 +122,7 @@ class Token
'kind' => $this->kind, 'kind' => $this->kind,
'value' => $this->value, 'value' => $this->value,
'line' => $this->line, 'line' => $this->line,
'column' => $this->column 'column' => $this->column,
]; ];
} }
} }

View File

@ -1,19 +1,24 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Language; namespace GraphQL\Language;
use ArrayObject;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NodeList; use GraphQL\Language\AST\NodeList;
use GraphQL\Utils\TypeInfo; use GraphQL\Utils\TypeInfo;
use stdClass;
class VisitorOperation use function array_pop;
{ use function array_splice;
public $doBreak; use function call_user_func;
use function call_user_func_array;
public $doContinue; use function count;
use function func_get_args;
public $removeNode; use function is_array;
} use function is_callable;
use function json_encode;
/** /**
* Utility for efficient AST traversal and modification. * Utility for efficient AST traversal and modification.
@ -104,6 +109,7 @@ class VisitorOperation
*/ */
class Visitor class Visitor
{ {
/** @var string[][] */
public static $visitorKeys = [ public static $visitorKeys = [
NodeKind::NAME => [], NodeKind::NAME => [],
NodeKind::DOCUMENT => ['definitions'], NodeKind::DOCUMENT => ['definitions'],
@ -122,7 +128,7 @@ class Visitor
'variableDefinitions', 'variableDefinitions',
'typeCondition', 'typeCondition',
'directives', 'directives',
'selectionSet' 'selectionSet',
], ],
NodeKind::INT => [], NodeKind::INT => [],
@ -158,16 +164,16 @@ class Visitor
NodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'], NodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'],
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => ['name', 'directives', 'fields'], NodeKind::INPUT_OBJECT_TYPE_EXTENSION => ['name', 'directives', 'fields'],
NodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations'] NodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations'],
]; ];
/** /**
* Visit the AST (see class description for details) * Visit the AST (see class description for details)
* *
* @api * @api
* @param Node $root * @param Node|ArrayObject|stdClass $root
* @param array $visitor * @param callable[] $visitor
* @param array $keyMap * @param mixed[]|null $keyMap
* @return Node|mixed * @return Node|mixed
* @throws \Exception * @throws \Exception
*/ */
@ -277,12 +283,12 @@ class Visitor
$edits[] = [$key, $editValue]; $edits[] = [$key, $editValue];
if (! $isLeaving) { if (! $isLeaving) {
if ($editValue instanceof Node) { if (! ($editValue instanceof Node)) {
$node = $editValue;
} else {
array_pop($path); array_pop($path);
continue; continue;
} }
$node = $editValue;
} }
} }
} }
@ -300,7 +306,7 @@ class Visitor
'index' => $index, 'index' => $index,
'keys' => $keys, 'keys' => $keys,
'edits' => $edits, 'edits' => $edits,
'prev' => $stack 'prev' => $stack,
]; ];
$inArray = $node instanceof NodeList || is_array($node); $inArray = $node instanceof NodeList || is_array($node);
@ -312,7 +318,6 @@ class Visitor
} }
$parent = $node; $parent = $node;
} }
} while ($stack); } while ($stack);
if (count($edits) !== 0) { if (count($edits) !== 0) {
@ -332,6 +337,7 @@ class Visitor
{ {
$r = new VisitorOperation(); $r = new VisitorOperation();
$r->doBreak = true; $r->doBreak = true;
return $r; return $r;
} }
@ -345,6 +351,7 @@ class Visitor
{ {
$r = new VisitorOperation(); $r = new VisitorOperation();
$r->doContinue = true; $r->doContinue = true;
return $r; return $r;
} }
@ -358,25 +365,36 @@ class Visitor
{ {
$r = new VisitorOperation(); $r = new VisitorOperation();
$r->removeNode = true; $r->removeNode = true;
return $r; return $r;
} }
/** /**
* @param $visitors * @param callable[][] $visitors
* @return array * @return callable[][]
*/ */
static function visitInParallel($visitors) public static function visitInParallel($visitors)
{ {
$visitorsCount = count($visitors); $visitorsCount = count($visitors);
$skipping = new \SplFixedArray($visitorsCount); $skipping = new \SplFixedArray($visitorsCount);
return [ return [
'enter' => function ($node) use ($visitors, $skipping, $visitorsCount) { 'enter' => function (Node $node) use ($visitors, $skipping, $visitorsCount) {
for ($i = 0; $i < $visitorsCount; $i++) { for ($i = 0; $i < $visitorsCount; $i++) {
if (empty($skipping[$i])) { if (! empty($skipping[$i])) {
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ false); continue;
}
$fn = self::getVisitFn(
$visitors[$i],
$node->kind, /* isLeaving */
false
);
if (! $fn) {
continue;
}
if ($fn) {
$result = call_user_func_array($fn, func_get_args()); $result = call_user_func_array($fn, func_get_args());
if ($result instanceof VisitorOperation) { if ($result instanceof VisitorOperation) {
@ -391,13 +409,15 @@ class Visitor
return $result; return $result;
} }
} }
}
}
}, },
'leave' => function ($node) use ($visitors, $skipping, $visitorsCount) { 'leave' => function (Node $node) use ($visitors, $skipping, $visitorsCount) {
for ($i = 0; $i < $visitorsCount; $i++) { for ($i = 0; $i < $visitorsCount; $i++) {
if (empty($skipping[$i])) { if (empty($skipping[$i])) {
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ true); $fn = self::getVisitFn(
$visitors[$i],
$node->kind, /* isLeaving */
true
);
if ($fn) { if ($fn) {
$result = call_user_func_array($fn, func_get_args()); $result = call_user_func_array($fn, func_get_args());
@ -415,7 +435,7 @@ class Visitor
$skipping[$i] = null; $skipping[$i] = null;
} }
} }
} },
]; ];
} }
@ -423,10 +443,10 @@ class Visitor
* Creates a new visitor instance which maintains a provided TypeInfo instance * Creates a new visitor instance which maintains a provided TypeInfo instance
* along with visiting visitor. * along with visiting visitor.
*/ */
static function visitWithTypeInfo(TypeInfo $typeInfo, $visitor) public static function visitWithTypeInfo(TypeInfo $typeInfo, $visitor)
{ {
return [ return [
'enter' => function ($node) use ($typeInfo, $visitor) { 'enter' => function (Node $node) use ($typeInfo, $visitor) {
$typeInfo->enter($node); $typeInfo->enter($node);
$fn = self::getVisitFn($visitor, $node->kind, false); $fn = self::getVisitFn($visitor, $node->kind, false);
@ -438,31 +458,35 @@ class Visitor
$typeInfo->enter($result); $typeInfo->enter($result);
} }
} }
return $result; return $result;
} }
return null; return null;
}, },
'leave' => function ($node) use ($typeInfo, $visitor) { 'leave' => function (Node $node) use ($typeInfo, $visitor) {
$fn = self::getVisitFn($visitor, $node->kind, true); $fn = self::getVisitFn($visitor, $node->kind, true);
$result = $fn ? call_user_func_array($fn, func_get_args()) : null; $result = $fn ? call_user_func_array($fn, func_get_args()) : null;
$typeInfo->leave($node); $typeInfo->leave($node);
return $result; return $result;
} },
]; ];
} }
/** /**
* @param $visitor * @param callable[]|null $visitor
* @param $kind * @param string $kind
* @param $isLeaving * @param bool $isLeaving
* @return null * @return callable|null
*/ */
public static function getVisitFn($visitor, $kind, $isLeaving) public static function getVisitFn($visitor, $kind, $isLeaving)
{ {
if (!$visitor) { if ($visitor === null) {
return null; return null;
} }
$kindVisitor = isset($visitor[$kind]) ? $visitor[$kind] : null;
$kindVisitor = $visitor[$kind] ?? null;
if (! $isLeaving && is_callable($kindVisitor)) { if (! $isLeaving && is_callable($kindVisitor)) {
// { Kind() {} } // { Kind() {} }
@ -471,19 +495,21 @@ class Visitor
if (is_array($kindVisitor)) { if (is_array($kindVisitor)) {
if ($isLeaving) { if ($isLeaving) {
$kindSpecificVisitor = isset($kindVisitor['leave']) ? $kindVisitor['leave'] : null; $kindSpecificVisitor = $kindVisitor['leave'] ?? null;
} else { } else {
$kindSpecificVisitor = isset($kindVisitor['enter']) ? $kindVisitor['enter'] : null; $kindSpecificVisitor = $kindVisitor['enter'] ?? null;
} }
if ($kindSpecificVisitor && is_callable($kindSpecificVisitor)) { if ($kindSpecificVisitor && is_callable($kindSpecificVisitor)) {
// { Kind: { enter() {}, leave() {} } } // { Kind: { enter() {}, leave() {} } }
return $kindSpecificVisitor; return $kindSpecificVisitor;
} }
return null; return null;
} }
$visitor += ['leave' => null, 'enter' => null]; $visitor += ['leave' => null, 'enter' => null];
$specificVisitor = $isLeaving ? $visitor['leave'] : $visitor['enter']; $specificVisitor = $isLeaving ? $visitor['leave'] : $visitor['enter'];
if ($specificVisitor) { if ($specificVisitor) {
@ -491,13 +517,14 @@ class Visitor
// { enter() {}, leave() {} } // { enter() {}, leave() {} }
return $specificVisitor; return $specificVisitor;
} }
$specificKindVisitor = isset($specificVisitor[$kind]) ? $specificVisitor[$kind] : null; $specificKindVisitor = $specificVisitor[$kind] ?? null;
if (is_callable($specificKindVisitor)) { if (is_callable($specificKindVisitor)) {
// { enter: { Kind() {} }, leave: { Kind() {} } } // { enter: { Kind() {} }, leave: { Kind() {} } }
return $specificKindVisitor; return $specificKindVisitor;
} }
} }
return null; return null;
} }
} }

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace GraphQL\Language;
class VisitorOperation
{
/** @var bool */
public $doBreak;
/** @var bool */
public $doContinue;
/** @var bool */
public $removeNode;
}

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Utils; namespace GraphQL\Utils;
use GraphQL\Error\Error; use GraphQL\Error\Error;
@ -33,6 +36,22 @@ use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ScalarType; use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema; use GraphQL\Type\Schema;
use function array_combine;
use function array_key_exists;
use function array_map;
use function count;
use function floatval;
use function intval;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
use function is_object;
use function is_string;
use function iterator_to_array;
use function json_encode;
use function property_exists;
use function substr;
/** /**
* Various utilities dealing with AST * Various utilities dealing with AST
@ -61,16 +80,16 @@ class AST
* This is a reverse operation for AST::toArray($node) * This is a reverse operation for AST::toArray($node)
* *
* @api * @api
* @param array $node * @param mixed[] $node
* @return Node * @return Node
*/ */
public static function fromArray(array $node) public static function fromArray(array $node)
{ {
if (! isset($node['kind']) || ! isset(NodeKind::$classMap[$node['kind']])) { if (! isset($node['kind']) || ! isset(NodeKind::$classMap[$node['kind']])) {
throw new InvariantViolation("Unexpected node structure: " . Utils::printSafeJson($node)); throw new InvariantViolation('Unexpected node structure: ' . Utils::printSafeJson($node));
} }
$kind = isset($node['kind']) ? $node['kind'] : null; $kind = $node['kind'] ?? null;
$class = NodeKind::$classMap[$kind]; $class = NodeKind::$classMap[$kind];
$instance = new $class([]); $instance = new $class([]);
@ -78,9 +97,8 @@ class AST
$instance->loc = Location::create($node['loc']['start'], $node['loc']['end']); $instance->loc = Location::create($node['loc']['start'], $node['loc']['end']);
} }
foreach ($node as $key => $value) { foreach ($node as $key => $value) {
if ('loc' === $key || 'kind' === $key) { if ($key === 'loc' || $key === 'kind') {
continue; continue;
} }
if (is_array($value)) { if (is_array($value)) {
@ -92,6 +110,7 @@ class AST
} }
$instance->{$key} = $value; $instance->{$key} = $value;
} }
return $instance; return $instance;
} }
@ -99,8 +118,7 @@ class AST
* Convert AST node to serializable array * Convert AST node to serializable array
* *
* @api * @api
* @param Node $node * @return mixed[]
* @return array
*/ */
public static function toArray(Node $node) public static function toArray(Node $node)
{ {
@ -126,17 +144,17 @@ class AST
* | null | NullValue | * | null | NullValue |
* *
* @api * @api
* @param $value * @param Type|mixed|null $value
* @param InputType $type
* @return ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode * @return ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode
*/ */
static function astFromValue($value, InputType $type) public static function astFromValue($value, InputType $type)
{ {
if ($type instanceof NonNull) { if ($type instanceof NonNull) {
$astValue = self::astFromValue($value, $type->getWrappedType()); $astValue = self::astFromValue($value, $type->getWrappedType());
if ($astValue instanceof NullValueNode) { if ($astValue instanceof NullValueNode) {
return null; return null;
} }
return $astValue; return $astValue;
} }
@ -152,12 +170,16 @@ class AST
$valuesNodes = []; $valuesNodes = [];
foreach ($value as $item) { foreach ($value as $item) {
$itemNode = self::astFromValue($item, $itemType); $itemNode = self::astFromValue($item, $itemType);
if ($itemNode) { if (! $itemNode) {
continue;
}
$valuesNodes[] = $itemNode; $valuesNodes[] = $itemNode;
} }
}
return new ListValueNode(['values' => $valuesNodes]); return new ListValueNode(['values' => $valuesNodes]);
} }
return self::astFromValue($value, $itemType); return self::astFromValue($value, $itemType);
} }
@ -173,14 +195,14 @@ class AST
$fieldNodes = []; $fieldNodes = [];
foreach ($fields as $fieldName => $field) { foreach ($fields as $fieldName => $field) {
if ($isArrayLike) { if ($isArrayLike) {
$fieldValue = isset($value[$fieldName]) ? $value[$fieldName] : null; $fieldValue = $value[$fieldName] ?? null;
} else { } else {
$fieldValue = isset($value->{$fieldName}) ? $value->{$fieldName} : null; $fieldValue = $value->{$fieldName} ?? null;
} }
// Have to check additionally if key exists, since we differentiate between // Have to check additionally if key exists, since we differentiate between
// "no key" and "value is null": // "no key" and "value is null":
if (null !== $fieldValue) { if ($fieldValue !== null) {
$fieldExists = true; $fieldExists = true;
} elseif ($isArray) { } elseif ($isArray) {
$fieldExists = array_key_exists($fieldName, $value); $fieldExists = array_key_exists($fieldName, $value);
@ -191,17 +213,22 @@ class AST
$fieldExists = property_exists($value, $fieldName); $fieldExists = property_exists($value, $fieldName);
} }
if ($fieldExists) { if (! $fieldExists) {
continue;
}
$fieldNode = self::astFromValue($fieldValue, $field->getType()); $fieldNode = self::astFromValue($fieldValue, $field->getType());
if ($fieldNode) { if (! $fieldNode) {
continue;
}
$fieldNodes[] = new ObjectFieldNode([ $fieldNodes[] = new ObjectFieldNode([
'name' => new NameNode(['value' => $fieldName]), 'name' => new NameNode(['value' => $fieldName]),
'value' => $fieldNode 'value' => $fieldNode,
]); ]);
} }
}
}
return new ObjectValueNode(['fields' => $fieldNodes]); return new ObjectValueNode(['fields' => $fieldNodes]);
} }
@ -230,9 +257,12 @@ class AST
return new IntValueNode(['value' => $serialized]); return new IntValueNode(['value' => $serialized]);
} }
if (is_float($serialized)) { if (is_float($serialized)) {
// int cast with == used for performance reasons
// @codingStandardsIgnoreLine
if ((int) $serialized == $serialized) { if ((int) $serialized == $serialized) {
return new IntValueNode(['value' => $serialized]); return new IntValueNode(['value' => $serialized]);
} }
return new FloatValueNode(['value' => $serialized]); return new FloatValueNode(['value' => $serialized]);
} }
if (is_string($serialized)) { if (is_string($serialized)) {
@ -250,7 +280,7 @@ class AST
// Use json_encode, which uses the same string encoding as GraphQL, // Use json_encode, which uses the same string encoding as GraphQL,
// then remove the quotes. // then remove the quotes.
return new StringValueNode([ return new StringValueNode([
'value' => substr(json_encode($serialized), 1, -1) 'value' => substr(json_encode($serialized), 1, -1),
]); ]);
} }
@ -280,17 +310,16 @@ class AST
* | Null Value | null | * | Null Value | null |
* *
* @api * @api
* @param $valueNode * @param ValueNode|null $valueNode
* @param InputType $type
* @param mixed[]|null $variables * @param mixed[]|null $variables
* @return array|null|\stdClass * @return mixed[]|null|\stdClass
* @throws \Exception * @throws \Exception
*/ */
public static function valueFromAST($valueNode, InputType $type, $variables = null) public static function valueFromAST($valueNode, InputType $type, $variables = null)
{ {
$undefined = Utils::undefined(); $undefined = Utils::undefined();
if (!$valueNode) { if ($valueNode === null) {
// When there is no AST, then there is also no value. // When there is no AST, then there is also no value.
// Importantly, this is different from returning the GraphQL null value. // Importantly, this is different from returning the GraphQL null value.
return $undefined; return $undefined;
@ -301,6 +330,7 @@ class AST
// Invalid: intentionally return no value. // Invalid: intentionally return no value.
return $undefined; return $undefined;
} }
return self::valueFromAST($valueNode, $type->getWrappedType(), $variables); return self::valueFromAST($valueNode, $type->getWrappedType(), $variables);
} }
@ -346,6 +376,7 @@ class AST
$coercedValues[] = $itemValue; $coercedValues[] = $itemValue;
} }
} }
return $coercedValues; return $coercedValues;
} }
$coercedValue = self::valueFromAST($valueNode, $itemType, $variables); $coercedValue = self::valueFromAST($valueNode, $itemType, $variables);
@ -353,6 +384,7 @@ class AST
// Invalid: intentionally return no value. // Invalid: intentionally return no value.
return $undefined; return $undefined;
} }
return [$coercedValue]; return [$coercedValue];
} }
@ -364,11 +396,16 @@ class AST
$coercedObj = []; $coercedObj = [];
$fields = $type->getFields(); $fields = $type->getFields();
$fieldNodes = Utils::keyMap($valueNode->fields, function($field) {return $field->name->value;}); $fieldNodes = Utils::keyMap(
$valueNode->fields,
function ($field) {
return $field->name->value;
}
);
foreach ($fields as $field) { foreach ($fields as $field) {
/** @var ValueNode $fieldNode */ /** @var ValueNode $fieldNode */
$fieldName = $field->name; $fieldName = $field->name;
$fieldNode = isset($fieldNodes[$fieldName]) ? $fieldNodes[$fieldName] : null; $fieldNode = $fieldNodes[$fieldName] ?? null;
if (! $fieldNode || self::isMissingVariable($fieldNode->value, $variables)) { if (! $fieldNode || self::isMissingVariable($fieldNode->value, $variables)) {
if ($field->defaultValueExists()) { if ($field->defaultValueExists()) {
@ -388,6 +425,7 @@ class AST
} }
$coercedObj[$fieldName] = $fieldValue; $coercedObj[$fieldName] = $fieldValue;
} }
return $coercedObj; return $coercedObj;
} }
@ -437,11 +475,12 @@ class AST
* *
* @api * @api
* @param Node $valueNode * @param Node $valueNode
* @param array|null $variables * @param mixed[]|null $variables
* @return mixed * @return mixed
* @throws \Exception * @throws \Exception
*/ */
public static function valueFromASTUntyped($valueNode, array $variables = null) { public static function valueFromASTUntyped($valueNode, ?array $variables = null)
{
switch (true) { switch (true) {
case $valueNode instanceof NullValueNode: case $valueNode instanceof NullValueNode:
return null; return null;
@ -463,16 +502,21 @@ class AST
case $valueNode instanceof ObjectValueNode: case $valueNode instanceof ObjectValueNode:
return array_combine( return array_combine(
array_map( array_map(
function($field) { return $field->name->value; }, function ($field) {
return $field->name->value;
},
iterator_to_array($valueNode->fields) iterator_to_array($valueNode->fields)
), ),
array_map( array_map(
function($field) use ($variables) { return self::valueFromASTUntyped($field->value, $variables); }, function ($field) use ($variables) {
return self::valueFromASTUntyped($field->value, $variables);
},
iterator_to_array($valueNode->fields) iterator_to_array($valueNode->fields)
) )
); );
case $valueNode instanceof VariableNode: case $valueNode instanceof VariableNode:
$variableName = $valueNode->name->value; $variableName = $valueNode->name->value;
return ($variables && isset($variables[$variableName])) return ($variables && isset($variables[$variableName]))
? $variables[$variableName] ? $variables[$variableName]
: null; : null;
@ -485,7 +529,6 @@ class AST
* Returns type definition for given AST Type node * Returns type definition for given AST Type node
* *
* @api * @api
* @param Schema $schema
* @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode * @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
* @return Type|null * @return Type|null
* @throws \Exception * @throws \Exception
@ -494,10 +537,12 @@ class AST
{ {
if ($inputTypeNode instanceof ListTypeNode) { if ($inputTypeNode instanceof ListTypeNode) {
$innerType = self::typeFromAST($schema, $inputTypeNode->type); $innerType = self::typeFromAST($schema, $inputTypeNode->type);
return $innerType ? new ListOfType($innerType) : null; return $innerType ? new ListOfType($innerType) : null;
} }
if ($inputTypeNode instanceof NonNullTypeNode) { if ($inputTypeNode instanceof NonNullTypeNode) {
$innerType = self::typeFromAST($schema, $inputTypeNode->type); $innerType = self::typeFromAST($schema, $inputTypeNode->type);
return $innerType ? new NonNull($innerType) : null; return $innerType ? new NonNull($innerType) : null;
} }
if ($inputTypeNode instanceof NamedTypeNode) { if ($inputTypeNode instanceof NamedTypeNode) {
@ -510,21 +555,20 @@ class AST
/** /**
* Returns true if the provided valueNode is a variable which is not defined * Returns true if the provided valueNode is a variable which is not defined
* in the set of variables. * in the set of variables.
* @param $valueNode * @param ValueNode $valueNode
* @param $variables * @param mixed[] $variables
* @return bool * @return bool
*/ */
private static function isMissingVariable($valueNode, $variables) private static function isMissingVariable($valueNode, $variables)
{ {
return $valueNode instanceof VariableNode && return $valueNode instanceof VariableNode &&
(!$variables || !array_key_exists($valueNode->name->value, $variables)); (count($variables) === 0 || ! array_key_exists($valueNode->name->value, $variables));
} }
/** /**
* Returns operation type ("query", "mutation" or "subscription") given a document and operation name * Returns operation type ("query", "mutation" or "subscription") given a document and operation name
* *
* @api * @api
* @param DocumentNode $document
* @param string $operationName * @param string $operationName
* @return bool * @return bool
*/ */
@ -532,13 +576,16 @@ class AST
{ {
if ($document->definitions) { if ($document->definitions) {
foreach ($document->definitions as $def) { foreach ($document->definitions as $def) {
if ($def instanceof OperationDefinitionNode) { if (! ($def instanceof OperationDefinitionNode)) {
continue;
}
if (! $operationName || (isset($def->name->value) && $def->name->value === $operationName)) { if (! $operationName || (isset($def->name->value) && $def->name->value === $operationName)) {
return $def->operation; return $def->operation;
} }
} }
} }
}
return false; return false;
} }
} }

View File

@ -1,16 +1,21 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Utils; namespace GraphQL\Utils;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Executor\Values; use GraphQL\Executor\Values;
use GraphQL\Language\AST\DirectiveDefinitionNode; use GraphQL\Language\AST\DirectiveDefinitionNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode; use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\EnumValueDefinitionNode; use GraphQL\Language\AST\EnumValueDefinitionNode;
use GraphQL\Language\AST\FieldDefinitionNode; use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode; use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode; use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ListTypeNode; use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode; use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NonNullTypeNode; use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode; use GraphQL\Language\AST\ObjectTypeDefinitionNode;
@ -19,47 +24,49 @@ use GraphQL\Language\AST\TypeNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode; use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\Token; use GraphQL\Language\Token;
use GraphQL\Type\Definition\CustomScalarType; use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InputType; use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType; use GraphQL\Type\Definition\UnionType;
use function array_reverse;
use function implode;
use function is_array;
use function is_string;
use function sprintf;
class ASTDefinitionBuilder class ASTDefinitionBuilder
{ {
/** @var Node[] */
/**
* @var array
*/
private $typeDefintionsMap; private $typeDefintionsMap;
/** /** @var callable */
* @var callable
*/
private $typeConfigDecorator; private $typeConfigDecorator;
/** /** @var bool[] */
* @var array
*/
private $options; private $options;
/** /** @var callable */
* @var callable
*/
private $resolveType; private $resolveType;
/** /** @var Type[] */
* @var array
*/
private $cache; private $cache;
public function __construct(array $typeDefintionsMap, $options, callable $resolveType, callable $typeConfigDecorator = null) /**
{ * @param Node[] $typeDefintionsMap
* @param bool[] $options
*/
public function __construct(
array $typeDefintionsMap,
$options,
callable $resolveType,
?callable $typeConfigDecorator = null
) {
$this->typeDefintionsMap = $typeDefintionsMap; $this->typeDefintionsMap = $typeDefintionsMap;
$this->typeConfigDecorator = $typeConfigDecorator; $this->typeConfigDecorator = $typeConfigDecorator;
$this->options = $options; $this->options = $options;
@ -69,19 +76,20 @@ class ASTDefinitionBuilder
} }
/** /**
* @param Type $innerType
* @param TypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode * @param TypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
* @return Type * @return Type
*/ */
private function buildWrappedType(Type $innerType, TypeNode $inputTypeNode) private function buildWrappedType(Type $innerType, TypeNode $inputTypeNode)
{ {
if ($inputTypeNode->kind == NodeKind::LIST_TYPE) { if ($inputTypeNode->kind === NodeKind::LIST_TYPE) {
return Type::listOf($this->buildWrappedType($innerType, $inputTypeNode->type)); return Type::listOf($this->buildWrappedType($innerType, $inputTypeNode->type));
} }
if ($inputTypeNode->kind == NodeKind::NON_NULL_TYPE) { if ($inputTypeNode->kind === NodeKind::NON_NULL_TYPE) {
$wrappedType = $this->buildWrappedType($innerType, $inputTypeNode->type); $wrappedType = $this->buildWrappedType($innerType, $inputTypeNode->type);
return Type::nonNull(NonNull::assertNullableType($wrappedType)); return Type::nonNull(NonNull::assertNullableType($wrappedType));
} }
return $innerType; return $innerType;
} }
@ -95,6 +103,7 @@ class ASTDefinitionBuilder
while ($namedType->kind === NodeKind::LIST_TYPE || $namedType->kind === NodeKind::NON_NULL_TYPE) { while ($namedType->kind === NodeKind::LIST_TYPE || $namedType->kind === NodeKind::NON_NULL_TYPE) {
$namedType = $namedType->type; $namedType = $namedType->type;
} }
return $namedType; return $namedType;
} }
@ -104,7 +113,8 @@ class ASTDefinitionBuilder
* @return Type * @return Type
* @throws Error * @throws Error
*/ */
private function internalBuildType($typeName, $typeNode = null) { private function internalBuildType($typeName, $typeNode = null)
{
if (! isset($this->cache[$typeName])) { if (! isset($this->cache[$typeName])) {
if (isset($this->typeDefintionsMap[$typeName])) { if (isset($this->typeDefintionsMap[$typeName])) {
$type = $this->makeSchemaDef($this->typeDefintionsMap[$typeName]); $type = $this->makeSchemaDef($this->typeDefintionsMap[$typeName]);
@ -112,20 +122,10 @@ class ASTDefinitionBuilder
$fn = $this->typeConfigDecorator; $fn = $this->typeConfigDecorator;
try { try {
$config = $fn($type->config, $this->typeDefintionsMap[$typeName], $this->typeDefintionsMap); $config = $fn($type->config, $this->typeDefintionsMap[$typeName], $this->typeDefintionsMap);
} catch (\Exception $e) {
throw new Error(
"Type config decorator passed to " . (static::class) . " threw an error " .
"when building $typeName type: {$e->getMessage()}",
null,
null,
null,
null,
$e
);
} catch (\Throwable $e) { } catch (\Throwable $e) {
throw new Error( throw new Error(
"Type config decorator passed to " . (static::class) . " threw an error " . sprintf('Type config decorator passed to %s threw an error ', static::class) .
"when building $typeName type: {$e->getMessage()}", sprintf('when building %s type: %s', $typeName, $e->getMessage()),
null, null,
null, null,
null, null,
@ -135,8 +135,11 @@ class ASTDefinitionBuilder
} }
if (! is_array($config) || isset($config[0])) { if (! is_array($config) || isset($config[0])) {
throw new Error( throw new Error(
"Type config decorator passed to " . (static::class) . " is expected to return an array, but got " . sprintf(
'Type config decorator passed to %s is expected to return an array, but got %s',
static::class,
Utils::getVariableType($config) Utils::getVariableType($config)
)
); );
} }
$type = $this->makeSchemaDefFromConfig($this->typeDefintionsMap[$typeName], $config); $type = $this->makeSchemaDefFromConfig($this->typeDefintionsMap[$typeName], $config);
@ -166,13 +169,13 @@ class ASTDefinitionBuilder
} }
/** /**
* @param TypeNode $typeNode
* @return Type|InputType * @return Type|InputType
* @throws Error * @throws Error
*/ */
private function internalBuildWrappedType(TypeNode $typeNode) private function internalBuildWrappedType(TypeNode $typeNode)
{ {
$typeDef = $this->buildType($this->getNamedTypeNode($typeNode)); $typeDef = $this->buildType($this->getNamedTypeNode($typeNode));
return $this->buildWrappedType($typeDef, $typeNode); return $this->buildWrappedType($typeDef, $typeNode);
} }
@ -181,9 +184,12 @@ class ASTDefinitionBuilder
return new Directive([ return new Directive([
'name' => $directiveNode->name->value, 'name' => $directiveNode->name->value,
'description' => $this->getDescription($directiveNode), 'description' => $this->getDescription($directiveNode),
'locations' => Utils::map($directiveNode->locations, function ($node) { 'locations' => Utils::map(
$directiveNode->locations,
function ($node) {
return $node->value; return $node->value;
}), }
),
'args' => $directiveNode->arguments ? FieldArgument::createMap($this->makeInputValues($directiveNode->arguments)) : null, 'args' => $directiveNode->arguments ? FieldArgument::createMap($this->makeInputValues($directiveNode->arguments)) : null,
'astNode' => $directiveNode, 'astNode' => $directiveNode,
]); ]);
@ -203,6 +209,11 @@ class ASTDefinitionBuilder
]; ];
} }
/**
* @param ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode|EnumTypeDefinitionNode|ScalarTypeDefinitionNode|InputObjectTypeDefinitionNode|UnionTypeDefinitionNode $def
* @return CustomScalarType|EnumType|InputObjectType|InterfaceType|ObjectType|UnionType
* @throws Error
*/
private function makeSchemaDef($def) private function makeSchemaDef($def)
{ {
if (! $def) { if (! $def) {
@ -222,10 +233,16 @@ class ASTDefinitionBuilder
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION: case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
return $this->makeInputObjectDef($def); return $this->makeInputObjectDef($def);
default: default:
throw new Error("Type kind of {$def->kind} not supported."); throw new Error(sprintf('Type kind of %s not supported.', $def->kind));
} }
} }
/**
* @param ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode|EnumTypeExtensionNode|ScalarTypeDefinitionNode|InputObjectTypeDefinitionNode $def
* @param mixed[] $config
* @return CustomScalarType|EnumType|InputObjectType|InterfaceType|ObjectType|UnionType
* @throws Error
*/
private function makeSchemaDefFromConfig($def, array $config) private function makeSchemaDefFromConfig($def, array $config)
{ {
if (! $def) { if (! $def) {
@ -245,13 +262,14 @@ class ASTDefinitionBuilder
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION: case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
return new InputObjectType($config); return new InputObjectType($config);
default: default:
throw new Error("Type kind of {$def->kind} not supported."); throw new Error(sprintf('Type kind of %s not supported.', $def->kind));
} }
} }
private function makeTypeDef(ObjectTypeDefinitionNode $def) private function makeTypeDef(ObjectTypeDefinitionNode $def)
{ {
$typeName = $def->name->value; $typeName = $def->name->value;
return new ObjectType([ return new ObjectType([
'name' => $typeName, 'name' => $typeName,
'description' => $this->getDescription($def), 'description' => $this->getDescription($def),
@ -261,7 +279,7 @@ class ASTDefinitionBuilder
'interfaces' => function () use ($def) { 'interfaces' => function () use ($def) {
return $this->makeImplementedInterfaces($def); return $this->makeImplementedInterfaces($def);
}, },
'astNode' => $def 'astNode' => $def,
]); ]);
} }
@ -286,10 +304,14 @@ class ASTDefinitionBuilder
// Note: While this could make early assertions to get the correctly // Note: While this could make early assertions to get the correctly
// typed values, that would throw immediately while type system // typed values, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results. // validation with validateSchema() will produce more actionable results.
return Utils::map($def->interfaces, function ($iface) { return Utils::map(
$def->interfaces,
function ($iface) {
return $this->buildType($iface); return $this->buildType($iface);
});
} }
);
}
return null; return null;
} }
@ -309,11 +331,12 @@ class ASTDefinitionBuilder
'name' => $value->name->value, 'name' => $value->name->value,
'type' => $type, 'type' => $type,
'description' => $this->getDescription($value), 'description' => $this->getDescription($value),
'astNode' => $value 'astNode' => $value,
]; ];
if (isset($value->defaultValue)) { if (isset($value->defaultValue)) {
$config['defaultValue'] = AST::valueFromAST($value->defaultValue, $type); $config['defaultValue'] = AST::valueFromAST($value->defaultValue, $type);
} }
return $config; return $config;
} }
); );
@ -322,13 +345,14 @@ class ASTDefinitionBuilder
private function makeInterfaceDef(InterfaceTypeDefinitionNode $def) private function makeInterfaceDef(InterfaceTypeDefinitionNode $def)
{ {
$typeName = $def->name->value; $typeName = $def->name->value;
return new InterfaceType([ return new InterfaceType([
'name' => $typeName, 'name' => $typeName,
'description' => $this->getDescription($def), 'description' => $this->getDescription($def),
'fields' => function () use ($def) { 'fields' => function () use ($def) {
return $this->makeFieldDefMap($def); return $this->makeFieldDefMap($def);
}, },
'astNode' => $def 'astNode' => $def,
]); ]);
} }
@ -347,7 +371,7 @@ class ASTDefinitionBuilder
return [ return [
'description' => $this->getDescription($enumValue), 'description' => $this->getDescription($enumValue),
'deprecationReason' => $this->getDeprecationReason($enumValue), 'deprecationReason' => $this->getDeprecationReason($enumValue),
'astNode' => $enumValue 'astNode' => $enumValue,
]; ];
} }
) )
@ -365,9 +389,12 @@ class ASTDefinitionBuilder
// values below, that would throw immediately while type system // values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results. // validation with validateSchema() will produce more actionable results.
'types' => $def->types 'types' => $def->types
? Utils::map($def->types, function ($typeNode) { ? Utils::map(
$def->types,
function ($typeNode) {
return $this->buildType($typeNode); return $this->buildType($typeNode);
}): }
) :
[], [],
'astNode' => $def, 'astNode' => $def,
]); ]);
@ -409,7 +436,8 @@ class ASTDefinitionBuilder
private function getDeprecationReason($node) private function getDeprecationReason($node)
{ {
$deprecated = Values::getDirectiveValues(Directive::deprecatedDirective(), $node); $deprecated = Values::getDirectiveValues(Directive::deprecatedDirective(), $node);
return isset($deprecated['reason']) ? $deprecated['reason'] : null;
return $deprecated['reason'] ?? null;
} }
/** /**
@ -438,8 +466,7 @@ class ASTDefinitionBuilder
} }
$comments = []; $comments = [];
$token = $loc->startToken->prev; $token = $loc->startToken->prev;
while ( while ($token &&
$token &&
$token->kind === Token::COMMENT && $token->kind === Token::COMMENT &&
$token->next && $token->prev && $token->next && $token->prev &&
$token->line + 1 === $token->next->line && $token->line + 1 === $token->next->line &&

View File

@ -6,7 +6,9 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Language\AST\ArgumentNode; use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NodeList;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
use function array_map; use function array_map;
@ -25,6 +27,7 @@ class KnownArgumentNames extends ValidationRule
{ {
return [ return [
NodeKind::ARGUMENT => function (ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) { NodeKind::ARGUMENT => function (ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) {
/** @var NodeList|Node[] $ancestors */
$argDef = $context->getArgument(); $argDef = $context->getArgument();
if ($argDef !== null) { if ($argDef !== null) {
return; return;

View File

@ -6,6 +6,7 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\OperationDefinitionNode; use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
@ -28,7 +29,7 @@ class LoneAnonymousOperation extends ValidationRule
NodeKind::DOCUMENT => function (DocumentNode $node) use (&$operationCount) { NodeKind::DOCUMENT => function (DocumentNode $node) use (&$operationCount) {
$tmp = Utils::filter( $tmp = Utils::filter(
$node->definitions, $node->definitions,
function ($definition) { function (Node $definition) {
return $definition->kind === NodeKind::OPERATION_DEFINITION; return $definition->kind === NodeKind::OPERATION_DEFINITION;
} }
); );

View File

@ -111,7 +111,7 @@ abstract class QuerySecurityRule extends ValidationRule
$_astAndDefs = $astAndDefs ?: new \ArrayObject(); $_astAndDefs = $astAndDefs ?: new \ArrayObject();
foreach ($selectionSet->selections as $selection) { foreach ($selectionSet->selections as $selection) {
switch ($selection->getKind()) { switch ($selection->kind) {
case NodeKind::FIELD: case NodeKind::FIELD:
/** @var FieldNode $selection */ /** @var FieldNode $selection */
$fieldName = $selection->name->value; $fieldName = $selection->name->value;

View File

@ -11,6 +11,7 @@ use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\HasSelectionSet; use GraphQL\Language\AST\HasSelectionSet;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\OperationDefinitionNode; use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Language\AST\VariableNode; use GraphQL\Language\AST\VariableNode;
use GraphQL\Language\Visitor; use GraphQL\Language\Visitor;
use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\FieldDefinition;
@ -191,6 +192,7 @@ class ValidationContext
$spreads = $this->fragmentSpreads[$node] ?? null; $spreads = $this->fragmentSpreads[$node] ?? null;
if (! $spreads) { if (! $spreads) {
$spreads = []; $spreads = [];
/** @var SelectionSetNode[] $setsToVisit */
$setsToVisit = [$node->selectionSet]; $setsToVisit = [$node->selectionSet];
while (! empty($setsToVisit)) { while (! empty($setsToVisit)) {
$set = array_pop($setsToVisit); $set = array_pop($setsToVisit);

View File

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace GraphQL\Tests\Language; namespace GraphQL\Tests\Language;
use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\AST\DocumentNode;
@ -15,6 +18,14 @@ use GraphQL\Language\Visitor;
use GraphQL\Tests\Validator\ValidatorTestCase; use GraphQL\Tests\Validator\ValidatorTestCase;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Utils\TypeInfo; use GraphQL\Utils\TypeInfo;
use function array_keys;
use function array_slice;
use function count;
use function file_get_contents;
use function func_get_args;
use function gettype;
use function is_array;
use function iterator_to_array;
class VisitorTest extends ValidatorTestCase class VisitorTest extends ValidatorTestCase
{ {
@ -60,7 +71,10 @@ class VisitorTest extends ValidatorTestCase
$this->assertInternalType('array', $ancestors); $this->assertInternalType('array', $ancestors);
$this->assertCount(count($path) - 1, $ancestors); $this->assertCount(count($path) - 1, $ancestors);
if (!$isEdited) { if ($isEdited) {
return;
}
$this->assertEquals($node, $parentArray[$key]); $this->assertEquals($node, $parentArray[$key]);
$this->assertEquals($node, $this->getNodeByPath($ast, $path)); $this->assertEquals($node, $this->getNodeByPath($ast, $path));
$ancestorsLength = count($ancestors); $ancestorsLength = count($ancestors);
@ -69,7 +83,6 @@ class VisitorTest extends ValidatorTestCase
$this->assertEquals($ancestors[$i], $this->getNodeByPath($ast, $ancestorPath)); $this->assertEquals($ancestors[$i], $this->getNodeByPath($ast, $ancestorPath));
} }
} }
}
public function testValidatesPathArgument() public function testValidatesPathArgument()
{ {
@ -104,9 +117,6 @@ class VisitorTest extends ValidatorTestCase
$this->assertEquals($expected, $visited); $this->assertEquals($expected, $visited);
} }
/**
* @it allows editing a node both on enter and on leave
*/
public function testAllowsEditingNodeOnEnterAndOnLeave() public function testAllowsEditingNodeOnEnterAndOnLeave()
{ {
$ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]); $ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]);
@ -120,7 +130,7 @@ class VisitorTest extends ValidatorTestCase
$newNode = clone $node; $newNode = clone $node;
$newNode->selectionSet = new SelectionSetNode([ $newNode->selectionSet = new SelectionSetNode([
'selections' => [] 'selections' => [],
]); ]);
$newNode->didEnter = true; $newNode->didEnter = true;
return $newNode; return $newNode;
@ -131,8 +141,8 @@ class VisitorTest extends ValidatorTestCase
$newNode->selectionSet = $selectionSet; $newNode->selectionSet = $selectionSet;
$newNode->didLeave = true; $newNode->didLeave = true;
return $newNode; return $newNode;
} },
] ],
]); ]);
$this->assertNotEquals($ast, $editedAst); $this->assertNotEquals($ast, $editedAst);
@ -144,9 +154,6 @@ class VisitorTest extends ValidatorTestCase
$this->assertEquals($expected, $editedAst); $this->assertEquals($expected, $editedAst);
} }
/**
* @it allows editing the root node on enter and on leave
*/
public function testAllowsEditingRootNodeOnEnterAndLeave() public function testAllowsEditingRootNodeOnEnterAndLeave()
{ {
$ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]); $ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]);
@ -163,11 +170,10 @@ class VisitorTest extends ValidatorTestCase
}, },
'leave' => function (DocumentNode $node) use ($definitions, $ast) { 'leave' => function (DocumentNode $node) use ($definitions, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args(), true); $this->checkVisitorFnArgs($ast, func_get_args(), true);
$tmp = clone $node;
$node->definitions = $definitions; $node->definitions = $definitions;
$node->didLeave = true; $node->didLeave = true;
} },
] ],
]); ]);
$this->assertNotEquals($ast, $editedAst); $this->assertNotEquals($ast, $editedAst);
@ -179,9 +185,6 @@ class VisitorTest extends ValidatorTestCase
$this->assertEquals($tmp, $editedAst); $this->assertEquals($tmp, $editedAst);
} }
/**
* @it allows for editing on enter
*/
public function testAllowsForEditingOnEnter() public function testAllowsForEditingOnEnter()
{ {
$ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]); $ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]);
@ -191,7 +194,7 @@ class VisitorTest extends ValidatorTestCase
if ($node instanceof FieldNode && $node->name->value === 'b') { if ($node instanceof FieldNode && $node->name->value === 'b') {
return Visitor::removeNode(); return Visitor::removeNode();
} }
} },
]); ]);
$this->assertEquals( $this->assertEquals(
@ -204,9 +207,6 @@ class VisitorTest extends ValidatorTestCase
); );
} }
/**
* @it allows for editing on leave
*/
public function testAllowsForEditingOnLeave() public function testAllowsForEditingOnLeave()
{ {
$ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]); $ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]);
@ -216,7 +216,7 @@ class VisitorTest extends ValidatorTestCase
if ($node instanceof FieldNode && $node->name->value === 'b') { if ($node instanceof FieldNode && $node->name->value === 'b') {
return Visitor::removeNode(); return Visitor::removeNode();
} }
} },
]); ]);
$this->assertEquals( $this->assertEquals(
@ -230,16 +230,11 @@ class VisitorTest extends ValidatorTestCase
); );
} }
/**
* @it visits edited node
*/
public function testVisitsEditedNode() public function testVisitsEditedNode()
{ {
$addedField = new FieldNode(array( $addedField = new FieldNode([
'name' => new NameNode(array( 'name' => new NameNode(['value' => '__typename']),
'value' => '__typename' ]);
))
));
$didVisitAddedField = false; $didVisitAddedField = false;
@ -250,23 +245,22 @@ class VisitorTest extends ValidatorTestCase
$this->checkVisitorFnArgs($ast, func_get_args(), true); $this->checkVisitorFnArgs($ast, func_get_args(), true);
if ($node instanceof FieldNode && $node->name->value === 'a') { if ($node instanceof FieldNode && $node->name->value === 'a') {
return new FieldNode([ return new FieldNode([
'selectionSet' => new SelectionSetNode(array( 'selectionSet' => new SelectionSetNode([
'selections' => NodeList::create([$addedField])->merge($node->selectionSet->selections) 'selections' => NodeList::create([$addedField])->merge($node->selectionSet->selections),
)) ]),
]); ]);
} }
if ($node === $addedField) { if ($node !== $addedField) {
return;
}
$didVisitAddedField = true; $didVisitAddedField = true;
} },
}
]); ]);
$this->assertTrue($didVisitAddedField); $this->assertTrue($didVisitAddedField);
} }
/**
* @it allows skipping a sub-tree
*/
public function testAllowsSkippingASubTree() public function testAllowsSkippingASubTree()
{ {
$visited = []; $visited = [];
@ -275,15 +269,15 @@ class VisitorTest extends ValidatorTestCase
Visitor::visit($ast, [ Visitor::visit($ast, [
'enter' => function (Node $node) use (&$visited, $ast) { 'enter' => function (Node $node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['enter', $node->kind, $node->value ?? null];
if ($node instanceof FieldNode && $node->name->value === 'b') { if ($node instanceof FieldNode && $node->name->value === 'b') {
return Visitor::skipNode(); return Visitor::skipNode();
} }
}, },
'leave' => function (Node $node) use (&$visited, $ast) { 'leave' => function (Node $node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
} },
]); ]);
$expected = [ $expected = [
@ -301,15 +295,12 @@ class VisitorTest extends ValidatorTestCase
[ 'leave', 'Field', null ], [ 'leave', 'Field', null ],
[ 'leave', 'SelectionSet', null ], [ 'leave', 'SelectionSet', null ],
[ 'leave', 'OperationDefinition', null ], [ 'leave', 'OperationDefinition', null ],
[ 'leave', 'Document', null ] [ 'leave', 'Document', null ],
]; ];
$this->assertEquals($expected, $visited); $this->assertEquals($expected, $visited);
} }
/**
* @it allows early exit while visiting
*/
public function testAllowsEarlyExitWhileVisiting() public function testAllowsEarlyExitWhileVisiting()
{ {
$visited = []; $visited = [];
@ -318,15 +309,15 @@ class VisitorTest extends ValidatorTestCase
Visitor::visit($ast, [ Visitor::visit($ast, [
'enter' => function (Node $node) use (&$visited, $ast) { 'enter' => function (Node $node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['enter', $node->kind, $node->value ?? null];
if ($node instanceof NameNode && $node->value === 'x') { if ($node instanceof NameNode && $node->value === 'x') {
return Visitor::stop(); return Visitor::stop();
} }
}, },
'leave' => function (Node $node) use (&$visited, $ast) { 'leave' => function (Node $node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
} },
]); ]);
$expected = [ $expected = [
@ -342,15 +333,12 @@ class VisitorTest extends ValidatorTestCase
[ 'leave', 'Name', 'b' ], [ 'leave', 'Name', 'b' ],
[ 'enter', 'SelectionSet', null ], [ 'enter', 'SelectionSet', null ],
[ 'enter', 'Field', null ], [ 'enter', 'Field', null ],
[ 'enter', 'Name', 'x' ] [ 'enter', 'Name', 'x' ],
]; ];
$this->assertEquals($expected, $visited); $this->assertEquals($expected, $visited);
} }
/**
* @it allows early exit while leaving
*/
public function testAllowsEarlyExitWhileLeaving() public function testAllowsEarlyExitWhileLeaving()
{ {
$visited = []; $visited = [];
@ -359,16 +347,16 @@ class VisitorTest extends ValidatorTestCase
Visitor::visit($ast, [ Visitor::visit($ast, [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['enter', $node->kind, $node->value ?? null];
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
if ($node->kind === NodeKind::NAME && $node->value === 'x') { if ($node->kind === NodeKind::NAME && $node->value === 'x') {
return Visitor::stop(); return Visitor::stop();
} }
} },
]); ]);
$this->assertEquals($visited, [ $this->assertEquals($visited, [
@ -385,13 +373,10 @@ class VisitorTest extends ValidatorTestCase
[ 'enter', 'SelectionSet', null ], [ 'enter', 'SelectionSet', null ],
[ 'enter', 'Field', null ], [ 'enter', 'Field', null ],
[ 'enter', 'Name', 'x' ], [ 'enter', 'Name', 'x' ],
[ 'leave', 'Name', 'x' ] [ 'leave', 'Name', 'x' ],
]); ]);
} }
/**
* @it allows a named functions visitor API
*/
public function testAllowsANamedFunctionsVisitorAPI() public function testAllowsANamedFunctionsVisitorAPI()
{ {
$visited = []; $visited = [];
@ -410,8 +395,8 @@ class VisitorTest extends ValidatorTestCase
'leave' => function (SelectionSetNode $node) use (&$visited, $ast) { 'leave' => function (SelectionSetNode $node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, null]; $visited[] = ['leave', $node->kind, null];
} },
] ],
]); ]);
$expected = [ $expected = [
@ -428,9 +413,6 @@ class VisitorTest extends ValidatorTestCase
$this->assertEquals($expected, $visited); $this->assertEquals($expected, $visited);
} }
/**
* @it Experimental: visits variables defined in fragments
*/
public function testExperimentalVisitsVariablesDefinedInFragments() public function testExperimentalVisitsVariablesDefinedInFragments()
{ {
$ast = Parser::parse( $ast = Parser::parse(
@ -445,11 +427,11 @@ class VisitorTest extends ValidatorTestCase
Visitor::visit($ast, [ Visitor::visit($ast, [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['enter', $node->kind, $node->value ?? null];
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
}, },
]); ]);
@ -487,9 +469,6 @@ class VisitorTest extends ValidatorTestCase
$this->assertEquals($expected, $visited); $this->assertEquals($expected, $visited);
} }
/**
* @it visits kitchen sink
*/
public function testVisitsKitchenSink() public function testVisitsKitchenSink()
{ {
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql'); $kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
@ -506,7 +485,7 @@ class VisitorTest extends ValidatorTestCase
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$r = ['leave', $node->kind, $key, $parent instanceof Node ? $parent->kind : null]; $r = ['leave', $node->kind, $key, $parent instanceof Node ? $parent->kind : null];
$visited[] = $r; $visited[] = $r;
} },
]); ]);
$expected = [ $expected = [
@ -819,17 +798,15 @@ class VisitorTest extends ValidatorTestCase
[ 'leave', 'Field', 1, null ], [ 'leave', 'Field', 1, null ],
[ 'leave', 'SelectionSet', 'selectionSet', 'OperationDefinition' ], [ 'leave', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
[ 'leave', 'OperationDefinition', 4, null ], [ 'leave', 'OperationDefinition', 4, null ],
[ 'leave', 'Document', null, null ] [ 'leave', 'Document', null, null ],
]; ];
$this->assertEquals($expected, $visited); $this->assertEquals($expected, $visited);
} }
// Describe: visitInParallel
// Note: nearly identical to the above test of the same test but using visitInParallel.
/** /**
* @it allows skipping a sub-tree * Describe: visitInParallel
* Note: nearly identical to the above test of the same test but using visitInParallel.
*/ */
public function testAllowsSkippingSubTree() public function testAllowsSkippingSubTree()
{ {
@ -840,7 +817,7 @@ class VisitorTest extends ValidatorTestCase
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = [ 'enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = [ 'enter', $node->kind, $node->value ?? null];
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
return Visitor::skipNode(); return Visitor::skipNode();
@ -849,9 +826,9 @@ class VisitorTest extends ValidatorTestCase
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
} },
] ],
])); ]));
$this->assertEquals([ $this->assertEquals([
@ -873,9 +850,6 @@ class VisitorTest extends ValidatorTestCase
], $visited); ], $visited);
} }
/**
* @it allows skipping different sub-trees
*/
public function testAllowsSkippingDifferentSubTrees() public function testAllowsSkippingDifferentSubTrees()
{ {
$visited = []; $visited = [];
@ -885,29 +859,29 @@ class VisitorTest extends ValidatorTestCase
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['no-a', 'enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['no-a', 'enter', $node->kind, $node->value ?? null];
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') {
return Visitor::skipNode(); return Visitor::skipNode();
} }
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = [ 'no-a', 'leave', $node->kind, isset($node->value) ? $node->value : null ]; $visited[] = [ 'no-a', 'leave', $node->kind, $node->value ?? null ];
} },
], ],
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['no-b', 'enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['no-b', 'enter', $node->kind, $node->value ?? null];
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
return Visitor::skipNode(); return Visitor::skipNode();
} }
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['no-b', 'leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['no-b', 'leave', $node->kind, $node->value ?? null];
} },
] ],
])); ]));
$this->assertEquals([ $this->assertEquals([
@ -948,9 +922,6 @@ class VisitorTest extends ValidatorTestCase
], $visited); ], $visited);
} }
/**
* @it allows early exit while visiting
*/
public function testAllowsEarlyExitWhileVisiting2() public function testAllowsEarlyExitWhileVisiting2()
{ {
$visited = []; $visited = [];
@ -959,7 +930,7 @@ class VisitorTest extends ValidatorTestCase
Visitor::visit($ast, Visitor::visitInParallel([ [ Visitor::visit($ast, Visitor::visitInParallel([ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$value = isset($node->value) ? $node->value : null; $value = $node->value ?? null;
$visited[] = ['enter', $node->kind, $value]; $visited[] = ['enter', $node->kind, $value];
if ($node->kind === 'Name' && $value === 'x') { if ($node->kind === 'Name' && $value === 'x') {
return Visitor::stop(); return Visitor::stop();
@ -967,9 +938,10 @@ class VisitorTest extends ValidatorTestCase
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
} },
] ])); ],
]));
$this->assertEquals([ $this->assertEquals([
[ 'enter', 'Document', null ], [ 'enter', 'Document', null ],
@ -984,13 +956,10 @@ class VisitorTest extends ValidatorTestCase
[ 'leave', 'Name', 'b' ], [ 'leave', 'Name', 'b' ],
[ 'enter', 'SelectionSet', null ], [ 'enter', 'SelectionSet', null ],
[ 'enter', 'Field', null ], [ 'enter', 'Field', null ],
[ 'enter', 'Name', 'x' ] [ 'enter', 'Name', 'x' ],
], $visited); ], $visited);
} }
/**
* @it allows early exit from different points
*/
public function testAllowsEarlyExitFromDifferentPoints() public function testAllowsEarlyExitFromDifferentPoints()
{ {
$visited = []; $visited = [];
@ -1000,7 +969,7 @@ class VisitorTest extends ValidatorTestCase
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$value = isset($node->value) ? $node->value : null; $value = $node->value ?? null;
$visited[] = ['break-a', 'enter', $node->kind, $value]; $visited[] = ['break-a', 'enter', $node->kind, $value];
if ($node->kind === 'Name' && $value === 'a') { if ($node->kind === 'Name' && $value === 'a') {
return Visitor::stop(); return Visitor::stop();
@ -1008,13 +977,13 @@ class VisitorTest extends ValidatorTestCase
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = [ 'break-a', 'leave', $node->kind, isset($node->value) ? $node->value : null ]; $visited[] = [ 'break-a', 'leave', $node->kind, $node->value ?? null ];
} },
], ],
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$value = isset($node->value) ? $node->value : null; $value = $node->value ?? null;
$visited[] = ['break-b', 'enter', $node->kind, $value]; $visited[] = ['break-b', 'enter', $node->kind, $value];
if ($node->kind === 'Name' && $value === 'b') { if ($node->kind === 'Name' && $value === 'b') {
return Visitor::stop(); return Visitor::stop();
@ -1022,8 +991,8 @@ class VisitorTest extends ValidatorTestCase
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['break-b', 'leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['break-b', 'leave', $node->kind, $node->value ?? null];
} },
], ],
])); ]));
@ -1047,13 +1016,10 @@ class VisitorTest extends ValidatorTestCase
[ 'break-b', 'leave', 'SelectionSet', null ], [ 'break-b', 'leave', 'SelectionSet', null ],
[ 'break-b', 'leave', 'Field', null ], [ 'break-b', 'leave', 'Field', null ],
[ 'break-b', 'enter', 'Field', null ], [ 'break-b', 'enter', 'Field', null ],
[ 'break-b', 'enter', 'Name', 'b' ] [ 'break-b', 'enter', 'Name', 'b' ],
], $visited); ], $visited);
} }
/**
* @it allows early exit while leaving
*/
public function testAllowsEarlyExitWhileLeaving2() public function testAllowsEarlyExitWhileLeaving2()
{ {
$visited = []; $visited = [];
@ -1062,17 +1028,18 @@ class VisitorTest extends ValidatorTestCase
Visitor::visit($ast, Visitor::visitInParallel([ [ Visitor::visit($ast, Visitor::visitInParallel([ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['enter', $node->kind, $node->value ?? null];
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$value = isset($node->value) ? $node->value : null; $value = $node->value ?? null;
$visited[] = ['leave', $node->kind, $value]; $visited[] = ['leave', $node->kind, $value];
if ($node->kind === 'Name' && $value === 'x') { if ($node->kind === 'Name' && $value === 'x') {
return Visitor::stop(); return Visitor::stop();
} }
} },
] ])); ],
]));
$this->assertEquals([ $this->assertEquals([
[ 'enter', 'Document', null ], [ 'enter', 'Document', null ],
@ -1088,13 +1055,10 @@ class VisitorTest extends ValidatorTestCase
[ 'enter', 'SelectionSet', null ], [ 'enter', 'SelectionSet', null ],
[ 'enter', 'Field', null ], [ 'enter', 'Field', null ],
[ 'enter', 'Name', 'x' ], [ 'enter', 'Name', 'x' ],
[ 'leave', 'Name', 'x' ] [ 'leave', 'Name', 'x' ],
], $visited); ], $visited);
} }
/**
* @it allows early exit from leaving different points
*/
public function testAllowsEarlyExitFromLeavingDifferentPoints() public function testAllowsEarlyExitFromLeavingDifferentPoints()
{ {
$visited = []; $visited = [];
@ -1104,28 +1068,28 @@ class VisitorTest extends ValidatorTestCase
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['break-a', 'enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['break-a', 'enter', $node->kind, $node->value ?? null];
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['break-a', 'leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['break-a', 'leave', $node->kind, $node->value ?? null];
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') {
return Visitor::stop(); return Visitor::stop();
} }
} },
], ],
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['break-b', 'enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['break-b', 'enter', $node->kind, $node->value ?? null];
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['break-b', 'leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['break-b', 'leave', $node->kind, $node->value ?? null];
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
return Visitor::stop(); return Visitor::stop();
} }
} },
], ],
])); ]));
@ -1165,13 +1129,10 @@ class VisitorTest extends ValidatorTestCase
[ 'break-b', 'leave', 'Name', 'x' ], [ 'break-b', 'leave', 'Name', 'x' ],
[ 'break-b', 'leave', 'Field', null ], [ 'break-b', 'leave', 'Field', null ],
[ 'break-b', 'leave', 'SelectionSet', null ], [ 'break-b', 'leave', 'SelectionSet', null ],
[ 'break-b', 'leave', 'Field', null ] [ 'break-b', 'leave', 'Field', null ],
], $visited); ], $visited);
} }
/**
* @it allows for editing on enter
*/
public function testAllowsForEditingOnEnter2() public function testAllowsForEditingOnEnter2()
{ {
$visited = []; $visited = [];
@ -1184,17 +1145,17 @@ class VisitorTest extends ValidatorTestCase
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
return Visitor::removeNode(); return Visitor::removeNode();
} }
} },
], ],
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['enter', $node->kind, $node->value ?? null];
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args(), true); $this->checkVisitorFnArgs($ast, func_get_args(), true);
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
} },
], ],
])); ]));
@ -1232,13 +1193,10 @@ class VisitorTest extends ValidatorTestCase
['leave', 'Field', null], ['leave', 'Field', null],
['leave', 'SelectionSet', null], ['leave', 'SelectionSet', null],
['leave', 'OperationDefinition', null], ['leave', 'OperationDefinition', null],
['leave', 'Document', null] ['leave', 'Document', null],
], $visited); ], $visited);
} }
/**
* @it allows for editing on leave
*/
public function testAllowsForEditingOnLeave2() public function testAllowsForEditingOnLeave2()
{ {
$visited = []; $visited = [];
@ -1251,17 +1209,17 @@ class VisitorTest extends ValidatorTestCase
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
return Visitor::removeNode(); return Visitor::removeNode();
} }
} },
], ],
[ [
'enter' => function ($node) use (&$visited, $ast) { 'enter' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args()); $this->checkVisitorFnArgs($ast, func_get_args());
$visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['enter', $node->kind, $node->value ?? null];
}, },
'leave' => function ($node) use (&$visited, $ast) { 'leave' => function ($node) use (&$visited, $ast) {
$this->checkVisitorFnArgs($ast, func_get_args(), true); $this->checkVisitorFnArgs($ast, func_get_args(), true);
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $visited[] = ['leave', $node->kind, $node->value ?? null];
} },
], ],
])); ]));
@ -1305,14 +1263,13 @@ class VisitorTest extends ValidatorTestCase
['leave', 'Field', null], ['leave', 'Field', null],
['leave', 'SelectionSet', null], ['leave', 'SelectionSet', null],
['leave', 'OperationDefinition', null], ['leave', 'OperationDefinition', null],
['leave', 'Document', null] ['leave', 'Document', null],
], $visited); ], $visited);
} }
// Describe: visitWithTypeInfo
/** /**
* @it maintains type info during visit * Describe: visitWithTypeInfo
*/ */
public function testMaintainsTypeInfoDuringVisit() public function testMaintainsTypeInfoDuringVisit()
{ {
@ -1333,7 +1290,7 @@ class VisitorTest extends ValidatorTestCase
$node->kind === 'Name' ? $node->value : null, $node->kind === 'Name' ? $node->value : null,
$parentType ? (string) $parentType : null, $parentType ? (string) $parentType : null,
$type ? (string) $type : null, $type ? (string) $type : null,
$inputType ? (string)$inputType : null $inputType ? (string) $inputType : null,
]; ];
}, },
'leave' => function ($node) use ($typeInfo, &$visited, $ast) { 'leave' => function ($node) use ($typeInfo, &$visited, $ast) {
@ -1347,9 +1304,9 @@ class VisitorTest extends ValidatorTestCase
$node->kind === 'Name' ? $node->value : null, $node->kind === 'Name' ? $node->value : null,
$parentType ? (string) $parentType : null, $parentType ? (string) $parentType : null,
$type ? (string) $type : null, $type ? (string) $type : null,
$inputType ? (string)$inputType : null $inputType ? (string) $inputType : null,
]; ];
} },
])); ]));
$this->assertEquals([ $this->assertEquals([
@ -1392,13 +1349,10 @@ class VisitorTest extends ValidatorTestCase
['leave', 'Field', null, 'QueryRoot', 'Human', null], ['leave', 'Field', null, 'QueryRoot', 'Human', null],
['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], ['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null],
['leave', 'OperationDefinition', null, null, 'QueryRoot', null], ['leave', 'OperationDefinition', null, null, 'QueryRoot', null],
['leave', 'Document', null, null, null, null] ['leave', 'Document', null, null, null, null],
], $visited); ], $visited);
} }
/**
* @it maintains type info during edit
*/
public function testMaintainsTypeInfoDuringEdit() public function testMaintainsTypeInfoDuringEdit()
{ {
$visited = []; $visited = [];
@ -1419,12 +1373,11 @@ class VisitorTest extends ValidatorTestCase
$node->kind === 'Name' ? $node->value : null, $node->kind === 'Name' ? $node->value : null,
$parentType ? (string) $parentType : null, $parentType ? (string) $parentType : null,
$type ? (string) $type : null, $type ? (string) $type : null,
$inputType ? (string)$inputType : null $inputType ? (string) $inputType : null,
]; ];
// Make a query valid by adding missing selection sets. // Make a query valid by adding missing selection sets.
if ( if ($node->kind === 'Field' &&
$node->kind === 'Field' &&
! $node->selectionSet && ! $node->selectionSet &&
Type::isCompositeType(Type::getNamedType($type)) Type::isCompositeType(Type::getNamedType($type))
) { ) {
@ -1435,12 +1388,11 @@ class VisitorTest extends ValidatorTestCase
'directives' => $node->directives, 'directives' => $node->directives,
'selectionSet' => new SelectionSetNode([ 'selectionSet' => new SelectionSetNode([
'kind' => 'SelectionSet', 'kind' => 'SelectionSet',
'selections' => [ 'selections' => [new FieldNode([
new FieldNode([ 'name' => new NameNode(['value' => '__typename']),
'name' => new NameNode(['value' => '__typename']) ]),
]) ],
] ]),
])
]); ]);
} }
}, },
@ -1455,9 +1407,9 @@ class VisitorTest extends ValidatorTestCase
$node->kind === 'Name' ? $node->value : null, $node->kind === 'Name' ? $node->value : null,
$parentType ? (string) $parentType : null, $parentType ? (string) $parentType : null,
$type ? (string) $type : null, $type ? (string) $type : null,
$inputType ? (string)$inputType : null $inputType ? (string) $inputType : null,
]; ];
} },
])); ]));
$this->assertEquals(Printer::doPrint(Parser::parse( $this->assertEquals(Printer::doPrint(Parser::parse(
@ -1510,7 +1462,7 @@ class VisitorTest extends ValidatorTestCase
['leave', 'Field', null, 'QueryRoot', 'Alien', null], ['leave', 'Field', null, 'QueryRoot', 'Alien', null],
['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], ['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null],
['leave', 'OperationDefinition', null, null, 'QueryRoot', null], ['leave', 'OperationDefinition', null, null, 'QueryRoot', null],
['leave', 'Document', null, null, null, null] ['leave', 'Document', null, null, null, null],
], $visited); ], $visited);
} }
} }