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 isUppercaseString(): TrinaryLogic
296: {
297: return TrinaryLogic::createNo();
298: }
299:
300: public function isClassString(): TrinaryLogic
301: {
302: return TrinaryLogic::createNo();
303: }
304:
305: public function getClassStringObjectType(): Type
306: {
307: return new ErrorType();
308: }
309:
310: public function getObjectTypeOrClassStringObjectType(): Type
311: {
312: return new ErrorType();
313: }
314:
315: public function isVoid(): TrinaryLogic
316: {
317: return TrinaryLogic::createNo();
318: }
319:
320: public function isScalar(): TrinaryLogic
321: {
322: return TrinaryLogic::createNo();
323: }
324:
325: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
326: {
327: if ($type instanceof ConstantScalarType) {
328: return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion);
329: }
330:
331: if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
332: // @phpstan-ignore equal.alwaysTrue, equal.notAllowed
333: return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore
334: }
335:
336: if ($type instanceof CompoundType) {
337: return $type->looseCompare($this, $phpVersion);
338: }
339:
340: return new BooleanType();
341: }
342:
343: public function getSmallerType(PhpVersion $phpVersion): Type
344: {
345: return new NeverType();
346: }
347:
348: public function getSmallerOrEqualType(PhpVersion $phpVersion): Type
349: {
350: // All falsey types except '0'
351: return new UnionType([
352: new NullType(),
353: new ConstantBooleanType(false),
354: new ConstantIntegerType(0),
355: new ConstantFloatType(0.0),
356: new ConstantStringType(''),
357: new ConstantArrayType([], []),
358: ]);
359: }
360:
361: public function getGreaterType(PhpVersion $phpVersion): Type
362: {
363: // All truthy types, but also '0'
364: return new MixedType(false, new UnionType([
365: new NullType(),
366: new ConstantBooleanType(false),
367: new ConstantIntegerType(0),
368: new ConstantFloatType(0.0),
369: new ConstantStringType(''),
370: new ConstantArrayType([], []),
371: ]));
372: }
373:
374: public function getGreaterOrEqualType(PhpVersion $phpVersion): Type
375: {
376: return new MixedType();
377: }
378:
379: public function getFiniteTypes(): array
380: {
381: return [$this];
382: }
383:
384: public function exponentiate(Type $exponent): Type
385: {
386: return new UnionType(
387: [
388: new ConstantIntegerType(0),
389: new ConstantIntegerType(1),
390: ],
391: );
392: }
393:
394: public function toPhpDocNode(): TypeNode
395: {
396: return new IdentifierTypeNode('null');
397: }
398:
399: }
400: