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