-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateTimeSubMethodThrowTypeExtension
- Loading branch information
1 parent
2e27573
commit 229b9ca
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodThrowTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use function count; | ||
use function in_array; | ||
|
||
final class DateTimeSubMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'sub' | ||
&& in_array($methodReflection->getDeclaringClass()->getName(), [DateTime::class, DateTimeImmutable::class], true); | ||
} | ||
|
||
public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
if (count($methodCall->getArgs()) === 0) { | ||
return null; | ||
} | ||
|
||
if (!$this->phpVersion->hasDateTimeExceptions()) { | ||
return null; | ||
} | ||
|
||
return new ObjectType('DateMalformedStringException'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Bug11503; | ||
|
||
class Foo { | ||
public function test() { | ||
$date = new \DateTimeImmutable(); | ||
$interval = new \DateInterval('P1M'); | ||
$date->sub($interval); | ||
$date->add($interval); | ||
$date->modify('+1 day'); | ||
$date->setDate(2024, 8, 13); | ||
$date->setISODate(2024, 1); | ||
$date->setTime(0, 0, 0, 0); | ||
$date->setTimestamp(1); | ||
$zone = new \DateTimeZone('UTC'); | ||
$date->setTimezone($zone); | ||
} | ||
} |