Skip to content

Commit b911d18

Browse files
Simplify
1 parent b922946 commit b911d18

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

src/Type/Php/DateIntervalFormatDynamicReturnTypeExtension.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\DependencyInjection\AutowiredService;
99
use PHPStan\Reflection\MethodReflection;
10-
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
10+
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
1111
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
12-
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
13-
use PHPStan\Type\Accessory\AccessoryNumericStringType;
14-
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
12+
use PHPStan\Type\Constant\ConstantStringType;
1513
use PHPStan\Type\DynamicMethodReturnTypeExtension;
14+
use PHPStan\Type\GeneralizePrecision;
1615
use PHPStan\Type\IntersectionType;
1716
use PHPStan\Type\StringType;
1817
use PHPStan\Type\Type;
@@ -53,37 +52,18 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
5352
return null;
5453
}
5554

56-
// The worst case scenario for the non-falsy-string check is that every number are 0.
55+
// The worst case scenario for the non-falsy-string check is that every number is 0.
5756
$dateInterval = new DateInterval('P0D');
5857

5958
$possibleReturnTypes = [];
6059
foreach ($constantStrings as $string) {
6160
$value = $dateInterval->format($string->getValue());
62-
63-
$accessories = [];
64-
if (is_numeric($value)) {
65-
$accessories[] = new AccessoryNumericStringType();
66-
}
67-
if ($value !== '0' && $value !== '') {
68-
$accessories[] = new AccessoryNonFalsyStringType();
69-
} elseif ($value !== '') {
70-
$accessories[] = new AccessoryNonEmptyStringType();
71-
}
72-
if (strtolower($value) === $value) {
73-
$accessories[] = new AccessoryLowercaseStringType();
74-
}
75-
if (strtoupper($value) === $value) {
76-
$accessories[] = new AccessoryUppercaseStringType();
77-
}
78-
79-
if (count($accessories) === 0) {
80-
return null;
81-
}
82-
83-
$possibleReturnTypes[] = new IntersectionType([new StringType(), ...$accessories]);
61+
$possibleReturnTypes[] = new ConstantStringType($value);
8462
}
8563

86-
return TypeCombinator::union(...$possibleReturnTypes);
64+
$result = TypeCombinator::union(...$possibleReturnTypes)->generalize(GeneralizePrecision::moreSpecific());
65+
66+
return TypeCombinator::remove($result, new AccessoryLiteralStringType());
8767
}
8868

8969
}

0 commit comments

Comments
 (0)