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