1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +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 committed by Daniil Gentili
parent 9fd4a24c55
commit 1ad65fb899
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
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\New_(
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\Arg(
$match_condition
$match_condition,
false,
false,
$attributes
),
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Array_(
$array_items
)
$array_items,
$attributes
),
false,
false,
$attributes
),
new PhpParser\Node\Arg(
new PhpParser\Node\Expr\ConstFetch(
new PhpParser\Node\Name\FullyQualified(['true'])
)
new PhpParser\Node\Name\FullyQualified(['true']),
$attributes
),
false,
false,
$attributes
),
],
$attributes

View File

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

View File

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