Fix CS in src/Error

This commit is contained in:
Simon Podlipsky 2018-09-26 10:36:07 +02:00
parent 73e75b6314
commit 0063bd6c15
No known key found for this signature in database
GPG Key ID: 725C2BD962B42663
6 changed files with 77 additions and 41 deletions

View File

@ -17,8 +17,9 @@ interface ClientAware
/** /**
* Returns true when exception message is safe to be displayed to a client. * Returns true when exception message is safe to be displayed to a client.
* *
* @api
* @return bool * @return bool
*
* @api
*/ */
public function isClientSafe(); public function isClientSafe();
@ -27,8 +28,9 @@ interface ClientAware
* *
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it. * Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
* *
* @api
* @return string * @return string
*
* @api
*/ */
public function getCategory(); public function getCategory();
} }

View File

@ -4,10 +4,13 @@ declare(strict_types=1);
namespace GraphQL\Error; namespace GraphQL\Error;
use Exception;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\Source; use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation; use GraphQL\Language\SourceLocation;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use JsonSerializable;
use Throwable;
use Traversable; use Traversable;
use function array_filter; use function array_filter;
use function array_map; use function array_map;
@ -28,7 +31,7 @@ use function iterator_to_array;
* Class extends standard PHP `\Exception`, so all standard methods of base `\Exception` class * Class extends standard PHP `\Exception`, so all standard methods of base `\Exception` class
* are available in addition to those listed below. * are available in addition to those listed below.
*/ */
class Error extends \Exception implements \JsonSerializable, ClientAware class Error extends Exception implements JsonSerializable, ClientAware
{ {
const CATEGORY_GRAPHQL = 'graphql'; const CATEGORY_GRAPHQL = 'graphql';
const CATEGORY_INTERNAL = 'internal'; const CATEGORY_INTERNAL = 'internal';
@ -85,7 +88,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* @param Node|Node[]|Traversable|null $nodes * @param Node|Node[]|Traversable|null $nodes
* @param mixed[]|null $positions * @param mixed[]|null $positions
* @param mixed[]|null $path * @param mixed[]|null $path
* @param \Throwable $previous * @param Throwable $previous
* @param mixed[] $extensions * @param mixed[] $extensions
*/ */
public function __construct( public function __construct(
@ -100,7 +103,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
parent::__construct($message, 0, $previous); parent::__construct($message, 0, $previous);
// Compute list of blame nodes. // Compute list of blame nodes.
if ($nodes instanceof \Traversable) { if ($nodes instanceof Traversable) {
$nodes = iterator_to_array($nodes); $nodes = iterator_to_array($nodes);
} elseif ($nodes && ! is_array($nodes)) { } elseif ($nodes && ! is_array($nodes)) {
$nodes = [$nodes]; $nodes = [$nodes];
@ -136,6 +139,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* @param mixed $error * @param mixed $error
* @param Node[]|null $nodes * @param Node[]|null $nodes
* @param mixed[]|null $path * @param mixed[]|null $path
*
* @return Error * @return Error
*/ */
public static function createLocatedError($error, $nodes = null, $path = null) public static function createLocatedError($error, $nodes = null, $path = null)
@ -159,7 +163,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
$source = $error->source; $source = $error->source;
$positions = $error->positions; $positions = $error->positions;
$extensions = $error->extensions; $extensions = $error->extensions;
} elseif ($error instanceof \Exception || $error instanceof \Throwable) { } elseif ($error instanceof Exception || $error instanceof Throwable) {
$message = $error->getMessage(); $message = $error->getMessage();
$originalError = $error; $originalError = $error;
} else { } else {
@ -222,7 +226,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
{ {
if ($this->positions === null && ! empty($this->nodes)) { if ($this->positions === null && ! empty($this->nodes)) {
$positions = array_map( $positions = array_map(
function ($node) { static function ($node) {
return isset($node->loc) ? $node->loc->start : null; return isset($node->loc) ? $node->loc->start : null;
}, },
$this->nodes $this->nodes
@ -230,7 +234,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
$this->positions = array_filter( $this->positions = array_filter(
$positions, $positions,
function ($p) { static function ($p) {
return $p !== null; return $p !== null;
} }
); );
@ -250,8 +254,9 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* point out to field mentioned in multiple fragments. Errors during execution include a * point out to field mentioned in multiple fragments. Errors during execution include a
* single location, the field which produced the error. * single location, the field which produced the error.
* *
* @api
* @return SourceLocation[] * @return SourceLocation[]
*
* @api
*/ */
public function getLocations() public function getLocations()
{ {
@ -262,7 +267,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
if ($positions && $source) { if ($positions && $source) {
$this->locations = array_map( $this->locations = array_map(
function ($pos) use ($source) { static function ($pos) use ($source) {
return $source->getLocation($pos); return $source->getLocation($pos);
}, },
$positions $positions
@ -270,7 +275,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
} elseif ($nodes) { } elseif ($nodes) {
$this->locations = array_filter( $this->locations = array_filter(
array_map( array_map(
function ($node) { static function ($node) {
if ($node->loc && $node->loc->source) { if ($node->loc && $node->loc->source) {
return $node->loc->source->getLocation($node->loc->start); return $node->loc->source->getLocation($node->loc->start);
} }
@ -298,8 +303,9 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* Returns an array describing the path from the root value to the field which produced this error. * Returns an array describing the path from the root value to the field which produced this error.
* Only included for execution errors. * Only included for execution errors.
* *
* @api
* @return mixed[]|null * @return mixed[]|null
*
* @api
*/ */
public function getPath() public function getPath()
{ {
@ -318,6 +324,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* Returns array representation of error suitable for serialization * Returns array representation of error suitable for serialization
* *
* @deprecated Use FormattedError::createFromException() instead * @deprecated Use FormattedError::createFromException() instead
*
* @return mixed[] * @return mixed[]
*/ */
public function toSerializableArray() public function toSerializableArray()
@ -328,7 +335,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
$locations = Utils::map( $locations = Utils::map(
$this->getLocations(), $this->getLocations(),
function (SourceLocation $loc) { static function (SourceLocation $loc) {
return $loc->toSerializableArray(); return $loc->toSerializableArray();
} }
); );
@ -348,7 +355,9 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
/** /**
* Specify data which should be serialized to JSON * Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
*
* @return mixed data which can be serialized by <b>json_encode</b>, * @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource. * which is a value of any type other than a resource.
*/ */

View File

@ -4,12 +4,16 @@ declare(strict_types=1);
namespace GraphQL\Error; namespace GraphQL\Error;
use Countable;
use ErrorException;
use Exception;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\Source; use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation; use GraphQL\Language\SourceLocation;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\WrappingType; use GraphQL\Type\Definition\WrappingType;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use Throwable;
use function addcslashes; use function addcslashes;
use function array_filter; use function array_filter;
use function array_intersect_key; use function array_intersect_key;
@ -45,8 +49,9 @@ class FormattedError
* Set default error message for internal errors formatted using createFormattedError(). * Set default error message for internal errors formatted using createFormattedError().
* This value can be overridden by passing 3rd argument to `createFormattedError()`. * This value can be overridden by passing 3rd argument to `createFormattedError()`.
* *
* @api
* @param string $msg * @param string $msg
*
* @api
*/ */
public static function setInternalErrorMessage($msg) public static function setInternalErrorMessage($msg)
{ {
@ -132,6 +137,7 @@ class FormattedError
/** /**
* @param int $len * @param int $len
*
* @return string * @return string
*/ */
private static function whitespace($len) private static function whitespace($len)
@ -141,6 +147,7 @@ class FormattedError
/** /**
* @param int $len * @param int $len
*
* @return string * @return string
*/ */
private static function lpad($len, $str) private static function lpad($len, $str)
@ -157,17 +164,20 @@ class FormattedError
* *
* For a list of available debug flags see GraphQL\Error\Debug constants. * For a list of available debug flags see GraphQL\Error\Debug constants.
* *
* @api * @param Throwable $e
* @param \Throwable $e * @param bool|int $debug
* @param bool|int $debug * @param string $internalErrorMessage
* @param string $internalErrorMessage *
* @return mixed[] * @return mixed[]
* @throws \Throwable *
* @throws Throwable
*
* @api
*/ */
public static function createFromException($e, $debug = false, $internalErrorMessage = null) public static function createFromException($e, $debug = false, $internalErrorMessage = null)
{ {
Utils::invariant( Utils::invariant(
$e instanceof \Exception || $e instanceof \Throwable, $e instanceof Exception || $e instanceof Throwable,
'Expected exception, got %s', 'Expected exception, got %s',
Utils::getVariableType($e) Utils::getVariableType($e)
); );
@ -189,7 +199,7 @@ class FormattedError
if ($e instanceof Error) { if ($e instanceof Error) {
$locations = Utils::map( $locations = Utils::map(
$e->getLocations(), $e->getLocations(),
function (SourceLocation $loc) { static function (SourceLocation $loc) {
return $loc->toSerializableArray(); return $loc->toSerializableArray();
} }
); );
@ -215,11 +225,13 @@ class FormattedError
* Decorates spec-compliant $formattedError with debug entries according to $debug flags * Decorates spec-compliant $formattedError with debug entries according to $debug flags
* (see GraphQL\Error\Debug for available flags) * (see GraphQL\Error\Debug for available flags)
* *
* @param mixed[] $formattedError * @param mixed[] $formattedError
* @param \Throwable $e * @param Throwable $e
* @param bool $debug * @param bool $debug
*
* @return mixed[] * @return mixed[]
* @throws \Throwable *
* @throws Throwable
*/ */
public static function addDebugEntries(array $formattedError, $e, $debug) public static function addDebugEntries(array $formattedError, $e, $debug)
{ {
@ -228,7 +240,7 @@ class FormattedError
} }
Utils::invariant( Utils::invariant(
$e instanceof \Exception || $e instanceof \Throwable, $e instanceof Exception || $e instanceof Throwable,
'Expected exception, got %s', 'Expected exception, got %s',
Utils::getVariableType($e) Utils::getVariableType($e)
); );
@ -259,7 +271,7 @@ class FormattedError
} }
if ($debug & Debug::INCLUDE_TRACE) { if ($debug & Debug::INCLUDE_TRACE) {
if ($e instanceof \ErrorException || $e instanceof \Error) { if ($e instanceof ErrorException || $e instanceof \Error) {
$formattedError += [ $formattedError += [
'file' => $e->getFile(), 'file' => $e->getFile(),
'line' => $e->getLine(), 'line' => $e->getLine(),
@ -282,15 +294,16 @@ class FormattedError
* If initial formatter is not set, FormattedError::createFromException is used * If initial formatter is not set, FormattedError::createFromException is used
* *
* @param bool $debug * @param bool $debug
* @return callable|\Closure *
* @return callable|callable
*/ */
public static function prepareFormatter(?callable $formatter = null, $debug) public static function prepareFormatter(?callable $formatter = null, $debug)
{ {
$formatter = $formatter ?: function ($e) { $formatter = $formatter ?: static function ($e) {
return FormattedError::createFromException($e); return FormattedError::createFromException($e);
}; };
if ($debug) { if ($debug) {
$formatter = function ($e) use ($formatter, $debug) { $formatter = static function ($e) use ($formatter, $debug) {
return FormattedError::addDebugEntries($formatter($e), $e, $debug); return FormattedError::addDebugEntries($formatter($e), $e, $debug);
}; };
} }
@ -301,9 +314,11 @@ class FormattedError
/** /**
* Returns error trace as serializable array * Returns error trace as serializable array
* *
* @api * @param Throwable $error
* @param \Throwable $error *
* @return mixed[] * @return mixed[]
*
* @api
*/ */
public static function toSafeTrace($error) public static function toSafeTrace($error)
{ {
@ -319,12 +334,12 @@ class FormattedError
} }
return array_map( return array_map(
function ($err) { static function ($err) {
$safeErr = array_intersect_key($err, ['file' => true, 'line' => true]); $safeErr = array_intersect_key($err, ['file' => true, 'line' => true]);
if (isset($err['function'])) { if (isset($err['function'])) {
$func = $err['function']; $func = $err['function'];
$args = ! empty($err['args']) ? array_map([__CLASS__, 'printVar'], $err['args']) : []; $args = ! empty($err['args']) ? array_map([self::class, 'printVar'], $err['args']) : [];
$funcStr = $func . '(' . implode(', ', $args) . ')'; $funcStr = $func . '(' . implode(', ', $args) . ')';
if (isset($err['class'])) { if (isset($err['class'])) {
@ -342,6 +357,7 @@ class FormattedError
/** /**
* @param mixed $var * @param mixed $var
*
* @return string * @return string
*/ */
public static function printVar($var) public static function printVar($var)
@ -356,7 +372,7 @@ class FormattedError
} }
if (is_object($var)) { if (is_object($var)) {
return 'instance of ' . get_class($var) . ($var instanceof \Countable ? '(' . count($var) . ')' : ''); return 'instance of ' . get_class($var) . ($var instanceof Countable ? '(' . count($var) . ')' : '');
} }
if (is_array($var)) { if (is_array($var)) {
return 'array(' . count($var) . ')'; return 'array(' . count($var) . ')';
@ -382,8 +398,10 @@ class FormattedError
/** /**
* @deprecated as of v0.8.0 * @deprecated as of v0.8.0
*
* @param string $error * @param string $error
* @param SourceLocation[] $locations * @param SourceLocation[] $locations
*
* @return mixed[] * @return mixed[]
*/ */
public static function create($error, array $locations = []) public static function create($error, array $locations = [])
@ -392,7 +410,7 @@ class FormattedError
if (! empty($locations)) { if (! empty($locations)) {
$formatted['locations'] = array_map( $formatted['locations'] = array_map(
function ($loc) { static function ($loc) {
return $loc->toArray(); return $loc->toArray();
}, },
$locations $locations
@ -404,9 +422,10 @@ class FormattedError
/** /**
* @deprecated as of v0.10.0, use general purpose method createFromException() instead * @deprecated as of v0.10.0, use general purpose method createFromException() instead
*
* @return mixed[] * @return mixed[]
*/ */
public static function createFromPHPError(\ErrorException $e) public static function createFromPHPError(ErrorException $e)
{ {
return [ return [
'message' => $e->getMessage(), 'message' => $e->getMessage(),

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace GraphQL\Error; namespace GraphQL\Error;
use LogicException;
/** /**
* Class InvariantVoilation * Class InvariantVoilation
* *
@ -11,6 +13,6 @@ namespace GraphQL\Error;
* This exception should not inherit base Error exception as it is raised when there is an error somewhere in * This exception should not inherit base Error exception as it is raised when there is an error somewhere in
* user-land code * user-land code
*/ */
class InvariantViolation extends \LogicException class InvariantViolation extends LogicException
{ {
} }

View File

@ -4,12 +4,14 @@ declare(strict_types=1);
namespace GraphQL\Error; namespace GraphQL\Error;
use RuntimeException;
/** /**
* Class UserError * Class UserError
* *
* Error caused by actions of GraphQL clients. Can be safely displayed to a client... * Error caused by actions of GraphQL clients. Can be safely displayed to a client...
*/ */
class UserError extends \RuntimeException implements ClientAware class UserError extends RuntimeException implements ClientAware
{ {
/** /**
* @return bool * @return bool

View File

@ -50,8 +50,9 @@ final class Warning
* *
* When passing true - suppresses all warnings. * When passing true - suppresses all warnings.
* *
* @api
* @param bool|int $suppress * @param bool|int $suppress
*
* @api
*/ */
public static function suppress($suppress = true) public static function suppress($suppress = true)
{ {
@ -74,8 +75,9 @@ final class Warning
* *
* When passing true - re-enables all warnings. * When passing true - re-enables all warnings.
* *
* @api
* @param bool|int $enable * @param bool|int $enable
*
* @api
*/ */
public static function enable($enable = true) public static function enable($enable = true)
{ {