|
7 | 7 | use PHPStan\Analyser\Scope;
|
8 | 8 | use PHPStan\DependencyInjection\AutowiredService;
|
9 | 9 | use PHPStan\Reflection\MethodReflection;
|
10 |
| -use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
| 10 | +use PHPStan\Type\Accessory\AccessoryLowercaseStringType; |
11 | 11 | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
|
12 |
| -use PHPStan\Type\Constant\ConstantStringType; |
| 12 | +use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
| 13 | +use PHPStan\Type\Accessory\AccessoryNumericStringType; |
| 14 | +use PHPStan\Type\Accessory\AccessoryUppercaseStringType; |
13 | 15 | use PHPStan\Type\DynamicMethodReturnTypeExtension;
|
14 |
| -use PHPStan\Type\GeneralizePrecision; |
15 | 16 | use PHPStan\Type\IntersectionType;
|
16 | 17 | use PHPStan\Type\StringType;
|
17 | 18 | use PHPStan\Type\Type;
|
18 | 19 | use PHPStan\Type\TypeCombinator;
|
19 | 20 | use function count;
|
| 21 | +use function is_numeric; |
| 22 | +use function strtolower; |
| 23 | +use function strtoupper; |
20 | 24 |
|
21 | 25 | #[AutowiredService]
|
22 | 26 | final class DateIntervalFormatDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
|
@@ -57,13 +61,31 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
|
57 | 61 | $possibleReturnTypes = [];
|
58 | 62 | foreach ($constantStrings as $string) {
|
59 | 63 | $value = $dateInterval->format($string->getValue());
|
60 |
| - $possibleReturnTypes[] = new ConstantStringType($value); |
| 64 | + |
| 65 | + $accessories = []; |
| 66 | + if (is_numeric($value)) { |
| 67 | + $accessories[] = new AccessoryNumericStringType(); |
| 68 | + } |
| 69 | + if ($value !== '0' && $value !== '') { |
| 70 | + $accessories[] = new AccessoryNonFalsyStringType(); |
| 71 | + } elseif ($value !== '') { |
| 72 | + $accessories[] = new AccessoryNonEmptyStringType(); |
| 73 | + } |
| 74 | + if (strtolower($value) === $value) { |
| 75 | + $accessories[] = new AccessoryLowercaseStringType(); |
| 76 | + } |
| 77 | + if (strtoupper($value) === $value) { |
| 78 | + $accessories[] = new AccessoryUppercaseStringType(); |
| 79 | + } |
| 80 | + |
| 81 | + if (count($accessories) === 0) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + $possibleReturnTypes[] = new IntersectionType([new StringType(), ...$accessories]); |
61 | 86 | }
|
62 | 87 |
|
63 |
| - return TypeCombinator::remove( |
64 |
| - TypeCombinator::union(...$possibleReturnTypes)->generalize(GeneralizePrecision::moreSpecific()), |
65 |
| - new AccessoryLiteralStringType(), |
66 |
| - ); |
| 88 | + return TypeCombinator::union(...$possibleReturnTypes); |
67 | 89 | }
|
68 | 90 |
|
69 | 91 | }
|
0 commit comments