1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +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:
SignpostMarv 2020-05-18 18:42:47 +01:00 committed by GitHub
parent 21798efed2
commit 911ac2d363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -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;

View File

@ -718,7 +718,7 @@ class ReturnTypeTest extends TestCase
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
',
'assertions' => [
'$res' => 'iterable<mixed, string>',
'$res' => 'iterable<mixed, numeric-string>',
],
],
'infersArrowClosureReturnTypes' => [
@ -756,7 +756,7 @@ class ReturnTypeTest extends TestCase
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
',
'assertions' => [
'$res' => 'iterable<mixed, string>',
'$res' => 'iterable<mixed, numeric-string>',
],
],
'infersCallableReturnTypes' => [
@ -778,7 +778,7 @@ class ReturnTypeTest extends TestCase
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
',
'assertions' => [
'$res' => 'iterable<mixed, string>',
'$res' => 'iterable<mixed, numeric-string>',
],
],
'infersCallableReturnTypesWithPartialTypehinting' => [
@ -800,7 +800,7 @@ class ReturnTypeTest extends TestCase
$res = map(function(int $i): string { return (string) $i; })([1,2,3]);
',
'assertions' => [
'$res' => 'iterable<mixed, string>',
'$res' => 'iterable<mixed, numeric-string>',
],
],
'mixedAssignmentWithUnderscore' => [

View File

@ -693,6 +693,24 @@ class ValueTest extends TestCase
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;
}'
],
];
}