mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Fix date return type provider
This commit is contained in:
parent
1a4395658e
commit
481a5bd61d
@ -8,10 +8,13 @@ use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|||||||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
|
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
|
||||||
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
|
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
|
||||||
use Psalm\Type;
|
use Psalm\Type;
|
||||||
|
use Psalm\Type\Atomic\TLiteralInt;
|
||||||
|
use Psalm\Type\Atomic\TLiteralString;
|
||||||
use Psalm\Type\Union;
|
use Psalm\Type\Union;
|
||||||
|
|
||||||
use function array_values;
|
use function array_values;
|
||||||
use function preg_match;
|
use function date;
|
||||||
|
use function is_numeric;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -35,21 +38,19 @@ class DateReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
|||||||
|
|
||||||
$call_args = $event->getCallArgs();
|
$call_args = $event->getCallArgs();
|
||||||
|
|
||||||
|
$format_type = Type::getString();
|
||||||
if (isset($call_args[0])) {
|
if (isset($call_args[0])) {
|
||||||
$type = $source->node_data->getType($call_args[0]->value);
|
$type = $source->node_data->getType($call_args[0]->value);
|
||||||
if ($type !== null && $type->isSingle()) {
|
if ($type !== null
|
||||||
$atomic_type = array_values($type->getAtomicTypes())[0];
|
&& $type->isSingleStringLiteral()
|
||||||
if ($atomic_type instanceof Type\Atomic\TLiteralString
|
&& is_numeric(date($type->getSingleStringLiteral()->value))
|
||||||
&& ($format_val = $atomic_type->value)
|
) {
|
||||||
&& preg_match('/[djNwzWmntLoYyBgGhHisuvZUI]+/', $format_val)
|
$format_type = Type::getNumericString();
|
||||||
) {
|
|
||||||
return Type::getNumericString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($call_args[1])) {
|
if (!isset($call_args[1])) {
|
||||||
return Type::getString();
|
return $format_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
$type = $source->node_data->getType($call_args[1]->value);
|
$type = $source->node_data->getType($call_args[1]->value);
|
||||||
@ -57,10 +58,12 @@ class DateReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
|||||||
$atomic_type = array_values($type->getAtomicTypes())[0];
|
$atomic_type = array_values($type->getAtomicTypes())[0];
|
||||||
if ($atomic_type instanceof Type\Atomic\TNumeric
|
if ($atomic_type instanceof Type\Atomic\TNumeric
|
||||||
|| $atomic_type instanceof Type\Atomic\TInt
|
|| $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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1732,6 +1732,7 @@ class FunctionCallTest extends TestCase
|
|||||||
'code' => '<?php
|
'code' => '<?php
|
||||||
$y = date("Y");
|
$y = date("Y");
|
||||||
$ym = date("Ym");
|
$ym = date("Ym");
|
||||||
|
$y_m = date("Y-m");
|
||||||
$m = date("m");
|
$m = date("m");
|
||||||
$F = date("F");
|
$F = date("F");
|
||||||
$y2 = date("Y", 10000);
|
$y2 = date("Y", 10000);
|
||||||
@ -1749,6 +1750,7 @@ class FunctionCallTest extends TestCase
|
|||||||
'assertions' => [
|
'assertions' => [
|
||||||
'$y===' => 'numeric-string',
|
'$y===' => 'numeric-string',
|
||||||
'$ym===' => 'numeric-string',
|
'$ym===' => 'numeric-string',
|
||||||
|
'$y_m===' => 'string',
|
||||||
'$m===' => 'numeric-string',
|
'$m===' => 'numeric-string',
|
||||||
'$F===' => 'string',
|
'$F===' => 'string',
|
||||||
'$y2===' => 'numeric-string',
|
'$y2===' => 'numeric-string',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user