From e664c4455e2b4bafef9cf4e83633a15193f0fbb9 Mon Sep 17 00:00:00 2001
From: Simon Podlipsky
* An offset to check for.
*
@@ -122,10 +128,13 @@ class MixedStore implements \ArrayAccess /** * Offset to retrieve + * * @link http://php.net/manual/en/arrayaccess.offsetget.php + * * @param mixed $offset
* The offset to retrieve. *
+ * * @return mixed Can return all value types. */ public function offsetGet($offset) @@ -165,13 +174,16 @@ class MixedStore implements \ArrayAccess /** * Offset to set + * * @link http://php.net/manual/en/arrayaccess.offsetset.php + * * @param mixed $offset* The offset to assign the value to. *
* @param mixed $value* The value to set. *
+ * * @return void */ public function offsetSet($offset, $value) @@ -195,16 +207,19 @@ class MixedStore implements \ArrayAccess $this->nullValue = $value; $this->nullValueIsSet = true; } else { - throw new \InvalidArgumentException('Unexpected offset type: ' . Utils::printSafe($offset)); + throw new InvalidArgumentException('Unexpected offset type: ' . Utils::printSafe($offset)); } } /** * Offset to unset + * * @link http://php.net/manual/en/arrayaccess.offsetunset.php + * * @param mixed $offset* The offset to unset. *
+ * * @return void */ public function offsetUnset($offset) diff --git a/src/Utils/PairSet.php b/src/Utils/PairSet.php index e910a4b..fe3a514 100644 --- a/src/Utils/PairSet.php +++ b/src/Utils/PairSet.php @@ -22,12 +22,13 @@ class PairSet * @param string $a * @param string $b * @param bool $areMutuallyExclusive + * * @return bool */ public function has($a, $b, $areMutuallyExclusive) { $first = $this->data[$a] ?? null; - $result = ($first && isset($first[$b])) ? $first[$b] : null; + $result = $first && isset($first[$b]) ? $first[$b] : null; if ($result === null) { return false; } diff --git a/src/Utils/SchemaPrinter.php b/src/Utils/SchemaPrinter.php index aba3e45..023ecb9 100644 --- a/src/Utils/SchemaPrinter.php +++ b/src/Utils/SchemaPrinter.php @@ -42,17 +42,19 @@ class SchemaPrinter * * - commentDescriptions: * Provide true to use preceding comments as the description. - * @api + * * @param bool[] $options + * + * @api */ public static function doPrint(Schema $schema, array $options = []) : string { return self::printFilteredSchema( $schema, - function ($type) { + static function ($type) { return ! Directive::isSpecifiedDirective($type); }, - function ($type) { + static function ($type) { return ! Type::isBuiltInType($type); }, $options @@ -66,7 +68,7 @@ class SchemaPrinter { $directives = array_filter( $schema->getDirectives(), - function ($directive) use ($directiveFilter) { + static function ($directive) use ($directiveFilter) { return $directiveFilter($directive); } ); @@ -83,13 +85,13 @@ class SchemaPrinter array_merge( [self::printSchemaDefinition($schema)], array_map( - function ($directive) use ($options) { + static function ($directive) use ($options) { return self::printDirective($directive, $options); }, $directives ), array_map( - function ($type) use ($options) { + static function ($type) use ($options) { return self::printType($type, $options); }, $types @@ -175,7 +177,7 @@ class SchemaPrinter return self::printDescriptionWithComments($lines, $indentation, $firstInBlock); } - $description = ($indentation && ! $firstInBlock) + $description = $indentation && ! $firstInBlock ? "\n" . $indentation . '"""' : $indentation . '"""'; @@ -274,7 +276,7 @@ class SchemaPrinter // If every arg does not have a description, print them on one line. if (Utils::every( $args, - function ($arg) { + static function ($arg) { return empty($arg->description); } )) { @@ -286,7 +288,7 @@ class SchemaPrinter implode( "\n", array_map( - function ($arg, $i) use ($indentation, $options) { + static function ($arg, $i) use ($indentation, $options) { return self::printDescription($options, $arg, ' ' . $indentation, ! $i) . ' ' . $indentation . self::printInputValue($arg); }, @@ -358,7 +360,7 @@ class SchemaPrinter ' implements ' . implode( ' & ', array_map( - function ($i) { + static function ($i) { return $i->name; }, $interfaces @@ -379,7 +381,7 @@ class SchemaPrinter return implode( "\n", array_map( - function ($f, $i) use ($options) { + static function ($f, $i) use ($options) { return self::printDescription($options, $f, ' ', ! $i) . ' ' . $f->name . self::printArgs($options, $f->args, ' ') . ': ' . (string) $f->getType() . self::printDeprecated($f); @@ -439,7 +441,7 @@ class SchemaPrinter return implode( "\n", array_map( - function ($value, $i) use ($options) { + static function ($value, $i) use ($options) { return self::printDescription($options, $value, ' ', ! $i) . ' ' . $value->name . self::printDeprecated($value); }, @@ -463,7 +465,7 @@ class SchemaPrinter implode( "\n", array_map( - function ($f, $i) use ($options) { + static function ($f, $i) use ($options) { return self::printDescription($options, $f, ' ', ! $i) . ' ' . self::printInputValue($f); }, $fields, @@ -474,8 +476,9 @@ class SchemaPrinter } /** - * @api * @param bool[] $options + * + * @api */ public static function printIntrospectionSchema(Schema $schema, array $options = []) : string { diff --git a/src/Utils/TypeComparators.php b/src/Utils/TypeComparators.php index c66b266..24e4b00 100644 --- a/src/Utils/TypeComparators.php +++ b/src/Utils/TypeComparators.php @@ -46,6 +46,7 @@ class TypeComparators * * @param AbstractType $maybeSubType * @param AbstractType $superType + * * @return bool */ public static function isTypeSubTypeOf(Schema $schema, $maybeSubType, $superType) diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index e27bf5a..76b8019 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -64,7 +64,6 @@ class TypeInfo private $enumValue; /** - * * @param Type|null $initialType */ public function __construct(Schema $schema, $initialType = null) @@ -128,6 +127,7 @@ class TypeInfo * * @param Type|null $type * @param Type[]|null $typeMap + * * @return Type[]|null */ public static function extractTypes($type, ?array $typeMap = null) @@ -175,7 +175,7 @@ class TypeInfo foreach ((array) $type->getFields() as $fieldName => $field) { if (! empty($field->args)) { $fieldArgTypes = array_map( - function (FieldArgument $arg) { + static function (FieldArgument $arg) { return $arg->getType(); }, $field->args @@ -200,6 +200,7 @@ class TypeInfo /** * @param Type[] $typeMap + * * @return Type[] */ public static function extractTypesFromDirectives(Directive $directive, array $typeMap = []) @@ -304,7 +305,7 @@ class TypeInfo if ($fieldOrDirective) { $argDef = Utils::find( $fieldOrDirective->args, - function ($arg) use ($node) { + static function ($arg) use ($node) { return $arg->name === $node->name->value; } ); @@ -406,7 +407,9 @@ class TypeInfo /** * @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode + * * @return Type|null + * * @throws InvariantViolation */ public static function typeFromAST(Schema $schema, $inputTypeNode) diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php index e1cfcef..be0bdda 100644 --- a/src/Utils/Utils.php +++ b/src/Utils/Utils.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace GraphQL\Utils; +use ErrorException; +use Exception; use GraphQL\Error\Error; use GraphQL\Error\InvariantViolation; use GraphQL\Error\Warning; @@ -11,6 +13,8 @@ use GraphQL\Language\AST\Node; use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\WrappingType; use InvalidArgumentException; +use LogicException; +use stdClass; use Traversable; use function array_keys; use function array_map; @@ -55,13 +59,14 @@ class Utils { static $undefined; - return $undefined ?: $undefined = new \stdClass(); + return $undefined ?: $undefined = new stdClass(); } /** * Check if the value is invalid * * @param mixed $value + * * @return bool */ public static function isInvalid($value) @@ -100,12 +105,13 @@ class Utils /** * @param mixed|Traversable $traversable + * * @return mixed|null */ public static function find($traversable, callable $predicate) { self::invariant( - is_array($traversable) || $traversable instanceof \Traversable, + is_array($traversable) || $traversable instanceof Traversable, __METHOD__ . ' expects array or Traversable' ); @@ -120,13 +126,15 @@ class Utils /** * @param mixed|Traversable $traversable + * * @return mixed[] - * @throws \Exception + * + * @throws Exception */ public static function filter($traversable, callable $predicate) { self::invariant( - is_array($traversable) || $traversable instanceof \Traversable, + is_array($traversable) || $traversable instanceof Traversable, __METHOD__ . ' expects array or Traversable' ); @@ -147,14 +155,16 @@ class Utils } /** - * @param mixed|\Traversable $traversable + * @param mixed|Traversable $traversable + * * @return int[][] - * @throws \Exception + * + * @throws Exception */ public static function map($traversable, callable $fn) { self::invariant( - is_array($traversable) || $traversable instanceof \Traversable, + is_array($traversable) || $traversable instanceof Traversable, __METHOD__ . ' expects array or Traversable' ); @@ -168,20 +178,22 @@ class Utils /** * @param mixed|Traversable $traversable + * * @return mixed[] - * @throws \Exception + * + * @throws Exception */ public static function mapKeyValue($traversable, callable $fn) { self::invariant( - is_array($traversable) || $traversable instanceof \Traversable, + is_array($traversable) || $traversable instanceof Traversable, __METHOD__ . ' expects array or Traversable' ); $map = []; foreach ($traversable as $key => $value) { - list($newKey, $newValue) = $fn($value, $key); - $map[$newKey] = $newValue; + [$newKey, $newValue] = $fn($value, $key); + $map[$newKey] = $newValue; } return $map; @@ -189,13 +201,15 @@ class Utils /** * @param mixed|Traversable $traversable + * * @return mixed[] - * @throws \Exception + * + * @throws Exception */ public static function keyMap($traversable, callable $keyFn) { self::invariant( - is_array($traversable) || $traversable instanceof \Traversable, + is_array($traversable) || $traversable instanceof Traversable, __METHOD__ . ' expects array or Traversable' ); @@ -215,7 +229,7 @@ class Utils public static function each($traversable, callable $fn) { self::invariant( - is_array($traversable) || $traversable instanceof \Traversable, + is_array($traversable) || $traversable instanceof Traversable, __METHOD__ . ' expects array or Traversable' ); @@ -237,12 +251,13 @@ class Utils * $keyFn is also allowed to return array of keys. Then value will be added to all arrays with given keys * * @param mixed[]|Traversable $traversable + * * @return mixed[] */ public static function groupBy($traversable, callable $keyFn) { self::invariant( - is_array($traversable) || $traversable instanceof \Traversable, + is_array($traversable) || $traversable instanceof Traversable, __METHOD__ . ' expects array or Traversable' ); @@ -259,6 +274,7 @@ class Utils /** * @param mixed[]|Traversable $traversable + * * @return mixed[][] */ public static function keyValMap($traversable, callable $keyFn, callable $valFn) @@ -273,6 +289,7 @@ class Utils /** * @param mixed[] $traversable + * * @return bool */ public static function every($traversable, callable $predicate) @@ -305,6 +322,7 @@ class Utils /** * @param Type|mixed $var + * * @return string */ public static function getVariableType($var) @@ -323,11 +341,12 @@ class Utils /** * @param mixed $var + * * @return string */ public static function printSafeJson($var) { - if ($var instanceof \stdClass) { + if ($var instanceof stdClass) { $var = (array) $var; } if (is_array($var)) { @@ -357,6 +376,7 @@ class Utils /** * @param Type|mixed $var + * * @return string */ public static function printSafe($var) @@ -401,6 +421,7 @@ class Utils * * @param string $ord * @param string $encoding + * * @return string */ public static function chr($ord, $encoding = 'UTF-8') @@ -420,6 +441,7 @@ class Utils * * @param string $char * @param string $encoding + * * @return mixed */ public static function ord($char, $encoding = 'UTF-8') @@ -442,6 +464,7 @@ class Utils * * @param string $string * @param int $position + * * @return mixed */ public static function charCodeAt($string, $position) @@ -453,6 +476,7 @@ class Utils /** * @param int|null $code + * * @return string */ public static function printCharCode($code) @@ -472,6 +496,7 @@ class Utils * Upholds the spec rules about naming. * * @param string $name + * * @throws Error */ public static function assertValidName($name) @@ -487,6 +512,7 @@ class Utils * * @param string $name * @param Node|null $node + * * @return Error|null */ public static function isValidNameError($name, $node = null) @@ -512,18 +538,19 @@ class Utils } /** - * Wraps original closure with PHP error handling (using set_error_handler). - * Resulting closure will collect all PHP errors that occur during the call in $errors array. + * Wraps original callable with PHP error handling (using set_error_handler). + * Resulting callable will collect all PHP errors that occur during the call in $errors array. * - * @param \ErrorException[] $errors - * @return \Closure + * @param ErrorException[] $errors + * + * @return callable */ public static function withErrorHandling(callable $fn, array &$errors) { - return function () use ($fn, &$errors) { + return static function () use ($fn, &$errors) { // Catch custom errors (to report them in query results) - set_error_handler(function ($severity, $message, $file, $line) use (&$errors) { - $errors[] = new \ErrorException($message, 0, $severity, $file, $line); + set_error_handler(static function ($severity, $message, $file, $line) use (&$errors) { + $errors[] = new ErrorException($message, 0, $severity, $file, $line); }); try { @@ -536,12 +563,13 @@ class Utils /** * @param string[] $items + * * @return string */ public static function quotedOrList(array $items) { $items = array_map( - function ($item) { + static function ($item) { return sprintf('"%s"', $item); }, $items @@ -552,12 +580,13 @@ class Utils /** * @param string[] $items + * * @return string */ public static function orList(array $items) { if (count($items) === 0) { - throw new \LogicException('items must not need to be empty.'); + throw new LogicException('items must not need to be empty.'); } $selected = array_slice($items, 0, 5); $selectedLength = count($selected); @@ -569,7 +598,7 @@ class Utils return array_reduce( range(1, $selectedLength - 1), - function ($list, $index) use ($selected, $selectedLength) { + static function ($list, $index) use ($selected, $selectedLength) { return $list . ($selectedLength > 2 ? ', ' : ' ') . ($index === $selectedLength - 1 ? 'or ' : '') . @@ -586,8 +615,10 @@ class Utils * Includes a custom alteration from Damerau-Levenshtein to treat case changes * as a single edit which helps identify mis-cased values with an edit distance * of 1 + * * @param string $input * @param string[] $options + * * @return string[] */ public static function suggestionList($input, array $options) diff --git a/src/Utils/Value.php b/src/Utils/Value.php index 9541291..2bb053c 100644 --- a/src/Utils/Value.php +++ b/src/Utils/Value.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace GraphQL\Utils; +use Exception; use GraphQL\Error\Error; use GraphQL\Language\AST\Node; use GraphQL\Type\Definition\EnumType; @@ -12,6 +13,8 @@ use GraphQL\Type\Definition\InputType; use GraphQL\Type\Definition\ListOfType; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ScalarType; +use Throwable; +use Traversable; use function array_key_exists; use function array_keys; use function array_map; @@ -61,7 +64,7 @@ class Value // the original error. try { return self::ofValue($type->parseValue($value)); - } catch (\Exception $error) { + } catch (Exception $error) { return self::ofErrors([ self::coercionError( sprintf('Expected type %s', $type->name), @@ -71,7 +74,7 @@ class Value $error ), ]); - } catch (\Throwable $error) { + } catch (Throwable $error) { return self::ofErrors([ self::coercionError( sprintf('Expected type %s', $type->name), @@ -95,7 +98,7 @@ class Value $suggestions = Utils::suggestionList( Utils::printSafe($value), array_map( - function ($enumValue) { + static function ($enumValue) { return $enumValue->name; }, $type->getValues() @@ -118,7 +121,7 @@ class Value if ($type instanceof ListOfType) { $itemType = $type->getWrappedType(); - if (is_array($value) || $value instanceof \Traversable) { + if (is_array($value) || $value instanceof Traversable) { $errors = []; $coercedValue = []; foreach ($value as $index => $itemValue) { @@ -144,7 +147,7 @@ class Value } if ($type instanceof InputObjectType) { - if (! is_object($value) && ! is_array($value) && ! $value instanceof \Traversable) { + if (! is_object($value) && ! is_array($value) && ! $value instanceof Traversable) { return self::ofErrors([ self::coercionError( sprintf('Expected type %s to be an object', $type->name), @@ -225,11 +228,12 @@ class Value } /** - * @param string $message - * @param Node $blameNode - * @param mixed[]|null $path - * @param string $subMessage - * @param \Exception|\Throwable|null $originalError + * @param string $message + * @param Node $blameNode + * @param mixed[]|null $path + * @param string $subMessage + * @param Exception|Throwable|null $originalError + * * @return Error */ private static function coercionError( @@ -258,6 +262,7 @@ class Value * Build a string describing the path into the value where the error was found * * @param mixed[]|null $path + * * @return string */ private static function printPath(?array $path = null) @@ -277,6 +282,7 @@ class Value /** * @param mixed $value + * * @return (mixed|null)[] */ private static function ofValue($value) @@ -287,6 +293,7 @@ class Value /** * @param mixed|null $prev * @param mixed|null $key + * * @return (mixed|null)[] */ private static function atPath($prev, $key) @@ -297,6 +304,7 @@ class Value /** * @param Error[] $errors * @param Error|Error[] $moreErrors + * * @return Error[] */ private static function add($errors, $moreErrors)