1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix date return type provider

This commit is contained in:
Daniil Gentili 2023-04-25 11:41:39 +02:00
parent 1a4395658e
commit 481a5bd61d
2 changed files with 17 additions and 12 deletions

View File

@ -8,10 +8,13 @@ use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Union;
use function array_values;
use function preg_match;
use function date;
use function is_numeric;
/**
* @internal
@ -35,21 +38,19 @@ class DateReturnTypeProvider implements FunctionReturnTypeProviderInterface
$call_args = $event->getCallArgs();
$format_type = Type::getString();
if (isset($call_args[0])) {
$type = $source->node_data->getType($call_args[0]->value);
if ($type !== null && $type->isSingle()) {
$atomic_type = array_values($type->getAtomicTypes())[0];
if ($atomic_type instanceof Type\Atomic\TLiteralString
&& ($format_val = $atomic_type->value)
&& preg_match('/[djNwzWmntLoYyBgGhHisuvZUI]+/', $format_val)
) {
return Type::getNumericString();
}
if ($type !== null
&& $type->isSingleStringLiteral()
&& is_numeric(date($type->getSingleStringLiteral()->value))
) {
$format_type = Type::getNumericString();
}
}
if (!isset($call_args[1])) {
return Type::getString();
return $format_type;
}
$type = $source->node_data->getType($call_args[1]->value);
@ -57,10 +58,12 @@ class DateReturnTypeProvider implements FunctionReturnTypeProviderInterface
$atomic_type = array_values($type->getAtomicTypes())[0];
if ($atomic_type instanceof Type\Atomic\TNumeric
|| $atomic_type instanceof Type\Atomic\TInt
|| $atomic_type instanceof TLiteralInt
|| ($atomic_type instanceof TLiteralString && is_numeric($atomic_type->value))
) {
return Type::getString();
return $format_type;
}
}
return Type::combineUnionTypes(Type::getString(), Type::getFalse());
return Type::combineUnionTypes($format_type, Type::getFalse());
}
}

View File

@ -1732,6 +1732,7 @@ class FunctionCallTest extends TestCase
'code' => '<?php
$y = date("Y");
$ym = date("Ym");
$y_m = date("Y-m");
$m = date("m");
$F = date("F");
$y2 = date("Y", 10000);
@ -1749,6 +1750,7 @@ class FunctionCallTest extends TestCase
'assertions' => [
'$y===' => 'numeric-string',
'$ym===' => 'numeric-string',
'$y_m===' => 'string',
'$m===' => 'numeric-string',
'$F===' => 'string',
'$y2===' => 'numeric-string',