1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
7: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8: use PHPStan\Reflection\ReflectionProvider;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Type\Constant\ConstantArrayType;
11: use PHPStan\Type\Constant\ConstantBooleanType;
12: use PHPStan\Type\Constant\ConstantFloatType;
13: use PHPStan\Type\Constant\ConstantIntegerType;
14: use PHPStan\Type\Constant\ConstantStringType;
15: use PHPStan\Type\Traits\FalseyBooleanTypeTrait;
16: use PHPStan\Type\Traits\NonArrayTypeTrait;
17: use PHPStan\Type\Traits\NonCallableTypeTrait;
18: use PHPStan\Type\Traits\NonGenericTypeTrait;
19: use PHPStan\Type\Traits\NonIterableTypeTrait;
20: use PHPStan\Type\Traits\NonObjectTypeTrait;
21: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
22:
23: /** @api */
24: #[InstanceofDeprecated(insteadUse: 'Type::isNull()')]
25: class NullType implements ConstantScalarType
26: {
27:
28: use NonArrayTypeTrait;
29: use NonCallableTypeTrait;
30: use NonIterableTypeTrait;
31: use NonObjectTypeTrait;
32: use FalseyBooleanTypeTrait;
33: use NonGenericTypeTrait;
34: use NonRemoveableTypeTrait;
35:
36: /** @api */
37: public function __construct()
38: {
39: }
40:
41: public function getReferencedClasses(): array
42: {
43: return [];
44: }
45:
46: public function getObjectClassNames(): array
47: {
48: return [];
49: }
50:
51: public function getObjectClassReflections(): array
52: {
53: return [];
54: }
55:
56: public function getConstantStrings(): array
57: {
58: return [];
59: }
60:
61: /**
62: * @return null
63: */
64: public function getValue()
65: {
66: return null;
67: }
68:
69: public function generalize(GeneralizePrecision $precision): Type
70: {
71: return $this;
72: }
73:
74: public function accepts(Type $type, bool $strictTypes): AcceptsResult
75: {
76: if ($type instanceof self) {
77: return AcceptsResult::createYes();
78: }
79:
80: if ($type instanceof CompoundType) {
81: return $type->isAcceptedBy($this, $strictTypes);
82: }
83:
84: return AcceptsResult::createNo();
85: }
86:
87: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
88: {
89: if ($type instanceof self) {
90: return IsSuperTypeOfResult::createYes();
91: }
92:
93: if ($type instanceof CompoundType) {
94: return $type->isSubTypeOf($this);
95: }
96:
97: return IsSuperTypeOfResult::createNo();
98: }
99:
100: public function equals(Type $type): bool
101: {
102: return $type instanceof self;
103: }
104:
105: public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
106: {
107: if ($otherType instanceof ConstantScalarType) {
108: return TrinaryLogic::createFromBoolean(null < $otherType->getValue());
109: }
110:
111: if ($otherType instanceof CompoundType) {
112: return $otherType->isGreaterThan($this, $phpVersion);
113: }
114:
115: if ($otherType->isObject()->yes()) {
116: return TrinaryLogic::createYes();
117: }
118:
119: return TrinaryLogic::createMaybe();
120: }
121:
122: public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): 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, $phpVersion);
130: }
131:
132: if ($otherType->isObject()->yes()) {
133: return TrinaryLogic::createYes();
134: }
135:
136: return TrinaryLogic::createMaybe();
137: }
138:
139: public function describe(VerbosityLevel $level): string
140: {
141: return 'null';
142: }
143:
144: public function toNumber(): Type
145: {
146: return new ConstantIntegerType(0);
147: }
148:
149: public function toBitwiseNotType(): Type
150: {
151: return new ErrorType();
152: }
153:
154: public function toClassConstantType(ReflectionProvider $reflectionProvider): Type
155: {
156: // Null `::class` reads as `null` (mirrors how `null::class` flows
157: // through the `::class` resolution pipeline alongside object types).
158: return $this;
159: }
160:
161: public function toAbsoluteNumber(): Type
162: {
163: return $this->toNumber()->toAbsoluteNumber();
164: }
165:
166: public function toString(): Type
167: {
168: return new ConstantStringType('');
169: }
170:
171: public function toInteger(): Type
172: {
173: return $this->toNumber();
174: }
175:
176: public function toFloat(): Type
177: {
178: return $this->toNumber()->toFloat();
179: }
180:
181: public function toArray(): Type
182: {
183: return new ConstantArrayType([], []);
184: }
185:
186: public function toArrayKey(): Type
187: {
188: return new ConstantStringType('');
189: }
190:
191: public function toCoercedArgumentType(bool $strictTypes): Type
192: {
193: return $this;
194: }
195:
196: public function isOffsetAccessible(): TrinaryLogic
197: {
198: return TrinaryLogic::createYes();
199: }
200:
201: public function isOffsetAccessLegal(): TrinaryLogic
202: {
203: return TrinaryLogic::createYes();
204: }
205:
206: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
207: {
208: return TrinaryLogic::createNo();
209: }
210:
211: public function getOffsetValueType(Type $offsetType): Type
212: {
213: return new ErrorType();
214: }
215:
216: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
217: {
218: $array = new ConstantArrayType([], []);
219: return $array->setOffsetValueType($offsetType, $valueType, $unionValues);
220: }
221:
222: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
223: {
224: return $this;
225: }
226:
227: public function unsetOffset(Type $offsetType): Type
228: {
229: return $this;
230: }
231:
232: public function traverse(callable $cb): Type
233: {
234: return $this;
235: }
236:
237: public function traverseSimultaneously(Type $right, callable $cb): Type
238: {
239: return $this;
240: }
241:
242: public function isNull(): TrinaryLogic
243: {
244: return TrinaryLogic::createYes();
245: }
246:
247: public function isConstantValue(): TrinaryLogic
248: {
249: return TrinaryLogic::createYes();
250: }
251:
252: public function isConstantScalarValue(): TrinaryLogic
253: {
254: return TrinaryLogic::createYes();
255: }
256:
257: public function getConstantScalarTypes(): array
258: {
259: return [$this];
260: }
261:
262: public function getConstantScalarValues(): array
263: {
264: return [$this->getValue()];
265: }
266:
267: public function isTrue(): TrinaryLogic
268: {
269: return TrinaryLogic::createNo();
270: }
271:
272: public function isFalse(): TrinaryLogic
273: {
274: return TrinaryLogic::createNo();
275: }
276:
277: public function isBoolean(): TrinaryLogic
278: {
279: return TrinaryLogic::createNo();
280: }
281:
282: public function isFloat(): TrinaryLogic
283: {
284: return TrinaryLogic::createNo();
285: }
286:
287: public function isInteger(): TrinaryLogic
288: {
289: return TrinaryLogic::createNo();
290: }
291:
292: public function isString(): TrinaryLogic
293: {
294: return TrinaryLogic::createNo();
295: }
296:
297: public function isNumericString(): TrinaryLogic
298: {
299: return TrinaryLogic::createNo();
300: }
301:
302: public function isDecimalIntegerString(): TrinaryLogic
303: {
304: return TrinaryLogic::createNo();
305: }
306:
307: public function isNonEmptyString(): TrinaryLogic
308: {
309: return TrinaryLogic::createNo();
310: }
311:
312: public function isNonFalsyString(): TrinaryLogic
313: {
314: return TrinaryLogic::createNo();
315: }
316:
317: public function isLiteralString(): TrinaryLogic
318: {
319: return TrinaryLogic::createNo();
320: }
321:
322: public function isLowercaseString(): TrinaryLogic
323: {
324: return TrinaryLogic::createNo();
325: }
326:
327: public function isUppercaseString(): TrinaryLogic
328: {
329: return TrinaryLogic::createNo();
330: }
331:
332: public function isClassString(): TrinaryLogic
333: {
334: return TrinaryLogic::createNo();
335: }
336:
337: public function getClassStringObjectType(): Type
338: {
339: return new ErrorType();
340: }
341:
342: public function getObjectTypeOrClassStringObjectType(): Type
343: {
344: return new ErrorType();
345: }
346:
347: public function isVoid(): TrinaryLogic
348: {
349: return TrinaryLogic::createNo();
350: }
351:
352: public function isScalar(): TrinaryLogic
353: {
354: return TrinaryLogic::createNo();
355: }
356:
357: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
358: {
359: if ($type instanceof ConstantScalarType) {
360: return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion);
361: }
362:
363: if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
364: // @phpstan-ignore equal.alwaysTrue, equal.notAllowed
365: return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore
366: }
367:
368: if ($type instanceof CompoundType) {
369: return $type->looseCompare($this, $phpVersion);
370: }
371:
372: return new BooleanType();
373: }
374:
375: public function getSmallerType(PhpVersion $phpVersion): Type
376: {
377: return new NeverType();
378: }
379:
380: public function getSmallerOrEqualType(PhpVersion $phpVersion): Type
381: {
382: // All falsey types except '0'
383: return new UnionType([
384: new NullType(),
385: new ConstantBooleanType(false),
386: new ConstantIntegerType(0),
387: new ConstantFloatType(0.0),
388: new ConstantStringType(''),
389: new ConstantArrayType([], []),
390: ]);
391: }
392:
393: public function getGreaterType(PhpVersion $phpVersion): Type
394: {
395: // All truthy types, but also '0'
396: return new MixedType(subtractedType: new UnionType([
397: new NullType(),
398: new ConstantBooleanType(false),
399: new ConstantIntegerType(0),
400: new ConstantFloatType(0.0),
401: new ConstantStringType(''),
402: new ConstantArrayType([], []),
403: ]));
404: }
405:
406: public function getGreaterOrEqualType(PhpVersion $phpVersion): Type
407: {
408: return new MixedType();
409: }
410:
411: public function getFiniteTypes(): array
412: {
413: return [$this];
414: }
415:
416: public function exponentiate(Type $exponent): Type
417: {
418: return new UnionType(
419: [
420: new ConstantIntegerType(0),
421: new ConstantIntegerType(1),
422: ],
423: );
424: }
425:
426: public function toPhpDocNode(): TypeNode
427: {
428: return new IdentifierTypeNode('null');
429: }
430:
431: public function hasTemplateOrLateResolvableType(): bool
432: {
433: return false;
434: }
435:
436: }
437: