-
Notifications
You must be signed in to change notification settings - Fork 505
/
Copy pathTypeSpecifierContext.php
103 lines (82 loc) · 2.1 KB
/
TypeSpecifierContext.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
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
103
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Type;
/**
* @api
*/
final class TypeSpecifierContext
{
public const CONTEXT_TRUE = 0b0001;
public const CONTEXT_TRUTHY_BUT_NOT_TRUE = 0b0010;
public const CONTEXT_TRUTHY = self::CONTEXT_TRUE | self::CONTEXT_TRUTHY_BUT_NOT_TRUE;
public const CONTEXT_FALSE = 0b0100;
public const CONTEXT_FALSEY_BUT_NOT_FALSE = 0b1000;
public const CONTEXT_FALSEY = self::CONTEXT_FALSE | self::CONTEXT_FALSEY_BUT_NOT_FALSE;
public const CONTEXT_BITMASK = 0b1111;
private ?Type $returnType = null;
private function __construct(private ?int $value)
{
}
private static function create(?int $value): self
{
return new self($value);
}
public static function createTrue(): self
{
return self::create(self::CONTEXT_TRUE);
}
public static function createTruthy(): self
{
return self::create(self::CONTEXT_TRUTHY);
}
public static function createFalse(): self
{
return self::create(self::CONTEXT_FALSE);
}
public static function createFalsey(): self
{
return self::create(self::CONTEXT_FALSEY);
}
public static function createNull(): self
{
return self::create(null);
}
public function negate(): self
{
if ($this->value === null) {
throw new ShouldNotHappenException();
}
return self::create(~$this->value & self::CONTEXT_BITMASK);
}
public function true(): bool
{
return $this->value !== null && (bool) ($this->value & self::CONTEXT_TRUE);
}
public function truthy(): bool
{
return $this->value !== null && (bool) ($this->value & self::CONTEXT_TRUTHY);
}
public function false(): bool
{
return $this->value !== null && (bool) ($this->value & self::CONTEXT_FALSE);
}
public function falsey(): bool
{
return $this->value !== null && (bool) ($this->value & self::CONTEXT_FALSEY);
}
public function null(): bool
{
return $this->value === null;
}
public function newWithReturnType(Type $type): self
{
$new = self::create($this->value);
$new->returnType = $type;
return $new;
}
public function getReturnType(): ?Type
{
return $this->returnType;
}
}