Skip to content

Commit

Permalink
Fix Error
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored and vladar committed Jun 21, 2020
1 parent 166a187 commit 0fa6330
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $e = new Error(
'msg',
null,
null,
null,
[],
null,
null,
['foo' => 'bar']
Expand Down
28 changes: 15 additions & 13 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Error extends Exception implements JsonSerializable, ClientAware
*/
private $source;

/** @var int[]|null */
/** @var int[] */
private $positions;

/** @var bool */
Expand All @@ -81,7 +81,7 @@ class Error extends Exception implements JsonSerializable, ClientAware
/**
* @param string $message
* @param Node|Node[]|Traversable|null $nodes
* @param mixed[]|null $positions
* @param mixed[] $positions
* @param mixed[]|null $path
* @param Throwable $previous
* @param mixed[] $extensions
Expand All @@ -90,7 +90,7 @@ public function __construct(
$message,
$nodes = null,
?Source $source = null,
$positions = null,
array $positions = [],
$path = null,
$previous = null,
array $extensions = []
Expand All @@ -100,7 +100,7 @@ public function __construct(
// Compute list of blame nodes.
if ($nodes instanceof Traversable) {
$nodes = iterator_to_array($nodes);
} elseif ($nodes && ! is_array($nodes)) {
} elseif ($nodes !== null && ! is_array($nodes)) {
$nodes = [$nodes];
}

Expand All @@ -109,7 +109,7 @@ public function __construct(
$this->positions = $positions;
$this->path = $path;
$this->extensions = count($extensions) > 0 ? $extensions : (
$previous && $previous instanceof self
$previous instanceof self
? $previous->extensions
: []
);
Expand All @@ -118,7 +118,7 @@ public function __construct(
$this->isClientSafe = $previous->isClientSafe();
$cat = $previous->getCategory();
$this->category = $cat === '' || $cat === null ? self::CATEGORY_INTERNAL: $cat;
} elseif ($previous) {
} elseif ($previous !== null) {
$this->isClientSafe = false;
$this->category = self::CATEGORY_INTERNAL;
} else {
Expand All @@ -141,16 +141,18 @@ public function __construct(
public static function createLocatedError($error, $nodes = null, $path = null)
{
if ($error instanceof self) {
if ($error->path && $error->nodes) {
if ($error->path !== null && $error->nodes !== null && count($error->nodes) !== 0) {
return $error;
}

$nodes = $nodes ?? $error->nodes;
$path = $path ?? $error->path;
}

$source = $positions = $originalError = null;
$extensions = [];
$source = null;
$originalError = null;
$positions = [];
$extensions = [];

if ($error instanceof self) {
$message = $error->getMessage();
Expand Down Expand Up @@ -218,9 +220,9 @@ public function getSource()
/**
* @return int[]
*/
public function getPositions()
public function getPositions() : array
{
if ($this->positions === null && ! empty($this->nodes)) {
if (count($this->positions) === 0 && ! empty($this->nodes)) {
$positions = array_map(
static function ($node) : ?int {
return isset($node->loc) ? $node->loc->start : null;
Expand Down Expand Up @@ -263,14 +265,14 @@ public function getLocations()
$source = $this->getSource();
$nodes = $this->nodes;

if ($positions && $source) {
if ($source !== null && count($positions) !== 0) {
$this->locations = array_map(
static function ($pos) use ($source) : SourceLocation {
return $source->getLocation($pos);
},
$positions
);
} elseif ($nodes) {
} elseif ($nodes !== null && count($nodes) !== 0) {
$locations = array_filter(
array_map(
static function ($node) : ?SourceLocation {
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/ASTDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private function internalBuildType($typeName, $typeNode = null)
sprintf('when building %s type: %s', $typeName, $e->getMessage()),
null,
null,
null,
[],
null,
$e
);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private static function coercionError(
($subMessage ? '; ' . $subMessage : '.'),
$blameNode,
null,
null,
[],
null,
$originalError
);
Expand Down
6 changes: 3 additions & 3 deletions tests/Error/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ErrorTest extends TestCase
public function testUsesTheStackOfAnOriginalError() : void
{
$prev = new Exception('Original');
$err = new Error('msg', null, null, null, null, $prev);
$err = new Error('msg', null, null, [], null, $prev);

self::assertSame($err->getPrevious(), $prev);
}
Expand Down Expand Up @@ -134,7 +134,7 @@ public function testSerializesToIncludePath() : void
'msg',
null,
null,
null,
[],
['path', 3, 'to', 'field']
);

Expand All @@ -151,7 +151,7 @@ public function testDefaultErrorFormatterIncludesExtensionFields() : void
'msg',
null,
null,
null,
[],
null,
null,
['foo' => 'bar']
Expand Down
2 changes: 1 addition & 1 deletion tests/Executor/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public function testNullsOutErrorSubtrees() : void
'Error getting asyncReturnErrorWithExtensions',
null,
null,
null,
[],
null,
null,
['foo' => 'bar']
Expand Down

0 comments on commit 0fa6330

Please sign in to comment.