1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser; |
4: | |
5: | |
6: | |
7: | |
8: | class PhpVersion { |
9: | |
10: | public int $id; |
11: | |
12: | |
13: | private const BUILTIN_TYPE_VERSIONS = [ |
14: | 'array' => 50100, |
15: | 'callable' => 50400, |
16: | 'bool' => 70000, |
17: | 'int' => 70000, |
18: | 'float' => 70000, |
19: | 'string' => 70000, |
20: | 'iterable' => 70100, |
21: | 'void' => 70100, |
22: | 'object' => 70200, |
23: | 'null' => 80000, |
24: | 'false' => 80000, |
25: | 'mixed' => 80000, |
26: | 'never' => 80100, |
27: | 'true' => 80200, |
28: | ]; |
29: | |
30: | private function __construct(int $id) { |
31: | $this->id = $id; |
32: | } |
33: | |
34: | |
35: | |
36: | |
37: | public static function fromComponents(int $major, int $minor): self { |
38: | return new self($major * 10000 + $minor * 100); |
39: | } |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | public static function getNewestSupported(): self { |
46: | return self::fromComponents(8, 4); |
47: | } |
48: | |
49: | |
50: | |
51: | |
52: | public static function getHostVersion(): self { |
53: | return self::fromComponents(\PHP_MAJOR_VERSION, \PHP_MINOR_VERSION); |
54: | } |
55: | |
56: | |
57: | |
58: | |
59: | public static function fromString(string $version): self { |
60: | if (!preg_match('/^(\d+)\.(\d+)/', $version, $matches)) { |
61: | throw new \LogicException("Invalid PHP version \"$version\""); |
62: | } |
63: | return self::fromComponents((int) $matches[1], (int) $matches[2]); |
64: | } |
65: | |
66: | |
67: | |
68: | |
69: | public function equals(PhpVersion $other): bool { |
70: | return $this->id === $other->id; |
71: | } |
72: | |
73: | |
74: | |
75: | |
76: | public function newerOrEqual(PhpVersion $other): bool { |
77: | return $this->id >= $other->id; |
78: | } |
79: | |
80: | |
81: | |
82: | |
83: | public function older(PhpVersion $other): bool { |
84: | return $this->id < $other->id; |
85: | } |
86: | |
87: | |
88: | |
89: | |
90: | public function isHostVersion(): bool { |
91: | return $this->equals(self::getHostVersion()); |
92: | } |
93: | |
94: | |
95: | |
96: | |
97: | public function supportsBuiltinType(string $type): bool { |
98: | $minVersion = self::BUILTIN_TYPE_VERSIONS[$type] ?? null; |
99: | return $minVersion !== null && $this->id >= $minVersion; |
100: | } |
101: | |
102: | |
103: | |
104: | |
105: | public function supportsShortArraySyntax(): bool { |
106: | return $this->id >= 50400; |
107: | } |
108: | |
109: | |
110: | |
111: | |
112: | public function supportsShortArrayDestructuring(): bool { |
113: | return $this->id >= 70100; |
114: | } |
115: | |
116: | |
117: | |
118: | |
119: | public function supportsFlexibleHeredoc(): bool { |
120: | return $this->id >= 70300; |
121: | } |
122: | |
123: | |
124: | |
125: | |
126: | public function supportsTrailingCommaInParamList(): bool { |
127: | return $this->id >= 80000; |
128: | } |
129: | |
130: | |
131: | |
132: | |
133: | public function allowsAssignNewByReference(): bool { |
134: | return $this->id < 70000; |
135: | } |
136: | |
137: | |
138: | |
139: | |
140: | public function allowsInvalidOctals(): bool { |
141: | return $this->id < 70000; |
142: | } |
143: | |
144: | |
145: | |
146: | |
147: | public function allowsDelInIdentifiers(): bool { |
148: | return $this->id < 70100; |
149: | } |
150: | |
151: | |
152: | |
153: | |
154: | public function supportsYieldWithoutParentheses(): bool { |
155: | return $this->id >= 70000; |
156: | } |
157: | |
158: | |
159: | |
160: | |
161: | public function supportsUnicodeEscapes(): bool { |
162: | return $this->id >= 70000; |
163: | } |
164: | } |
165: | |