Skip to content

Commit

Permalink
Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed May 3, 2022
1 parent 6b57ce2 commit 72a8920
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
6 changes: 5 additions & 1 deletion conf/config.neon
Expand Up @@ -1175,14 +1175,18 @@ services:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\DateTimeImmutableModifyReturnTypeExtension
class: PHPStan\Type\Php\DateTimeModifyReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
arguments:
dateTimeClass: \DateTime

-
class: PHPStan\Type\Php\DateTimeModifyReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
arguments:
dateTimeClass: \DateTimeImmutable

-
class: PHPStan\Type\Php\DateTimeConstructorThrowTypeExtension
Expand Down
15 changes: 0 additions & 15 deletions src/Type/Php/DateTimeImmutableModifyReturnTypeExtension.php

This file was deleted.

17 changes: 15 additions & 2 deletions src/Type/Php/DateTimeModifyReturnTypeExtension.php
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Php;

use DateTime;
use DateTimeInterface;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
Expand All @@ -18,9 +19,18 @@
class DateTimeModifyReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

/** @var class-string<DateTimeInterface> */
private $dateTimeClass;

/** @param class-string<DateTimeInterface> $dateTimeClass */
public function construct($dateTimeClass = DateTime::class)
{
$this->dateTimeClass = $dateTimeClass;
}

public function getClass(): string
{
return DateTime::class;
return $this->dateTimeClass;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
Expand All @@ -31,6 +41,9 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (count($methodCall->getArgs()) < 1) {
return $defaultReturnType;
}

$valueType = $scope->getType($methodCall->getArgs()[0]->value);
$constantStrings = TypeUtils::getConstantStrings($valueType);
Expand All @@ -39,7 +52,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
$hasDateTime = false;

foreach ($constantStrings as $constantString) {
if ((new DateTime())->modify($constantString->getValue()) === false) {
if (@(new DateTime())->modify($constantString->getValue()) === false) {
$hasFalse = true;
} else {
$hasDateTime = true;
Expand Down

0 comments on commit 72a8920

Please sign in to comment.