Skip to content

Commit 34d5241

Browse files
Fix
1 parent 7fcfbec commit 34d5241

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/Type/Php/DateIntervalFormatDynamicReturnTypeExtension.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
use PHPStan\Analyser\Scope;
88
use PHPStan\DependencyInjection\AutowiredService;
99
use PHPStan\Reflection\MethodReflection;
10-
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
10+
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
1111
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;
1315
use PHPStan\Type\DynamicMethodReturnTypeExtension;
14-
use PHPStan\Type\GeneralizePrecision;
1516
use PHPStan\Type\IntersectionType;
1617
use PHPStan\Type\StringType;
1718
use PHPStan\Type\Type;
@@ -57,13 +58,31 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
5758
$possibleReturnTypes = [];
5859
foreach ($constantStrings as $string) {
5960
$value = $dateInterval->format($string->getValue());
60-
$possibleReturnTypes[] = new ConstantStringType($value);
61+
62+
$accessories = [];
63+
if (is_numeric($value)) {
64+
$accessories[] = new AccessoryNumericStringType();
65+
}
66+
if ($value !== '0' && $value !== '') {
67+
$accessories[] = new AccessoryNonFalsyStringType();
68+
} elseif ($value !== '') {
69+
$accessories[] = new AccessoryNonEmptyStringType();
70+
}
71+
if (strtolower($value) === $value) {
72+
$accessories[] = new AccessoryLowercaseStringType();
73+
}
74+
if (strtoupper($value) === $value) {
75+
$accessories[] = new AccessoryUppercaseStringType();
76+
}
77+
78+
if (count($accessories) === 0) {
79+
return null;
80+
}
81+
82+
$possibleReturnTypes[] = new IntersectionType([new StringType(), ...$accessories]);
6183
}
6284

63-
return TypeCombinator::remove(
64-
TypeCombinator::union(...$possibleReturnTypes)->generalize(GeneralizePrecision::moreSpecific()),
65-
new AccessoryLiteralStringType(),
66-
);
85+
return TypeCombinator::union(...$possibleReturnTypes);
6786
}
6887

6988
}

0 commit comments

Comments
 (0)