From dcec735d11d5d0084939a3bb5e0b2012ac56205c Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Thu, 15 Dec 2016 01:28:36 -0500 Subject: [PATCH] Fix generation of MixedArrayAccess issue --- .../Statements/Expression/FetchChecker.php | 19 +++++++++-- tests/ArrayAccessTest.php | 34 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Checker/Statements/Expression/FetchChecker.php b/src/Psalm/Checker/Statements/Expression/FetchChecker.php index f31c4b8e5..ccfdbab2a 100644 --- a/src/Psalm/Checker/Statements/Expression/FetchChecker.php +++ b/src/Psalm/Checker/Statements/Expression/FetchChecker.php @@ -784,6 +784,8 @@ class FetchChecker return false; } + $inferred_key_type = null; + if (isset($stmt->var->inferredType)) { /** @var Type\Union */ $var_type = $stmt->var->inferredType; @@ -808,6 +810,12 @@ class FetchChecker } else { $key_type = $type->type_params[0]; } + + if ($inferred_key_type) { + Type::combineUnionTypes($key_type, $type->type_params[0]); + } else { + $inferred_key_type = $type->type_params[0]; + } } } } @@ -950,6 +958,12 @@ class FetchChecker $key_type = Type::getInt(); } + if (!$inferred_key_type) { + $inferred_key_type = Type::getInt(); + } else { + $inferred_key_type = Type::combineUnionTypes($inferred_key_type, Type::getInt()); + } + $stmt->inferredType = Type::getString(); } elseif ($type->isNull()) { if (IssueBuffer::accepts( @@ -961,8 +975,7 @@ class FetchChecker )) { if (isset($stmt->inferredType)) { $stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, Type::getNull()); - } - else { + } else { $stmt->inferredType = Type::getNull(); } continue; @@ -1013,7 +1026,7 @@ class FetchChecker if ($stmt->dim) { if (isset($stmt->dim->inferredType) && $key_type && !$key_type->isEmpty()) { foreach ($stmt->dim->inferredType->types as $at) { - if (($at->isMixed() || $at->isEmpty()) && !$key_type->isMixed()) { + if (($at->isMixed() || $at->isEmpty()) && !$inferred_key_type->isMixed()) { if (IssueBuffer::accepts( new MixedArrayOffset( 'Cannot access value on variable ' . $var_id . ' using mixed offset - expecting ' . diff --git a/tests/ArrayAccessTest.php b/tests/ArrayAccessTest.php index a22d30d34..7cd235606 100644 --- a/tests/ArrayAccessTest.php +++ b/tests/ArrayAccessTest.php @@ -116,4 +116,38 @@ class ArrayAccessTest extends PHPUnit_Framework_TestCase $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); $file_checker->check(true, true, $context); } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage MixedArrayAccess + */ + public function testMixedArrayAccess() + { + $context = new Context('somefile.php'); + $stmts = self::$parser->parse('check(true, true, $context); + } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage MixedArrayOffset + */ + public function testMixedArrayOffset() + { + $context = new Context('somefile.php'); + $stmts = self::$parser->parse('check(true, true, $context); + } }