From cbd992b7e3feeeaddae231ed1b57aecd04973290 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 3 May 2022 15:12:32 +0200 Subject: [PATCH] Rework --- conf/config.neon | 6 +++++- ...teTimeImmutableModifyReturnTypeExtension.php | 15 --------------- .../Php/DateTimeModifyReturnTypeExtension.php | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 18 deletions(-) delete mode 100644 src/Type/Php/DateTimeImmutableModifyReturnTypeExtension.php diff --git a/conf/config.neon b/conf/config.neon index 17bc45bc240..be45a28f2d6 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -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 diff --git a/src/Type/Php/DateTimeImmutableModifyReturnTypeExtension.php b/src/Type/Php/DateTimeImmutableModifyReturnTypeExtension.php deleted file mode 100644 index 70e35621803..00000000000 --- a/src/Type/Php/DateTimeImmutableModifyReturnTypeExtension.php +++ /dev/null @@ -1,15 +0,0 @@ - */ + private $dateTimeClass; + + /** @param class-string $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 @@ -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); @@ -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;