1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-04 18:48:03 +01:00

Merge pull request #6594 from orklah/falsable-returns

Falsable returns
This commit is contained in:
orklah 2021-10-05 20:15:25 +02:00 committed by GitHub
commit 0f275cb25a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -497,6 +497,14 @@ class Union implements TypeNode
$nullable = true; $nullable = true;
} }
$falsable = false;
if (isset($types['false']) && count($types) > 1) {
unset($types['false']);
$falsable = true;
}
$php_types = []; $php_types = [];
foreach ($types as $atomic_type) { foreach ($types as $atomic_type) {
@ -515,6 +523,10 @@ class Union implements TypeNode
$php_types[] = $php_type; $php_types[] = $php_type;
} }
if ($falsable) {
return ($nullable ? '?' : '') . implode('|', array_unique($php_types)) . '|false';
}
return ($nullable ? '?' : '') . implode('|', array_unique($php_types)); return ($nullable ? '?' : '') . implode('|', array_unique($php_types));
} }

View File

@ -713,6 +713,24 @@ class ReturnTypeManipulationTest extends FileManipulationTestCase
['MissingReturnType'], ['MissingReturnType'],
false, false,
true, true,
],
'OrFalseInReturn' => [
'<?php
function a() {
/** @var false|array $a */
$a = false;
return $a;
}',
'<?php
function a(): array|false {
/** @var false|array $a */
$a = false;
return $a;
}',
'8.0',
['MissingReturnType'],
false,
true,
] ]
]; ];
} }