Skip to content

Commit

Permalink
Add DateInterval extension
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and ondrejmirtes committed May 17, 2021
1 parent 9c5ddc5 commit 84ea0e1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\DateIntervalConstructorThrowTypeExtension
tags:
- phpstan.dynamicStaticMethodThrowTypeExtension

-
class: PHPStan\Type\Php\DateTimeDynamicReturnTypeExtension
tags:
Expand Down
44 changes: 44 additions & 0 deletions src/Type/Php/DateIntervalConstructorThrowTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use DateInterval;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodThrowTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;

class DateIntervalConstructorThrowTypeExtension implements DynamicStaticMethodThrowTypeExtension
{

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === '__construct' && $methodReflection->getDeclaringClass()->getName() === DateInterval::class;
}

public function getThrowTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
if (count($methodCall->args) === 0) {
return $methodReflection->getThrowType();
}

$arg = $methodCall->args[0]->value;
$constantStrings = TypeUtils::getConstantStrings($scope->getType($arg));
if (count($constantStrings) === 0) {
return $methodReflection->getThrowType();
}

foreach ($constantStrings as $constantString) {
try {
new \DateInterval($constantString->getValue());
} catch (\Exception $e) { // phpcs:ignore
return $methodReflection->getThrowType();
}
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function testRule(): void
'Dead catch - Exception is never thrown in the try block.',
180,
],
[
'Dead catch - Exception is never thrown in the try block.',
212,
],
]);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/unthrown-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,35 @@ public function doBaz(string $s): void
}

}

class TestDateInterval
{

public function doFoo(): void
{
try {
new \DateInterval('invalid format');
} catch (\Exception $e) {

}
}

public function doBar(): void
{
try {
new \DateInterval('P10D');
} catch (\Exception $e) {

}
}

public function doBaz(string $s): void
{
try {
new \DateInterval($s);
} catch (\Exception $e) {

}
}

}

0 comments on commit 84ea0e1

Please sign in to comment.