Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DateInterval extension #520

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,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) {

}
}

}