From 7023855fb3e8652417c1ec27ead2b33b41226ce4 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:19:53 +0100 Subject: [PATCH] add scalar & numeric handling for all cases where appropriate and ensure no more generic types being added for int/float (previous commit) --- .../ReturnTypeProvider/FilterUtils.php | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterUtils.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterUtils.php index dfce3b37b..5c32126de 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterUtils.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/FilterUtils.php @@ -33,6 +33,7 @@ use Psalm\Type\Atomic\TNonFalsyString; use Psalm\Type\Atomic\TNull; use Psalm\Type\Atomic\TNumeric; use Psalm\Type\Atomic\TNumericString; +use Psalm\Type\Atomic\TScalar; use Psalm\Type\Atomic\TString; use Psalm\Type\Atomic\TTrue; use Psalm\Type\Union; @@ -42,6 +43,7 @@ use function array_diff; use function array_keys; use function array_merge; use function filter_var; +use function get_class; use function implode; use function in_array; use function preg_match; @@ -919,7 +921,11 @@ final class FilterUtils $filter_types[] = new TFloat(); } - if ($atomic_type instanceof TMixed || $atomic_type instanceof TNumeric) { + // only these specific classes, not any class that extends either + // to avoid matching already better handled cases from above, e.g. float is numeric and scalar + if ($atomic_type instanceof TMixed + || get_class($atomic_type) === TNumeric::class + || get_class($atomic_type) === TScalar::class) { $filter_types[] = new TFloat(); } @@ -967,7 +973,9 @@ final class FilterUtils if ($atomic_type instanceof TMixed || $atomic_type instanceof TString || $atomic_type instanceof TInt - || $atomic_type instanceof TFloat) { + || $atomic_type instanceof TFloat + || $atomic_type instanceof TNumeric + || $atomic_type instanceof TScalar) { $filter_types[] = new TBool(); } @@ -1109,7 +1117,9 @@ final class FilterUtils $filter_types[] = $int_type; } - if ($atomic_type instanceof TMixed || $atomic_type instanceof TNumeric) { + if ($atomic_type instanceof TMixed + || get_class($atomic_type) === TNumeric::class + || get_class($atomic_type) === TScalar::class) { $filter_types[] = $int_type; } @@ -1130,9 +1140,7 @@ final class FilterUtils $filter_types[] = $atomic_type; } elseif ($atomic_type instanceof TString) { $filter_types[] = new TNonFalsyString(); - } - - if ($atomic_type instanceof TMixed) { + } elseif ($atomic_type instanceof TMixed || $atomic_type instanceof TScalar) { $filter_types[] = new TNonFalsyString(); } @@ -1160,6 +1168,7 @@ final class FilterUtils || $atomic_type instanceof TInt || $atomic_type instanceof TFloat || $atomic_type instanceof TNumeric + || $atomic_type instanceof TScalar || $atomic_type instanceof TMixed) { $filter_types[] = new TString(); } @@ -1184,11 +1193,10 @@ final class FilterUtils } else { $filter_types[] = $atomic_type; } - } - - if ($atomic_type instanceof TMixed + } elseif ($atomic_type instanceof TMixed || $atomic_type instanceof TInt - || $atomic_type instanceof TFloat) { + || $atomic_type instanceof TFloat + || $atomic_type instanceof TScalar) { $filter_types[] = $string_type; } @@ -1231,7 +1239,7 @@ final class FilterUtils continue; } - if ($atomic_type instanceof TMixed) { + if ($atomic_type instanceof TMixed || $atomic_type instanceof TScalar) { $filter_types[] = new TString(); } @@ -1311,7 +1319,7 @@ final class FilterUtils continue; } - if ($atomic_type instanceof TMixed) { + if ($atomic_type instanceof TMixed || $atomic_type instanceof TScalar) { $filter_types[] = new TString(); } @@ -1330,7 +1338,7 @@ final class FilterUtils continue; } - if ($atomic_type instanceof TMixed) { + if ($atomic_type instanceof TMixed || $atomic_type instanceof TScalar) { $filter_types[] = new TString(); } @@ -1387,7 +1395,7 @@ final class FilterUtils continue; } - if ($atomic_type instanceof TMixed) { + if ($atomic_type instanceof TMixed || $atomic_type instanceof TScalar) { $filter_types[] = new TNumericString(); $filter_types[] = Type::getAtomicStringFromLiteral(''); }