Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
verfriemelt-dot-org committed Dec 17, 2022
1 parent 4ef03b5 commit 5d52f9d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Type/Php/DateIntervalDynamicReturnTypeExtension.php
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use x\boo;
use function array_map;
use function count;
use function gettype;
Expand Down Expand Up @@ -40,21 +41,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);
}

Expand Down

0 comments on commit 5d52f9d

Please sign in to comment.