-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathExpressionTypeHolder.php
More file actions
102 lines (80 loc) · 1.9 KB
/
ExpressionTypeHolder.php
File metadata and controls
102 lines (80 loc) · 1.9 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Node\Expr;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
final class ExpressionTypeHolder
{
public function __construct(
private readonly Expr $expr,
private readonly Type $type,
private readonly TrinaryLogic $certainty,
)
{
}
public static function createYes(Expr $expr, Type $type): self
{
return new self($expr, $type, TrinaryLogic::createYes());
}
public static function createMaybe(Expr $expr, Type $type): self
{
return new self($expr, $type, TrinaryLogic::createMaybe());
}
public function equalTypes(self $other): bool
{
if ($this === $other) {
return true;
}
return $this->type === $other->type || $this->type->equals($other->type);
}
public function equals(self $other): bool
{
if ($this === $other) {
return true;
}
if (!$this->certainty->equals($other->certainty)) {
return false;
}
return $this->type === $other->type || $this->type->equals($other->type);
}
public function isSuperTypeOf(self $other): bool
{
if ($this === $other) {
return true;
}
if (!$this->certainty->equals($other->certainty)) {
return false;
}
return $this->type === $other->type || $this->type->isSuperTypeOf($other->type)->yes();
}
public function and(self $other): self
{
if ($this->type === $other->type || $this->type->equals($other->type)) {
if ($this->certainty->and($other->certainty)->yes()) {
return $this;
}
if ($this->certainty->maybe()) {
return $this;
}
return $other;
}
return new self(
$this->expr,
TypeCombinator::union($this->type, $other->type),
$this->certainty->and($other->certainty),
);
}
public function getExpr(): Expr
{
return $this->expr;
}
public function getType(): Type
{
return $this->type;
}
public function getCertainty(): TrinaryLogic
{
return $this->certainty;
}
}