-
Notifications
You must be signed in to change notification settings - Fork 650
/
DateTimeModifyReturnTypeProvider.php
61 lines (52 loc) · 1.81 KB
/
DateTimeModifyReturnTypeProvider.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
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Union;
class DateTimeModifyReturnTypeProvider implements MethodReturnTypeProviderInterface
{
public static function getClassLikeNames(): array
{
return ['DateTime', 'DateTimeImmutable'];
}
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Union
{
$statements_source = $event->getSource();
$call_args = $event->getCallArgs();
$method_name_lowercase = $event->getMethodNameLowercase();
if (
!$statements_source instanceof StatementsAnalyzer
|| $method_name_lowercase !== 'modify'
|| !isset($call_args[0])
) {
return null;
}
$first_arg = $call_args[0]->value;
$first_arg_type = $statements_source->node_data->getType($first_arg);
if (!$first_arg_type) {
return null;
}
$has_date_time = false;
$has_false = false;
foreach ($first_arg_type->getAtomicTypes() as $type_part) {
if (!$type_part instanceof TLiteralString) {
return null;
}
if (@(new \DateTime())->modify($type_part->value) === false) {
$has_false = true;
} else {
$has_date_time = true;
}
}
if ($has_false && !$has_date_time) {
return Type::getFalse();
}
if ($has_date_time && !$has_false) {
return Type::parseString($event->getFqClasslikeName());
}
return null;
}
}