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

Static analyse if date_create_from_format will fail or not #502

Merged
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
5 changes: 5 additions & 0 deletions conf/config.neon
Expand Up @@ -962,6 +962,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\DateTimeDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\DsMapDynamicReturnTypeExtension
tags:
Expand Down
43 changes: 43 additions & 0 deletions src/Type/Php/DateTimeDynamicReturnTypeExtension.php
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace PHPStan\Type\Php;

use DateTime;
use DateTimeImmutable;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;

class DateTimeDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['date_create_from_format', 'date_create_immutable_from_format'], true);
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();

$format = $scope->getType($functionCall->args[0]->value);
pascalheidmann marked this conversation as resolved.
Show resolved Hide resolved
$datetime = $scope->getType($functionCall->args[1]->value);

if (!$format instanceof ConstantStringType || !$datetime instanceof ConstantStringType) {
return $defaultReturnType;
}

$formatValue = TypeUtils::getConstantStrings($format)[0]->getValue();
pascalheidmann marked this conversation as resolved.
Show resolved Hide resolved
$datetimeValue = TypeUtils::getConstantStrings($datetime)[0]->getValue();
pascalheidmann marked this conversation as resolved.
Show resolved Hide resolved
$isValid = (DateTime::createFromFormat($formatValue, $datetimeValue) !== false);

$className = $functionReflection->getName() === 'date_create_from_format' ? DateTime::class : DateTimeImmutable::class;
return $isValid ? new ObjectType($className) : new ConstantBooleanType(false);
}
}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -381,6 +381,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/type-aliases.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4650.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2906.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeDynamicReturnTypes.php');
}

/**
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPStan/Analyser/data/DateTimeDynamicReturnTypes.php
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace DateTimeDynamicReturnTypes;

use DateTime;
use DateTimeImmutable;
use function date_create_from_format;
use function PHPStan\Testing\assertType;

class Foo
{
public function createDynamic(string $format, string $datetime): void {
assertType('DateTime|false', date_create_from_format($format, $datetime));
assertType('DateTimeImmutable|false', date_create_immutable_from_format($format, $datetime));

assertType('DateTime|false', DateTime::createFromFormat($format, $datetime));
assertType('DateTimeImmutable|false', DateTimeImmutable::createFromFormat($format, $datetime));
}

public function staticInvalidFormat(): void {
assertType('false', date_create_from_format('Foobar', '2022-02-20'));
assertType('false', date_create_immutable_from_format('Foobar', '2022-02-20'));

assertType('false', DateTime::createFromFormat('Foobar', '2022-02-20'));
assertType('false', DateTimeImmutable::createFromFormat('Foobar', '2022-02-20'));
}

public function staticInvalidDatetime(): void {
assertType('false', date_create_from_format('Y-m-d', '2022/02/20'));
assertType('false', date_create_immutable_from_format('Y-m-d', '2022/02/20'));

assertType('false', DateTime::createFromFormat('Y-m-d', '2022/02/20'));
assertType('false', DateTimeImmutable::createFromFormat('Y-m-d', '2022/02/20'));
}

public function staticValidStrings(): void {
assertType('DateTime', date_create_from_format('Y-m-d', '2020-10-12'));
assertType('DateTimeImmutable', date_create_immutable_from_format('Y-m-d', '2020-10-12'));

assertType('DateTime', DateTime::createFromFormat('Y-m-d', '2020-10-12'));
assertType('DateTimeImmutable', DateTimeImmutable::createFromFormat('Y-m-d', '2020-10-12'));
}
}