diff --git a/src/Error/Error.php b/src/Error/Error.php index 9491d24ac..e4ad9ac99 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -38,7 +38,11 @@ class Error extends Exception implements JsonSerializable, ClientAware const CATEGORY_GRAPHQL = 'graphql'; const CATEGORY_INTERNAL = 'internal'; - /** @var SourceLocation[] */ + /** + * Lazily initialized. + * + * @var SourceLocation[] + */ private $locations; /** @@ -203,10 +207,7 @@ public function getCategory() return $this->category; } - /** - * @return Source|null - */ - public function getSource() + public function getSource() : ?Source { if ($this->source === null) { if (isset($this->nodes[0]) && $this->nodes[0]->loc !== null) { @@ -258,9 +259,9 @@ static function ($p) : bool { * * @api */ - public function getLocations() + public function getLocations() : array { - if ($this->locations === null) { + if (! isset($this->locations)) { $positions = $this->getPositions(); $source = $this->getSource(); $nodes = $this->nodes; diff --git a/src/Type/Definition/EnumType.php b/src/Type/Definition/EnumType.php index 93f6283d8..9451ec74a 100644 --- a/src/Type/Definition/EnumType.php +++ b/src/Type/Definition/EnumType.php @@ -24,11 +24,17 @@ class EnumType extends Type implements InputType, OutputType, LeafType, Nullable /** @var EnumTypeDefinitionNode|null */ public $astNode; - /** @var EnumValueDefinition[] */ + /** + * Lazily initialized. + * + * @var EnumValueDefinition[] + */ private $values; /** - * Actually a MixedStore, PHPStan won't let us type it that way + * Lazily initialized. + * + * Actually a MixedStore, PHPStan won't let us type it that way. * * @var MixedStore */ @@ -88,9 +94,9 @@ private function getNameLookup() : ArrayObject /** * @return EnumValueDefinition[] */ - public function getValues() + public function getValues() : array { - if ($this->values === null) { + if (! isset($this->values)) { $this->values = []; $config = $this->config; @@ -145,7 +151,7 @@ public function serialize($value) */ private function getValueLookup() : MixedStore { - if ($this->valueLookup === null) { + if (! isset($this->valueLookup)) { $this->valueLookup = new MixedStore(); foreach ($this->getValues() as $valueName => $value) { diff --git a/src/Type/Definition/FieldDefinition.php b/src/Type/Definition/FieldDefinition.php index 8a3e4bb59..c0fd318a6 100644 --- a/src/Type/Definition/FieldDefinition.php +++ b/src/Type/Definition/FieldDefinition.php @@ -84,7 +84,12 @@ protected function __construct(array $config) $this->complexityFn = $config['complexity'] ?? self::DEFAULT_COMPLEXITY_FN; } - public static function defineFieldMap(Type $type, $fields) + /** + * @param (callable():mixed[])|mixed[] $fields + * + * @return array + */ + public static function defineFieldMap(Type $type, $fields) : array { if (is_callable($fields)) { $fields = $fields(); diff --git a/src/Type/Definition/InputObjectType.php b/src/Type/Definition/InputObjectType.php index 7eaa4d1f2..455e2efc8 100644 --- a/src/Type/Definition/InputObjectType.php +++ b/src/Type/Definition/InputObjectType.php @@ -4,7 +4,6 @@ namespace GraphQL\Type\Definition; -use Exception; use GraphQL\Error\InvariantViolation; use GraphQL\Language\AST\InputObjectTypeDefinitionNode; use GraphQL\Language\AST\InputObjectTypeExtensionNode; @@ -21,7 +20,11 @@ class InputObjectType extends Type implements InputType, NullableType, NamedType /** @var InputObjectTypeDefinitionNode|null */ public $astNode; - /** @var InputObjectField[] */ + /** + * Lazily initialized. + * + * @var InputObjectField[] + */ private $fields; /** @var InputObjectTypeExtensionNode[] */ @@ -46,16 +49,12 @@ public function __construct(array $config) } /** - * @param string $name - * - * @return InputObjectField - * - * @throws Exception + * @throws InvariantViolation */ - public function getField($name) + public function getField(string $name) : InputObjectField { - if ($this->fields === null) { - $this->getFields(); + if (! isset($this->fields)) { + $this->initializeFields(); } Utils::invariant(isset($this->fields[$name]), "Field '%s' is not defined for type '%s'", $name, $this->name); @@ -65,32 +64,36 @@ public function getField($name) /** * @return InputObjectField[] */ - public function getFields() + public function getFields() : array { - if ($this->fields === null) { - $this->fields = []; + if (! isset($this->fields)) { + $this->initializeFields(); + } - $fields = $this->config['fields'] ?? []; - if (is_callable($fields)) { - $fields = $fields(); - } + return $this->fields; + } - if (! is_array($fields)) { - throw new InvariantViolation( - sprintf('%s fields must be an array or a callable which returns such an array.', $this->name) - ); - } + protected function initializeFields() : void + { + $this->fields = []; + $fields = $this->config['fields'] ?? []; + if (is_callable($fields)) { + $fields = $fields(); + } - foreach ($fields as $name => $field) { - if ($field instanceof Type) { - $field = ['type' => $field]; - } - $field = new InputObjectField($field + ['name' => $name]); - $this->fields[$field->name] = $field; - } + if (! is_array($fields)) { + throw new InvariantViolation( + sprintf('%s fields must be an array or a callable which returns such an array.', $this->name) + ); } - return $this->fields; + foreach ($fields as $name => $field) { + if ($field instanceof Type) { + $field = ['type' => $field]; + } + $field = new InputObjectField($field + ['name' => $name]); + $this->fields[$field->name] = $field; + } } /** @@ -99,7 +102,7 @@ public function getFields() * * @throws InvariantViolation */ - public function assertValid() + public function assertValid() : void { parent::assertValid(); diff --git a/src/Type/Definition/InterfaceType.php b/src/Type/Definition/InterfaceType.php index c13d7fb05..e01987653 100644 --- a/src/Type/Definition/InterfaceType.php +++ b/src/Type/Definition/InterfaceType.php @@ -20,7 +20,11 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT /** @var InterfaceTypeExtensionNode[] */ public $extensionASTNodes; - /** @var FieldDefinition[] */ + /** + * Lazily initialized. + * + * @var FieldDefinition[] + */ private $fields; /** @@ -44,9 +48,11 @@ public function __construct(array $config) /** * @param mixed $type * - * @return self + * @return $this + * + * @throws InvariantViolation */ - public static function assertInterfaceType($type) + public static function assertInterfaceType($type) : self { Utils::invariant( $type instanceof self, @@ -56,30 +62,20 @@ public static function assertInterfaceType($type) return $type; } - /** - * @param string $name - * - * @return FieldDefinition - */ - public function getField($name) + public function getField(string $name) : FieldDefinition { - if ($this->fields === null) { - $this->getFields(); + if (! isset($this->fields)) { + $this->initializeFields(); } Utils::invariant(isset($this->fields[$name]), 'Field "%s" is not defined for type "%s"', $name, $this->name); return $this->fields[$name]; } - /** - * @param string $name - * - * @return bool - */ - public function hasField($name) + public function hasField(string $name) : bool { - if ($this->fields === null) { - $this->getFields(); + if (! isset($this->fields)) { + $this->initializeFields(); } return isset($this->fields[$name]); @@ -88,21 +84,26 @@ public function hasField($name) /** * @return FieldDefinition[] */ - public function getFields() + public function getFields() : array { - if ($this->fields === null) { - $fields = $this->config['fields'] ?? []; - $this->fields = FieldDefinition::defineFieldMap($this, $fields); + if (! isset($this->fields)) { + $this->initializeFields(); } return $this->fields; } + protected function initializeFields() : void + { + $fields = $this->config['fields'] ?? []; + $this->fields = FieldDefinition::defineFieldMap($this, $fields); + } + /** * Resolves concrete ObjectType for given object value * - * @param object $objectValue - * @param mixed[] $context + * @param object $objectValue + * @param mixed $context * * @return Type|null */ @@ -120,7 +121,7 @@ public function resolveType($objectValue, $context, ResolveInfo $info) /** * @throws InvariantViolation */ - public function assertValid() + public function assertValid() : void { parent::assertValid(); diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index e9d069cd1..199d90df1 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -4,7 +4,6 @@ namespace GraphQL\Type\Definition; -use Exception; use GraphQL\Deferred; use GraphQL\Error\InvariantViolation; use GraphQL\Language\AST\ObjectTypeDefinitionNode; @@ -67,13 +66,25 @@ class ObjectType extends Type implements OutputType, CompositeType, NullableType /** @var ?callable */ public $resolveFieldFn; - /** @var FieldDefinition[] */ + /** + * Lazily initialized. + * + * @var FieldDefinition[] + */ private $fields; - /** @var InterfaceType[] */ + /** + * Lazily initialized. + * + * @var InterfaceType[] + */ private $interfaces; - /** @var InterfaceType[]|null */ + /** + * Lazily initialized. + * + * @var InterfaceType[] + */ private $interfaceMap; /** @@ -98,9 +109,11 @@ public function __construct(array $config) /** * @param mixed $type * - * @return self + * @return $this + * + * @throws InvariantViolation */ - public static function assertObjectType($type) + public static function assertObjectType($type) : self { Utils::invariant( $type instanceof self, @@ -111,31 +124,22 @@ public static function assertObjectType($type) } /** - * @param string $name - * - * @return FieldDefinition - * - * @throws Exception + * @throws InvariantViolation */ - public function getField($name) + public function getField(string $name) : FieldDefinition { - if ($this->fields === null) { - $this->getFields(); + if (! isset($this->fields)) { + $this->initializeFields(); } Utils::invariant(isset($this->fields[$name]), 'Field "%s" is not defined for type "%s"', $name, $this->name); return $this->fields[$name]; } - /** - * @param string $name - * - * @return bool - */ - public function hasField($name) + public function hasField(string $name) : bool { - if ($this->fields === null) { - $this->getFields(); + if (! isset($this->fields)) { + $this->initializeFields(); } return isset($this->fields[$name]); @@ -146,47 +150,41 @@ public function hasField($name) * * @throws InvariantViolation */ - public function getFields() + public function getFields() : array { - if ($this->fields === null) { - $fields = $this->config['fields'] ?? []; - $this->fields = FieldDefinition::defineFieldMap($this, $fields); + if (! isset($this->fields)) { + $this->initializeFields(); } return $this->fields; } - /** - * @param InterfaceType $iface - * - * @return bool - */ - public function implementsInterface($iface) + protected function initializeFields() : void { - $map = $this->getInterfaceMap(); - - return isset($map[$iface->name]); + $fields = $this->config['fields'] ?? []; + $this->fields = FieldDefinition::defineFieldMap($this, $fields); } - private function getInterfaceMap() + public function implementsInterface(InterfaceType $interfaceType) : bool { - if ($this->interfaceMap === null) { + if (! isset($this->interfaceMap)) { $this->interfaceMap = []; foreach ($this->getInterfaces() as $interface) { + /** @var Type&InterfaceType $interface */ $interface = Schema::resolveType($interface); $this->interfaceMap[$interface->name] = $interface; } } - return $this->interfaceMap; + return isset($this->interfaceMap[$interfaceType->name]); } /** * @return InterfaceType[] */ - public function getInterfaces() + public function getInterfaces() : array { - if ($this->interfaces === null) { + if (! isset($this->interfaces)) { $interfaces = $this->config['interfaces'] ?? []; if (is_callable($interfaces)) { $interfaces = $interfaces(); @@ -208,8 +206,8 @@ public function getInterfaces() } /** - * @param mixed $value - * @param mixed[]|null $context + * @param mixed $value + * @param mixed $context * * @return bool|Deferred|null */ @@ -230,7 +228,7 @@ public function isTypeOf($value, $context, ResolveInfo $info) * * @throws InvariantViolation */ - public function assertValid() + public function assertValid() : void { parent::assertValid(); diff --git a/src/Type/Definition/ResolveInfo.php b/src/Type/Definition/ResolveInfo.php index 6b6dba6d7..6c118c234 100644 --- a/src/Type/Definition/ResolveInfo.php +++ b/src/Type/Definition/ResolveInfo.php @@ -108,7 +108,11 @@ class ResolveInfo */ public $variableValues = []; - /** @var QueryPlan */ + /** + * Lazily initialized. + * + * @var QueryPlan + */ private $queryPlan; /** @@ -203,7 +207,7 @@ public function getFieldSelection($depth = 0) */ public function lookAhead(array $options = []) : QueryPlan { - if ($this->queryPlan === null) { + if (! isset($this->queryPlan)) { $this->queryPlan = new QueryPlan( $this->parentType, $this->schema, diff --git a/src/Type/Definition/UnionType.php b/src/Type/Definition/UnionType.php index 51d6cb623..6301e60a7 100644 --- a/src/Type/Definition/UnionType.php +++ b/src/Type/Definition/UnionType.php @@ -19,10 +19,18 @@ class UnionType extends Type implements AbstractType, OutputType, CompositeType, /** @var UnionTypeDefinitionNode */ public $astNode; - /** @var ObjectType[] */ + /** + * Lazily initialized. + * + * @var ObjectType[] + */ private $types; - /** @var array|null */ + /** + * Lazily initialized. + * + * @var array + */ private $possibleTypeNames; /** @var UnionTypeExtensionNode[] */ @@ -57,7 +65,7 @@ public function isPossibleType(Type $type) : bool return false; } - if ($this->possibleTypeNames === null) { + if (! isset($this->possibleTypeNames)) { $this->possibleTypeNames = []; foreach ($this->getTypes() as $possibleType) { $this->possibleTypeNames[$possibleType->name] = true; @@ -69,10 +77,12 @@ public function isPossibleType(Type $type) : bool /** * @return ObjectType[] + * + * @throws InvariantViolation */ - public function getTypes() + public function getTypes() : array { - if ($this->types === null) { + if (! isset($this->types)) { $types = $this->config['types'] ?? null; if (is_callable($types)) { $types = $types(); @@ -120,7 +130,7 @@ public function resolveType($objectValue, $context, ResolveInfo $info) /** * @throws InvariantViolation */ - public function assertValid() + public function assertValid() : void { parent::assertValid(); diff --git a/src/Type/Schema.php b/src/Type/Schema.php index 173f340cf..3f0f49bfc 100644 --- a/src/Type/Schema.php +++ b/src/Type/Schema.php @@ -57,7 +57,11 @@ class Schema */ private $resolvedTypes = []; - /** @var array> */ + /** + * Lazily initialized. + * + * @var array> + */ private $possibleTypeMap; /** @@ -402,7 +406,7 @@ public function getPossibleTypes(Type $abstractType) : array /** * @return array> */ - private function getPossibleTypeMap() + private function getPossibleTypeMap() : array { if (! isset($this->possibleTypeMap)) { $this->possibleTypeMap = [];