Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateFunctionReturnTypeExtension: support more precise single-char date-formats #721

Merged
merged 4 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Type/Php/DateFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -31,10 +32,32 @@ public function getTypeFromFunctionCall(
}
$argType = $scope->getType($functionCall->getArgs()[0]->value);
$constantStrings = TypeUtils::getConstantStrings($argType);

if (count($constantStrings) === 0) {
return new StringType();
}

if (count($constantStrings) === 1) {
$constantString = $constantStrings[0]->getValue();

// see see https://www.php.net/manual/en/datetime.format.php
switch($constantString) {
case 'j': return IntegerRangeType::fromInterval(1, 31);
case 'N': return IntegerRangeType::fromInterval(1, 7);
case 'w': return IntegerRangeType::fromInterval(0, 6);
case 'z': return IntegerRangeType::fromInterval(0, 365);
case 'W': return IntegerRangeType::fromInterval(1, 53);
case 'n': return IntegerRangeType::fromInterval(1, 12);
case 't': return IntegerRangeType::fromInterval(28, 31);
case 'L': return IntegerRangeType::fromInterval(0, 1);
case 'o': return IntegerRangeType::fromInterval(1, 9999);
case 'Y': return IntegerRangeType::fromInterval(1, 9999);
case 'g': return IntegerRangeType::fromInterval(1, 12);
case 'G': return IntegerRangeType::fromInterval(0, 23);
case 'I': return IntegerRangeType::fromInterval(0, 1);
}
}

foreach ($constantStrings as $constantString) {
$formattedDate = date($constantString->getValue());
if (!is_numeric($formattedDate)) {
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function dataFileAsserts(): iterable

require_once __DIR__ . '/data/instanceof.php';

yield from $this->gatherAssertTypes(__DIR__ . '/data/date.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/instanceof.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/integer-range-types.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/random-int.php');
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-2899.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Foo

public function doFoo(string $s, $mixed)
{
assertType('numeric-string', date('Y'));
assertType('int<1, 9999>', date('Y'));
assertType('string', date('Y.m.d'));
assertType('string', date($s));
assertType('string', date($mixed));
Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Analyser/data/date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace DateFunction;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* see https://www.php.net/manual/en/datetime.format.php
*/
public function doFoo()
{
assertType('int<1, 31>', date('j'));
assertType('int<1, 7>', date('N'));
assertType('int<0, 6>', date('w'));
assertType('int<0, 365>', date('z'));

assertType('int<1, 53>', date('W'));

assertType('int<1, 12>', date('n'));
assertType('int<28, 31>', date('t'));

assertType('int<0, 1>', date('L'));

// we assume a max year of 9999
assertType('int<1, 9999>', date('o'));
assertType('int<1, 9999>', date('Y'));
staabm marked this conversation as resolved.
Show resolved Hide resolved

assertType('int<1, 12>', date('g'));
assertType('int<0, 23>', date('G'));

assertType('int<0, 1>', date('I'));
}

}