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.
*
* @api
* @return bool
*
* @api
*/
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.
*
* @api
* @return string
*
* @api
*/
public function getCategory();
}

View File

@ -4,10 +4,13 @@ declare(strict_types=1);
namespace GraphQL\Error;
use Exception;
use GraphQL\Language\AST\Node;
use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation;
use GraphQL\Utils\Utils;
use JsonSerializable;
use Throwable;
use Traversable;
use function array_filter;
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
* 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_INTERNAL = 'internal';
@ -85,7 +88,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* @param Node|Node[]|Traversable|null $nodes
* @param mixed[]|null $positions
* @param mixed[]|null $path
* @param \Throwable $previous
* @param Throwable $previous
* @param mixed[] $extensions
*/
public function __construct(
@ -100,7 +103,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
parent::__construct($message, 0, $previous);
// Compute list of blame nodes.
if ($nodes instanceof \Traversable) {
if ($nodes instanceof Traversable) {
$nodes = iterator_to_array($nodes);
} elseif ($nodes && ! is_array($nodes)) {
$nodes = [$nodes];
@ -136,6 +139,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* @param mixed $error
* @param Node[]|null $nodes
* @param mixed[]|null $path
*
* @return Error
*/
public static function createLocatedError($error, $nodes = null, $path = null)
@ -159,7 +163,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
$source = $error->source;
$positions = $error->positions;
$extensions = $error->extensions;
} elseif ($error instanceof \Exception || $error instanceof \Throwable) {
} elseif ($error instanceof Exception || $error instanceof Throwable) {
$message = $error->getMessage();
$originalError = $error;
} else {
@ -222,7 +226,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
{
if ($this->positions === null && ! empty($this->nodes)) {
$positions = array_map(
function ($node) {
static function ($node) {
return isset($node->loc) ? $node->loc->start : null;
},
$this->nodes
@ -230,7 +234,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
$this->positions = array_filter(
$positions,
function ($p) {
static function ($p) {
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
* single location, the field which produced the error.
*
* @api
* @return SourceLocation[]
*
* @api
*/
public function getLocations()
{
@ -262,7 +267,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
if ($positions && $source) {
$this->locations = array_map(
function ($pos) use ($source) {
static function ($pos) use ($source) {
return $source->getLocation($pos);
},
$positions
@ -270,7 +275,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
} elseif ($nodes) {
$this->locations = array_filter(
array_map(
function ($node) {
static function ($node) {
if ($node->loc && $node->loc->source) {
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.
* Only included for execution errors.
*
* @api
* @return mixed[]|null
*
* @api
*/
public function getPath()
{
@ -318,6 +324,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
* Returns array representation of error suitable for serialization
*
* @deprecated Use FormattedError::createFromException() instead
*
* @return mixed[]
*/
public function toSerializableArray()
@ -328,7 +335,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
$locations = Utils::map(
$this->getLocations(),
function (SourceLocation $loc) {
static function (SourceLocation $loc) {
return $loc->toSerializableArray();
}
);
@ -348,7 +355,9 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
/**
* Specify data which should be serialized to JSON
*
* @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.
*/

View File

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

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace GraphQL\Error;
use LogicException;
/**
* 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
* user-land code
*/
class InvariantViolation extends \LogicException
class InvariantViolation extends LogicException
{
}

View File

@ -4,12 +4,14 @@ declare(strict_types=1);
namespace GraphQL\Error;
use RuntimeException;
/**
* Class UserError
*
* 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

View File

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