1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Only collapse Traversable|array when we’re super sure it’s safe

Fixes #1333
This commit is contained in:
Brown 2019-02-15 11:07:08 -05:00
parent 27c99aed2f
commit 0f45d14fea
3 changed files with 30 additions and 1 deletions

View File

@ -204,6 +204,11 @@ class TypeCombination
if (isset($combination->type_params['array'])
&& (isset($combination->named_object_types['Traversable'])
|| isset($combination->type_params['Traversable']))
&& (($codebase && !$codebase->config->allow_phpstorm_generics)
|| isset($combination->type_params['Traversable'])
|| (isset($combination->named_object_types['Traversable'])
&& $combination->named_object_types['Traversable']->from_docblock)
)
) {
$array_param_types = $combination->type_params['array'];
$traversable_param_types = $combination->type_params['Traversable'] ?? [Type::getMixed(), Type::getMixed()];

View File

@ -12,7 +12,7 @@ class AnnotationTest extends TestCase
/**
* @return void
*/
public function testPhpStormGenericsWithValidArgument()
public function testPhpStormGenericsWithValidArrayIteratorArgument()
{
Config::getInstance()->allow_phpstorm_generics = true;
@ -35,6 +35,29 @@ class AnnotationTest extends TestCase
$this->analyzeFile('somefile.php', new Context());
}
/**
* @return void
*/
public function testPhpStormGenericsWithValidTraversableArgument()
{
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
function takesString(string $s): void {}
/** @param Traversable|string[] $i */
function takesTraversableOfString(Traversable $i): void {
foreach ($i as $s2) {
takesString($s2);
}
}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
* @return void
*/

View File

@ -20,6 +20,7 @@ class TypeCombinationTest extends TestCase
{
foreach ($types as $k => $type) {
$types[$k] = self::getAtomic($type);
$types[$k]->setFromDocblock();
}
/** @psalm-suppress InvalidArgument */