From 5c132ba8b8aa34d02f0e8c0cc27ed08c9f498803 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 1 Nov 2016 10:28:09 -0400 Subject: [PATCH] Fix a few type errors --- src/Psalm/Type.php | 6 +++--- src/Psalm/Type/ParseTree.php | 13 +++++++++++-- src/Psalm/Type/Union.php | 8 +------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index aa3852dbc..346f62ba3 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -401,10 +401,10 @@ abstract class Type throw new \InvalidArgumentException('You must pass at least one type to combineTypes'); } - /** @var array> */ + /** @var array> */ $key_types = []; - /** @var array> */ + /** @var array> */ $value_types = []; foreach ($types as $type) { @@ -447,7 +447,7 @@ abstract class Type } elseif ($type instanceof ObjectLike) { if (!isset($value_types['object-like'])) { - /** @var array */ + /** @var array */ $value_types['object-like'] = []; } diff --git a/src/Psalm/Type/ParseTree.php b/src/Psalm/Type/ParseTree.php index c47b46a4a..3bd2f29d8 100644 --- a/src/Psalm/Type/ParseTree.php +++ b/src/Psalm/Type/ParseTree.php @@ -99,8 +99,12 @@ class ParseTree throw new \InvalidArgumentException('Cannot parse comma in non-generic/array type'); } + if (!$current_parent) { + throw new \InvalidArgumentException('Cannot parse comma in with no parent node'); + } + if ($context_node->value === self::GENERIC && $current_parent->value !== self::GENERIC) { - if (!$current_parent->parent->value) { + if (!isset($current_parent->parent) || !$current_parent->parent->value) { throw new \InvalidArgumentException('Cannot parse comma in non-generic/array type'); } @@ -110,7 +114,7 @@ class ParseTree do { $current_leaf = $current_leaf->parent; } - while ($current_leaf->parent->value !== self::OBJECT_LIKE); + while ($current_leaf->parent && $current_leaf->parent->value !== self::OBJECT_LIKE); } break; @@ -163,6 +167,11 @@ class ParseTree } $new_leaf = new self($type_token, $current_leaf->parent); + + if (!isset($current_leaf->parent)) { + throw new \InvalidArgumentException('Current leaf must have a parent'); + } + $current_leaf->parent->children[] = $new_leaf; $current_leaf = $new_leaf; diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index ef72f31ba..cfbb944ec 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -160,13 +160,7 @@ class Union extends Type /** @return bool */ public function isEmpty() { - if ($this instanceof Atomic) { - return $this->value === 'empty'; - } - - if ($this instanceof Union) { - return isset($this->types['empty']); - } + return isset($this->types['empty']); } public function removeObjects()