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