/
DateIntervalDynamicReturnTypeExtension.php
53 lines (42 loc) · 1.42 KB
/
DateIntervalDynamicReturnTypeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
class DateIntervalDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{
public function getClass(): string
{
return DateInterval::class;
}
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'createFromDateString';
}
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
$strings = $scope->getType($methodCall->getArgs()[0]->value)->getConstantStrings();
if ($strings === []) {
return null;
}
if (count($strings) === 1) {
if (DateInterval::createFromDateString($dateTimeString->getValue()) === false) {
return new ConstantBooleanType(false);
}
return new ObjectType(DateInterval::class);
}
foreach($strings as $string) {
if (DateInterval::createFromDateString($dateTimeString->getValue()) === false) {
return null;
}
}
return new ObjectType(DateInterval::class);
}
}