mirror of
https://github.com/danog/psalm.git
synced 2024-12-02 09:37:59 +01:00
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
This commit is contained in:
parent
21798efed2
commit
911ac2d363
@ -1707,6 +1707,15 @@ class ExpressionAnalyzer
|
|||||||
while ($atomic_types) {
|
while ($atomic_types) {
|
||||||
$atomic_type = \array_pop($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) {
|
if ($atomic_type instanceof TString) {
|
||||||
$valid_strings[] = $atomic_type;
|
$valid_strings[] = $atomic_type;
|
||||||
continue;
|
continue;
|
||||||
|
@ -718,7 +718,7 @@ class ReturnTypeTest extends TestCase
|
|||||||
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
||||||
',
|
',
|
||||||
'assertions' => [
|
'assertions' => [
|
||||||
'$res' => 'iterable<mixed, string>',
|
'$res' => 'iterable<mixed, numeric-string>',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'infersArrowClosureReturnTypes' => [
|
'infersArrowClosureReturnTypes' => [
|
||||||
@ -756,7 +756,7 @@ class ReturnTypeTest extends TestCase
|
|||||||
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
||||||
',
|
',
|
||||||
'assertions' => [
|
'assertions' => [
|
||||||
'$res' => 'iterable<mixed, string>',
|
'$res' => 'iterable<mixed, numeric-string>',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'infersCallableReturnTypes' => [
|
'infersCallableReturnTypes' => [
|
||||||
@ -778,7 +778,7 @@ class ReturnTypeTest extends TestCase
|
|||||||
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
||||||
',
|
',
|
||||||
'assertions' => [
|
'assertions' => [
|
||||||
'$res' => 'iterable<mixed, string>',
|
'$res' => 'iterable<mixed, numeric-string>',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'infersCallableReturnTypesWithPartialTypehinting' => [
|
'infersCallableReturnTypesWithPartialTypehinting' => [
|
||||||
@ -800,7 +800,7 @@ class ReturnTypeTest extends TestCase
|
|||||||
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
|
||||||
',
|
',
|
||||||
'assertions' => [
|
'assertions' => [
|
||||||
'$res' => 'iterable<mixed, string>',
|
'$res' => 'iterable<mixed, numeric-string>',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'mixedAssignmentWithUnderscore' => [
|
'mixedAssignmentWithUnderscore' => [
|
||||||
|
@ -693,6 +693,24 @@ class ValueTest extends TestCase
|
|||||||
return 5;
|
return 5;
|
||||||
}',
|
}',
|
||||||
],
|
],
|
||||||
|
'numericStringCastFromInt' => [
|
||||||
|
'<?php
|
||||||
|
/**
|
||||||
|
* @return numeric-string
|
||||||
|
*/
|
||||||
|
function makeNumStringFromInt(int $v) {
|
||||||
|
return (string) $v;
|
||||||
|
}',
|
||||||
|
],
|
||||||
|
'numericStringCastFromFloat' => [
|
||||||
|
'<?php
|
||||||
|
/**
|
||||||
|
* @return numeric-string
|
||||||
|
*/
|
||||||
|
function makeNumStringFromFloat(float $v) {
|
||||||
|
return (string) $v;
|
||||||
|
}'
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user