Skip to content
Permalink
Browse files

Fix #1932 - coerce empty array passed as template param to general

  • Loading branch information...
muglug committed Jul 10, 2019
1 parent e7aadee commit 2cb762551559268e7e2d669e041f9e2aee139d70
Showing with 39 additions and 3 deletions.
  1. +4 −3 src/Psalm/Internal/Analyzer/TypeAnalyzer.php
  2. +35 −0 tests/Template/ClassTemplateTest.php
@@ -1776,10 +1776,11 @@ private static function isMatchingTypeContainedBy(
} elseif (!$input_type_part instanceof TIterable
&& !$container_param->hasTemplate()
&& !$input_param->hasTemplate()
&& !$input_param->hasLiteralValue()
&& !$input_param->hasEmptyArray()
) {
if ($input_param->had_template) {
if ($input_param->had_template
|| $input_param->hasEmptyArray()
|| $input_param->hasLiteralValue()
) {
if (!$atomic_comparison_result->replacement_atomic_type) {
$atomic_comparison_result->replacement_atomic_type = clone $input_type_part;
}
@@ -2057,6 +2057,41 @@ public function __construct() {
function expectsShape($_): void {}',
'error_message' => 'MixedArgumentTypeCoercion'
],
'coerceEmptyArrayToGeneral' => [
'<?php
/** @template T */
class Foo
{
/** @param \Closure(string):T $closure */
public function __construct($closure) {}
}
class Bar
{
/** @var Foo<array> */
private $FooArray;
public function __construct()
{
$this->FooArray = new Foo(function(string $s): array {
/** @psalm-suppress MixedAssignment */
$json = \json_decode($s, true);
if (! \is_array($json)) {
return [];
}
return $json;
});
takesEmpty($this->FooArray);
}
}
/** @param Foo<array<empty, empty>> $_ */
function takesEmpty($_): void {}',
'error_message' => 'MixedArgumentTypeCoercion'
],
];
}
}

0 comments on commit 2cb7625

Please sign in to comment.
You can’t perform that action at this time.