1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #4517 - track type contradiction issues in match expressions

This commit is contained in:
Matt Brown 2020-11-09 10:00:53 -05:00
parent 3aaa1d8447
commit e97a9c86eb
3 changed files with 37 additions and 8 deletions

View File

@ -106,10 +106,14 @@ class MatchAnalyzer
new PhpParser\Node\Expr\Throw_( new PhpParser\Node\Expr\Throw_(
new PhpParser\Node\Expr\New_( new PhpParser\Node\Expr\New_(
new PhpParser\Node\Name\FullyQualified( new PhpParser\Node\Name\FullyQualified(
'UnhandledMatchError' 'UnhandledMatchError',
) $stmt->getAttributes()
),
[],
$stmt->getAttributes()
) )
) ),
$stmt->getAttributes()
); );
} }
@ -255,17 +259,28 @@ class MatchAnalyzer
new PhpParser\Node\Name\FullyQualified(['in_array']), new PhpParser\Node\Name\FullyQualified(['in_array']),
[ [
new PhpParser\Node\Arg( new PhpParser\Node\Arg(
$match_condition $match_condition,
false,
false,
$attributes
), ),
new PhpParser\Node\Arg( new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Array_( new PhpParser\Node\Expr\Array_(
$array_items $array_items,
) $attributes
),
false,
false,
$attributes
), ),
new PhpParser\Node\Arg( new PhpParser\Node\Arg(
new PhpParser\Node\Expr\ConstFetch( new PhpParser\Node\Expr\ConstFetch(
new PhpParser\Node\Name\FullyQualified(['true']) new PhpParser\Node\Name\FullyQualified(['true']),
) $attributes
),
false,
false,
$attributes
), ),
], ],
$attributes $attributes

View File

@ -72,3 +72,5 @@ class ReflectionUnionType extends ReflectionType {
*/ */
public function getTypes() {} public function getTypes() {}
} }
class UnhandledMatchError extends Error {}

View File

@ -205,6 +205,18 @@ class MatchTest extends TestCase
false, false,
'8.0', '8.0',
], ],
'matchTrueImpossible' => [
'<?php
$foo = new \stdClass();
$a = match (true) {
$foo instanceof \stdClass => 1,
$foo instanceof \Exception => 1,
};',
'error_message' => 'TypeDoesNotContainType',
[],
false,
'8.0',
],
]; ];
} }
} }