Skip to content

Commit 4e32eaa

Browse files
committed
Update DateIntervalDynamicReturnTypeExtension to handle 8.3 exceptions
1 parent 7d17216 commit 4e32eaa

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/Type/Php/DateIntervalDynamicReturnTypeExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr\StaticCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\DependencyInjection\AutowiredService;
9+
use PHPStan\Php\PhpVersion;
910
use PHPStan\Reflection\MethodReflection;
1011
use PHPStan\Type\Constant\ConstantBooleanType;
1112
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
@@ -20,6 +21,10 @@ final class DateIntervalDynamicReturnTypeExtension
2021
implements DynamicStaticMethodReturnTypeExtension
2122
{
2223

24+
public function __construct(private PhpVersion $phpVersion)
25+
{
26+
}
27+
2328
public function getClass(): string
2429
{
2530
return DateInterval::class;
@@ -45,6 +50,9 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
4550
try {
4651
$result = @DateInterval::createFromDateString($string->getValue());
4752
} catch (Throwable) {
53+
if ($this->phpVersion->hasDateTimeExceptions()) {
54+
return new ObjectType('DateMalformedIntervalStringException');
55+
}
4856
$possibleReturnTypes[] = false;
4957
continue;
5058
}

tests/PHPStan/Rules/Exceptions/MissingCheckedExceptionInMethodThrowsRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public function testRule(): void
6666
'Method MissingExceptionMethodThrows\Foo::dateTimeModifyDoesThrows() throws checked exception DateMalformedStringException but it\'s missing from the PHPDoc @throws tag.',
6767
122,
6868
];
69+
$errors[] = [
70+
'Method MissingExceptionMethodThrows\Foo::dateIntervalFromStringDoesThrows() throws checked exception DateMalformedIntervalStringException but it\'s missing from the PHPDoc @throws tag.',
71+
127,
72+
];
6973
}
7074
$this->analyse([__DIR__ . '/data/missing-exception-method-throws.php'], $errors);
7175
}

tests/PHPStan/Rules/Exceptions/data/missing-exception-method-throws.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,14 @@ public function dateTimeModifyDoesThrows(\DateTime $dt, \DateTimeImmutable $dti,
122122
$dti->modify($m);
123123
}
124124

125+
public function dateIntervalFromStringDoesThrows(string $i): void
126+
{
127+
\DateInterval::createFromDateString($i);
128+
}
129+
130+
public function dateIntervalFromStringDoeNotThrow(): void
131+
{
132+
\DateInterval::createFromDateString('1 day');
133+
}
134+
125135
}

0 commit comments

Comments
 (0)