From 295a93dff6ff8d266d3db686c63326f574f7436f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B2=A0=5F=E0=B2=A0?= Date: Sat, 17 Dec 2022 22:32:42 +0100 Subject: [PATCH] simplify code --- .../DateIntervalDynamicReturnTypeExtension.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php b/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php index a59b975746..aa9dc2c1d5 100644 --- a/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php +++ b/src/Type/Php/DateIntervalDynamicReturnTypeExtension.php @@ -7,13 +7,10 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; use PHPStan\Type\Constant\ConstantBooleanType; -use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; -use function array_map; use function count; -use function gettype; use function in_array; class DateIntervalDynamicReturnTypeExtension @@ -40,21 +37,21 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, $strings = $scope->getType($arguments[0]->value)->getConstantStrings(); - $possibleReturnTypes = array_map( - static fn (ConstantStringType $s): string => gettype(@DateInterval::createFromDateString($s->getValue())), - $strings, - ); + $possibleReturnTypes = []; + foreach ($strings as $string) { + $possibleReturnTypes[] = @DateInterval::createFromDateString($string->getValue()) instanceof DateInterval ? DateInterval::class : false; + } // the error case, when wrong types are passed if (count($possibleReturnTypes) === 0) { return null; } - if (in_array('boolean', $possibleReturnTypes, true) && in_array('object', $possibleReturnTypes, true)) { + if (in_array(false, $possibleReturnTypes, true) && in_array(DateInterval::class, $possibleReturnTypes, true)) { return null; } - if (in_array('boolean', $possibleReturnTypes, true)) { + if (in_array(false, $possibleReturnTypes, true)) { return new ConstantBooleanType(false); }