1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Allow coercion from empty to array param

This commit is contained in:
Matthew Brown 2019-05-21 09:02:38 -04:00
parent 4ecf370900
commit ec2ee04fe8
2 changed files with 27 additions and 29 deletions

View File

@ -1591,6 +1591,10 @@ class TypeAnalyzer
continue;
}
if ($input_param->isEmpty()) {
continue;
}
if (!self::isContainedBy(
$codebase,
$input_param,

View File

@ -2230,6 +2230,29 @@ class TemplateTest extends TestCase
'$y' => 'array<array-key, A&B>',
]
],
'templateEmptyParamCoercion' => [
'<?php
namespace NS;
use Countable;
/** @template T */
class Collection
{
/** @psalm-var iterable<T> */
private $data;
/** @psalm-param iterable<T> $data */
public function __construct(iterable $data = []) {
$this->data = $data;
}
}
class Item {}
/** @psalm-param Collection<Item> $c */
function takesCollectionOfItems(Collection $c): void {}
takesCollectionOfItems(new Collection());',
],
];
}
@ -2757,35 +2780,6 @@ class TemplateTest extends TestCase
}',
'error_message' => 'InvalidDocblock',
],
'noComparisonToEmpty' => [
'<?php
/**
* @template K
* @template V
*/
class Container {
/** @var array<K, V> */
private $c;
/** @param array<K, V> $c */
public function __construct(array $c) {
$this->c = $c;
}
}
class Test {
/**
* @var Container<int, DateTime>
*/
private $c;
public function __construct()
{
$this->c = new Container([]);
}
}',
'error_message' => 'InvalidPropertyAssignmentValue'
],
'preventDogCatSnafu' => [
'<?php
class Animal {}