-
Notifications
You must be signed in to change notification settings - Fork 428
/
DatePeriodConstructorReturnTypeExtension.php
61 lines (50 loc) · 1.58 KB
/
DatePeriodConstructorReturnTypeExtension.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
54
55
56
57
58
59
60
61
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use DatePeriod;
use DateTimeInterface;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
class DatePeriodConstructorReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{
public function getClass(): string
{
return DatePeriod::class;
}
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === '__construct';
}
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
{
$thirdArgType = null;
if (isset($methodCall->getArgs()[2])) {
$thirdArgType = $scope->getType($methodCall->getArgs()[2]->value);
}
if (!$thirdArgType instanceof Type) {
return new GenericObjectType(DatePeriod::class, [
new NullType(),
new IntegerType(),
]);
}
if ((new ObjectType(DateTimeInterface::class))->isSuperTypeOf($thirdArgType)->yes()) {
return new GenericObjectType(DatePeriod::class, [
new ObjectType(DateTimeInterface::class),
new NullType(),
]);
}
if ((new IntegerType())->isSuperTypeOf($thirdArgType)->yes()) {
return new GenericObjectType(DatePeriod::class, [
new ObjectType(DateTimeInterface::class),
new NullType(),
]);
}
return new ObjectType(DatePeriod::class);
}
}