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 hasOffsetValueType(Type $offsetType): TrinaryLogic
178: {
179: return TrinaryLogic::createNo();
180: }
181:
182: public function getOffsetValueType(Type $offsetType): Type
183: {
184: return new ErrorType();
185: }
186:
187: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
188: {
189: $array = new ConstantArrayType([], []);
190: return $array->setOffsetValueType($offsetType, $valueType, $unionValues);
191: }
192:
193: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
194: {
195: return $this;
196: }
197:
198: public function unsetOffset(Type $offsetType): Type
199: {
200: return $this;
201: }
202:
203: public function traverse(callable $cb): Type
204: {
205: return $this;
206: }
207:
208: public function traverseSimultaneously(Type $right, callable $cb): Type
209: {
210: return $this;
211: }
212:
213: public function isNull(): TrinaryLogic
214: {
215: return TrinaryLogic::createYes();
216: }
217:
218: public function isConstantValue(): TrinaryLogic
219: {
220: return TrinaryLogic::createYes();
221: }
222:
223: public function isConstantScalarValue(): TrinaryLogic
224: {
225: return TrinaryLogic::createYes();
226: }
227:
228: public function getConstantScalarTypes(): array
229: {
230: return [$this];
231: }
232:
233: public function getConstantScalarValues(): array
234: {
235: return [$this->getValue()];
236: }
237:
238: public function isTrue(): TrinaryLogic
239: {
240: return TrinaryLogic::createNo();
241: }
242:
243: public function isFalse(): TrinaryLogic
244: {
245: return TrinaryLogic::createNo();
246: }
247:
248: public function isBoolean(): TrinaryLogic
249: {
250: return TrinaryLogic::createNo();
251: }
252:
253: public function isFloat(): TrinaryLogic
254: {
255: return TrinaryLogic::createNo();
256: }
257:
258: public function isInteger(): TrinaryLogic
259: {
260: return TrinaryLogic::createNo();
261: }
262:
263: public function isString(): TrinaryLogic
264: {
265: return TrinaryLogic::createNo();
266: }
267:
268: public function isNumericString(): TrinaryLogic
269: {
270: return TrinaryLogic::createNo();
271: }
272:
273: public function isNonEmptyString(): TrinaryLogic
274: {
275: return TrinaryLogic::createNo();
276: }
277:
278: public function isNonFalsyString(): TrinaryLogic
279: {
280: return TrinaryLogic::createNo();
281: }
282:
283: public function isLiteralString(): TrinaryLogic
284: {
285: return TrinaryLogic::createNo();
286: }
287:
288: public function isClassStringType(): TrinaryLogic
289: {
290: return TrinaryLogic::createNo();
291: }
292:
293: public function getClassStringObjectType(): Type
294: {
295: return new ErrorType();
296: }
297:
298: public function getObjectTypeOrClassStringObjectType(): Type
299: {
300: return new ErrorType();
301: }
302:
303: public function isVoid(): TrinaryLogic
304: {
305: return TrinaryLogic::createNo();
306: }
307:
308: public function isScalar(): TrinaryLogic
309: {
310: return TrinaryLogic::createNo();
311: }
312:
313: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
314: {
315: if ($type instanceof ConstantScalarType) {
316: return LooseComparisonHelper::compareConstantScalars($this, $type, $phpVersion);
317: }
318:
319: if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
320: // @phpstan-ignore equal.alwaysTrue, equal.notAllowed
321: return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore
322: }
323:
324: return new BooleanType();
325: }
326:
327: public function getSmallerType(): Type
328: {
329: return new NeverType();
330: }
331:
332: public function getSmallerOrEqualType(): Type
333: {
334: // All falsey types except '0'
335: return new UnionType([
336: new NullType(),
337: new ConstantBooleanType(false),
338: new ConstantIntegerType(0),
339: new ConstantFloatType(0.0),
340: new ConstantStringType(''),
341: new ConstantArrayType([], []),
342: ]);
343: }
344:
345: public function getGreaterType(): Type
346: {
347: // All truthy types, but also '0'
348: return new MixedType(false, new UnionType([
349: new NullType(),
350: new ConstantBooleanType(false),
351: new ConstantIntegerType(0),
352: new ConstantFloatType(0.0),
353: new ConstantStringType(''),
354: new ConstantArrayType([], []),
355: ]));
356: }
357:
358: public function getGreaterOrEqualType(): Type
359: {
360: return new MixedType();
361: }
362:
363: public function getFiniteTypes(): array
364: {
365: return [$this];
366: }
367:
368: public function exponentiate(Type $exponent): Type
369: {
370: return new UnionType(
371: [
372: new ConstantIntegerType(0),
373: new ConstantIntegerType(1),
374: ],
375: );
376: }
377:
378: public function toPhpDocNode(): TypeNode
379: {
380: return new IdentifierTypeNode('null');
381: }
382:
383: /**
384: * @param mixed[] $properties
385: */
386: public static function __set_state(array $properties): Type
387: {
388: return new self();
389: }
390:
391: }
392: