From b4f03abca627bf99706c41d109a6d15c6a0cb0b3 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 5 Jul 2019 17:44:22 -0400 Subject: [PATCH] Fix #1886 - allow empty array to be coerced to a mixed one --- src/Psalm/Internal/Analyzer/TypeAnalyzer.php | 1 + src/Psalm/Type/Union.php | 10 ++++++++++ tests/Template/ClassTemplateTest.php | 17 +++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php index e12ebf474..2fa0389b4 100644 --- a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php @@ -1789,6 +1789,7 @@ class TypeAnalyzer && !$container_param->hasTemplate() && !$input_param->hasTemplate() && !$input_param->hasLiteralValue() + && !$input_param->hasEmptyArray() ) { $input_storage = $codebase->classlike_storage_provider->get($input_type_part->value); diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index 5fac2833e..7af6a240f 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -569,6 +569,16 @@ class Union return isset($this->types['array']); } + /** + * @return bool + */ + public function hasEmptyArray() + { + return isset($this->types['array']) + && $this->types['array'] instanceof Atomic\TArray + && $this->types['array']->type_params[1]->isEmpty(); + } + /** * @return bool */ diff --git a/tests/Template/ClassTemplateTest.php b/tests/Template/ClassTemplateTest.php index ca0f683d7..24240fee7 100644 --- a/tests/Template/ClassTemplateTest.php +++ b/tests/Template/ClassTemplateTest.php @@ -1601,6 +1601,23 @@ class ClassTemplateTest extends TestCase return new TestPromise(true); }', ], + 'classTemplatedPropertyEmptyAssignment' => [ + ' */ + private $FooArray; + + public function __construct() { + $this->FooArray = new Foo(function(): array { return []; }); + } + }', + ], ]; }