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

Ignore redundant conditions inside match potentially in perpetuity?

This commit is contained in:
Brown 2020-08-30 16:23:53 -04:00 committed by Daniil Gentili
parent b785efb210
commit 9b726904f9
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 61 additions and 0 deletions

View File

@ -110,10 +110,28 @@ class MatchAnalyzer
);
}
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (ExpressionAnalyzer::analyze($statements_analyzer, $ternary, $context) === false) {
return false;
}
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if ($stmt_expr_type = $statements_analyzer->node_data->getType($ternary)) {
$old_node_data->setType($stmt, $stmt_expr_type ?: Type::getMixed());
}

View File

@ -43,6 +43,19 @@ class MatchTest extends TestCase
[],
'8.0'
],
'allMatchedNoRedundantCondition' => [
'<?php
function foo() : string {
$a = rand(0, 1) ? "a" : "b";
return match ($a) {
"a" => "hello",
"b" => "goodbye",
};
}',
[],
[],
'8.0'
],
];
}
@ -83,6 +96,36 @@ class MatchTest extends TestCase
false,
'8.0'
],
'allMatchedDefaultImpossible' => [
'<?php
function foo() : string {
$a = rand(0, 1) ? "a" : "b";
return match ($a) {
"a" => "hello",
"b" => "goodbye",
default => "impossible",
};
}',
'error_message' => 'TypeDoesNotContainType',
[],
false,
'8.0'
],
'allMatchedAnotherImpossible' => [
'<?php
function foo() : string {
$a = rand(0, 1) ? "a" : "b";
return match ($a) {
"a" => "hello",
"b" => "goodbye",
"c" => "impossible",
};
}',
'error_message' => 'TypeDoesNotContainType',
[],
false,
'8.0'
],
];
}
}