1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\TrinaryLogic;
7: use PHPStan\Type\Constant\ConstantArrayType;
8: use PHPStan\Type\Constant\ConstantBooleanType;
9: use PHPStan\Type\Constant\ConstantFloatType;
10: use PHPStan\Type\Constant\ConstantIntegerType;
11: use PHPStan\Type\Constant\ConstantStringType;
12: use PHPStan\Type\Traits\FalseyBooleanTypeTrait;
13: use PHPStan\Type\Traits\NonArrayTypeTrait;
14: use PHPStan\Type\Traits\NonCallableTypeTrait;
15: use PHPStan\Type\Traits\NonGenericTypeTrait;
16: use PHPStan\Type\Traits\NonIterableTypeTrait;
17: use PHPStan\Type\Traits\NonObjectTypeTrait;
18: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
19:
20: /** @api */
21: class NullType implements ConstantScalarType
22: {
23:
24: use NonArrayTypeTrait;
25: use NonCallableTypeTrait;
26: use NonIterableTypeTrait;
27: use NonObjectTypeTrait;
28: use FalseyBooleanTypeTrait;
29: use NonGenericTypeTrait;
30: use NonRemoveableTypeTrait;
31:
32: /** @api */
33: public function __construct()
34: {
35: }
36:
37: /**
38: * @return string[]
39: */
40: public function getReferencedClasses(): array
41: {
42: return [];
43: }
44:
45: public function getObjectClassNames(): array
46: {
47: return [];
48: }
49:
50: public function getObjectClassReflections(): array
51: {
52: return [];
53: }
54:
55: public function getConstantStrings(): array
56: {
57: return [];
58: }
59:
60: /**
61: * @return null
62: */
63: public function getValue()
64: {
65: return null;
66: }
67:
68: public function generalize(GeneralizePrecision $precision): Type
69: {
70: return $this;
71: }
72:
73: public function accepts(Type $type, bool $strictTypes): TrinaryLogic
74: {
75: return $this->acceptsWithReason($type, $strictTypes)->result;
76: }
77:
78: public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
79: {
80: if ($type instanceof self) {
81: return AcceptsResult::createYes();
82: }
83:
84: if ($type instanceof CompoundType) {
85: return $type->isAcceptedWithReasonBy($this, $strictTypes);
86: }
87:
88: return AcceptsResult::createNo();
89: }
90:
91: public function isSuperTypeOf(Type $type): TrinaryLogic
92: {
93: if ($type instanceof self) {
94: return TrinaryLogic::createYes();
95: }
96:
97: if ($type instanceof CompoundType) {
98: return $type->isSubTypeOf($this);
99: }
100:
101: return TrinaryLogic::createNo();
102: }
103:
104: public function equals(Type $type): bool
105: {
106: return $type instanceof self;
107: }
108:
109: public function isSmallerThan(Type $otherType): TrinaryLogic
110: {
111: if ($otherType instanceof ConstantScalarType) {
112: return TrinaryLogic::createFromBoolean(null < $otherType->getValue());
113: }
114:
115: if ($otherType instanceof CompoundType) {
116: return $otherType->isGreaterThan($this);
117: }
118:
119: return TrinaryLogic::createMaybe();
120: }
121:
122: public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
123: {
124: if ($otherType instanceof ConstantScalarType) {
125: return TrinaryLogic::createFromBoolean(null <= $otherType->getValue());
126: }
127:
128: if ($otherType instanceof CompoundType) {
129: return $otherType->isGreaterThanOrEqual($this);
130: }
131:
132: return TrinaryLogic::createMaybe();
133: }
134:
135: public function describe(VerbosityLevel $level): string
136: {
137: return 'null';
138: }
139:
140: public function toNumber(): Type
141: {
142: return new ConstantIntegerType(0);
143: }
144:
145: public function toString(): Type
146: {
147: return new ConstantStringType('');
148: }
149:
150: public function toInteger(): Type
151: {
152: return $this->toNumber();
153: }
154:
155: public function toFloat(): Type
156: {
157: return $this->toNumber()->toFloat();
158: }
159:
160: public function toArray(): Type
161: {
162: return new ConstantArrayType([], []);
163: }
164:
165: public function toArrayKey(): Type
166: {
167: return new ConstantStringType('');
168: }
169:
170: public function isOffsetAccessible(): TrinaryLogic
171: {
172: return TrinaryLogic::createYes();
173: }
174:
175: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
176: {
177: return TrinaryLogic::createNo();
178: }
179:
180: public function getOffsetValueType(Type $offsetType): Type
181: {
182: return new ErrorType();
183: }
184:
185: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
186: {
187: $array = new ConstantArrayType([], []);
188: return $array->setOffsetValueType($offsetType, $valueType, $unionValues);
189: }
190:
191: public function unsetOffset(Type $offsetType): Type
192: {
193: return $this;
194: }
195:
196: public function traverse(callable $cb): Type
197: {
198: return $this;
199: }
200:
201: public function isNull(): TrinaryLogic
202: {
203: return TrinaryLogic::createYes();
204: }
205:
206: public function isConstantValue(): TrinaryLogic
207: {
208: return TrinaryLogic::createYes();
209: }
210:
211: public function isConstantScalarValue(): TrinaryLogic
212: {
213: return TrinaryLogic::createYes();
214: }
215:
216: public function getConstantScalarTypes(): array
217: {
218: return [$this];
219: }
220:
221: public function getConstantScalarValues(): array
222: {
223: return [$this->getValue()];
224: }
225:
226: public function isTrue(): TrinaryLogic
227: {
228: return TrinaryLogic::createNo();
229: }
230:
231: public function isFalse(): TrinaryLogic
232: {
233: return TrinaryLogic::createNo();
234: }
235:
236: public function isBoolean(): TrinaryLogic
237: {
238: return TrinaryLogic::createNo();
239: }
240:
241: public function isFloat(): TrinaryLogic
242: {
243: return TrinaryLogic::createNo();
244: }
245:
246: public function isInteger(): TrinaryLogic
247: {
248: return TrinaryLogic::createNo();
249: }
250:
251: public function isString(): TrinaryLogic
252: {
253: return TrinaryLogic::createNo();
254: }
255:
256: public function isNumericString(): TrinaryLogic
257: {
258: return TrinaryLogic::createNo();
259: }
260:
261: public function isNonEmptyString(): TrinaryLogic
262: {
263: return TrinaryLogic::createNo();
264: }
265:
266: public function isNonFalsyString(): TrinaryLogic
267: {
268: return TrinaryLogic::createNo();
269: }
270:
271: public function isLiteralString(): TrinaryLogic
272: {
273: return TrinaryLogic::createNo();
274: }
275:
276: public function isClassStringType(): TrinaryLogic
277: {
278: return TrinaryLogic::createNo();
279: }
280:
281: public function getClassStringObjectType(): Type
282: {
283: return new ErrorType();
284: }
285:
286: public function getObjectTypeOrClassStringObjectType(): Type
287: {
288: return new ErrorType();
289: }
290:
291: public function isVoid(): TrinaryLogic
292: {
293: return TrinaryLogic::createNo();
294: }
295:
296: public function isScalar(): TrinaryLogic
297: {
298: return TrinaryLogic::createNo();
299: }
300:
301: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
302: {
303: if ($type instanceof ConstantScalarType) {
304: return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion);
305: }
306:
307: if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
308: // @phpstan-ignore-next-line
309: return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore
310: }
311:
312: return new BooleanType();
313: }
314:
315: public function getSmallerType(): Type
316: {
317: return new NeverType();
318: }
319:
320: public function getSmallerOrEqualType(): Type
321: {
322: // All falsey types except '0'
323: return new UnionType([
324: new NullType(),
325: new ConstantBooleanType(false),
326: new ConstantIntegerType(0),
327: new ConstantFloatType(0.0),
328: new ConstantStringType(''),
329: new ConstantArrayType([], []),
330: ]);
331: }
332:
333: public function getGreaterType(): Type
334: {
335: // All truthy types, but also '0'
336: return new MixedType(false, new UnionType([
337: new NullType(),
338: new ConstantBooleanType(false),
339: new ConstantIntegerType(0),
340: new ConstantFloatType(0.0),
341: new ConstantStringType(''),
342: new ConstantArrayType([], []),
343: ]));
344: }
345:
346: public function getGreaterOrEqualType(): Type
347: {
348: return new MixedType();
349: }
350:
351: public function exponentiate(Type $exponent): Type
352: {
353: return new UnionType(
354: [
355: new ConstantIntegerType(0),
356: new ConstantIntegerType(1),
357: ],
358: );
359: }
360:
361: /**
362: * @param mixed[] $properties
363: */
364: public static function __set_state(array $properties): Type
365: {
366: return new self();
367: }
368:
369: }
370: