From 56c47c2b7f37b9d62bd1e35d783ae78dfacda7db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B2=A0=5F=E0=B2=A0?= Date: Wed, 14 Dec 2022 08:14:18 +0100 Subject: [PATCH] add more testcases and cleanup --- ...DateIntervalDynamicReturnTypeExtension.php | 42 ++++++++++--------- tests/PHPStan/Analyser/data/bug-8442.php | 12 +++++- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php b/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php index 32710f2a1a8..4bc66928d89 100644 --- a/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php +++ b/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php @@ -1,4 +1,4 @@ -getName() === 'createFromDateString'; } - public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type + public function getTypeFromStaticMethodCall( MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope ): ?Type { - $strings = $scope->getType($methodCall->getArgs()[0]->value)->getConstantStrings(); - - if ($strings === []) { - return null; + $strings = $scope->getType( $methodCall->getArgs()[ 0 ]->value )->getConstantStrings(); + + if ( $strings === [] ) { + return null; } - - if (count($strings) === 1) { - if (DateInterval::createFromDateString($dateTimeString->getValue()) === false) { - return new ConstantBooleanType(false); - } - return new ObjectType(DateInterval::class); + + if ( count( $strings ) === 1 ) { + if ( DateInterval::createFromDateString( $strings[ 0 ]->getValue() ) === false ) { + return new ConstantBooleanType( false ); + } + + return new ObjectType( DateInterval::class ); } - - foreach($strings as $string) { - if (DateInterval::createFromDateString($dateTimeString->getValue()) === false) { - return null; - } + + foreach ( $strings as $string ) { + if ( DateInterval::createFromDateString( $string->getValue() ) === false ) { + return null; + } } - return new ObjectType(DateInterval::class); + return new ObjectType( DateInterval::class ); } } diff --git a/tests/PHPStan/Analyser/data/bug-8442.php b/tests/PHPStan/Analyser/data/bug-8442.php index 0b654ca2feb..f3733a03add 100644 --- a/tests/PHPStan/Analyser/data/bug-8442.php +++ b/tests/PHPStan/Analyser/data/bug-8442.php @@ -10,11 +10,19 @@ function () { assertType('DateInterval', DateInterval::createFromDateString('1 Day')); if (rand(0,1)) { - $interval = 'P1Y'; + $interval = '1 day'; } else { - $interval = 'P2Y'; + $interval = '2 day'; } assertType('DateInterval', DateInterval::createFromDateString($interval)); + + if (rand(0,1)) { + $interval = 'foo'; + } else { + $interval = '2 day'; + } + + assertType('DateInterval|false', DateInterval::createFromDateString($interval)); };