From d868710b2bf890142b8364f648f7272c11dab29a Mon Sep 17 00:00:00 2001 From: Brown Date: Wed, 8 Apr 2020 16:11:11 -0400 Subject: [PATCH] Fix handling of built arrays --- src/Psalm/Config/Creator.php | 1 - .../Expression/Fetch/ArrayFetchAnalyzer.php | 2 +- src/Psalm/Type/Union.php | 13 +++++++++++++ tests/Loop/ForeachTest.php | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Config/Creator.php b/src/Psalm/Config/Creator.php index 2d28c1101..ea7da2e42 100644 --- a/src/Psalm/Config/Creator.php +++ b/src/Psalm/Config/Creator.php @@ -121,7 +121,6 @@ class Creator $issues_at_level[$issue_level][$issue_type] = 0; } - /** @psalm-suppress MixedOperand */ $issues_at_level[$issue_level][$issue_type] += 100/$counted_types; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index 04142d8a3..46f01d857 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -118,7 +118,7 @@ class ArrayFetchAnalyzer if ($keyed_array_var_id && $context->hasVariable($keyed_array_var_id) && !$context->vars_in_scope[$keyed_array_var_id]->possibly_undefined - && !$context->vars_in_scope[$keyed_array_var_id]->isVanillaMixed() + && !$context->vars_in_scope[$keyed_array_var_id]->hasVanillaMixed() ) { $statements_analyzer->node_data->setType( $stmt, diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index 63c345157..48a977f84 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -978,6 +978,19 @@ class Union implements TypeNode && count($this->types) === 1; } + /** + * @return bool + */ + public function hasVanillaMixed() + { + /** + * @psalm-suppress UndefinedPropertyFetch + */ + return isset($this->types['mixed']) + && !$this->types['mixed']->from_loop_isset + && get_class($this->types['mixed']) === Type\Atomic\TMixed::class; + } + /** * @return bool */ diff --git a/tests/Loop/ForeachTest.php b/tests/Loop/ForeachTest.php index d4aca9732..f9a00487d 100644 --- a/tests/Loop/ForeachTest.php +++ b/tests/Loop/ForeachTest.php @@ -985,6 +985,20 @@ class ForeachTest extends \Psalm\Tests\TestCase $list = [4, 5, 6]; }', ], + 'createNestedArrayInLoop' => [ + '