From 0bd8b03f7a5acf4ed26afd3fdfb0d7bef08adec9 Mon Sep 17 00:00:00 2001 From: orklah Date: Wed, 14 Jul 2021 12:49:46 +0200 Subject: [PATCH] Fix usage of gettype in a switch with closed resource --- .../Statements/Expression/AssertionFinder.php | 20 +++++++++++++++++-- tests/FunctionCallTest.php | 9 +++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index 389c9de19..1d667e98d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -2381,7 +2381,15 @@ class AssertionFinder } } else { if ($var_name && $var_type) { - $if_types[$var_name] = [['!' . $var_type]]; + if ($var_type === 'class@anonymous') { + $if_types[$var_name] = [['!=object']]; + } elseif ($var_type === 'resource (closed)') { + $if_types[$var_name] = [['!closed-resource']]; + } elseif (substr($var_type, 0, 10) === 'resource (') { + $if_types[$var_name] = [['!=resource']]; + } else { + $if_types[$var_name] = [['!' . $var_type]]; + } } } @@ -3054,7 +3062,15 @@ class AssertionFinder } } else { if ($var_name && $var_type) { - $if_types[$var_name] = [[$var_type]]; + if ($var_type === 'class@anonymous') { + $if_types[$var_name] = [['=object']]; + } elseif ($var_type === 'resource (closed)') { + $if_types[$var_name] = [['closed-resource']]; + } elseif (substr($var_type, 0, 10) === 'resource (') { + $if_types[$var_name] = [['=resource']]; + } else { + $if_types[$var_name] = [[$var_type]]; + } } } diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index 47135e083..59e94e012 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -792,6 +792,15 @@ class FunctionCallTest extends TestCase if ($t === "object") {} }', ], + 'getTypeSwitchClosedResource' => [ + ' [ '