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