From 911ac2d363f8f99a343ba20387c8547978d96218 Mon Sep 17 00:00:00 2001 From: SignpostMarv Date: Mon, 18 May 2020 18:42:47 +0100 Subject: [PATCH] Infer numeric-string from (string)$numeric (#3390) * adding test for vimeo/psalm#3370 * check if typecast will result in numeric-string * int & float as per examples in vimeo/psalm#3370 * numeric & numeric-string because they shouldn't lose numericness. * satisfy phpcs * adjusting assertions to reflect typecasting change --- .../Analyzer/Statements/ExpressionAnalyzer.php | 9 +++++++++ tests/ReturnTypeTest.php | 8 ++++---- tests/TypeReconciliation/ValueTest.php | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php index 900c17c38..4907d540f 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php @@ -1707,6 +1707,15 @@ class ExpressionAnalyzer while ($atomic_types) { $atomic_type = \array_pop($atomic_types); + if ($atomic_type instanceof TFloat + || $atomic_type instanceof TInt + || $atomic_type instanceof Type\Atomic\TNumeric + || $atomic_type instanceof Type\Atomic\TNumericString + ) { + $castable_types[] = new Type\Atomic\TNumericString(); + continue; + } + if ($atomic_type instanceof TString) { $valid_strings[] = $atomic_type; continue; diff --git a/tests/ReturnTypeTest.php b/tests/ReturnTypeTest.php index 5281d32bf..f90d3bc2e 100644 --- a/tests/ReturnTypeTest.php +++ b/tests/ReturnTypeTest.php @@ -718,7 +718,7 @@ class ReturnTypeTest extends TestCase $res = map(function(int $i): string { return (string) $i; })([1,2,3]); ', 'assertions' => [ - '$res' => 'iterable', + '$res' => 'iterable', ], ], 'infersArrowClosureReturnTypes' => [ @@ -756,7 +756,7 @@ class ReturnTypeTest extends TestCase $res = map(function(int $i): string { return (string) $i; })([1,2,3]); ', 'assertions' => [ - '$res' => 'iterable', + '$res' => 'iterable', ], ], 'infersCallableReturnTypes' => [ @@ -778,7 +778,7 @@ class ReturnTypeTest extends TestCase $res = map(function(int $i): string { return (string) $i; })([1,2,3]); ', 'assertions' => [ - '$res' => 'iterable', + '$res' => 'iterable', ], ], 'infersCallableReturnTypesWithPartialTypehinting' => [ @@ -800,7 +800,7 @@ class ReturnTypeTest extends TestCase $res = map(function(int $i): string { return (string) $i; })([1,2,3]); ', 'assertions' => [ - '$res' => 'iterable', + '$res' => 'iterable', ], ], 'mixedAssignmentWithUnderscore' => [ diff --git a/tests/TypeReconciliation/ValueTest.php b/tests/TypeReconciliation/ValueTest.php index 80daeb0de..3be8551cd 100644 --- a/tests/TypeReconciliation/ValueTest.php +++ b/tests/TypeReconciliation/ValueTest.php @@ -693,6 +693,24 @@ class ValueTest extends TestCase return 5; }', ], + 'numericStringCastFromInt' => [ + ' [ + '