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 295a93d
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Type/Php/DateIntervalDynamicReturnTypeExtension.php
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down

0 comments on commit 295a93d

Please sign in to comment.