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\Turbo\ShadowedByTurboExtension;
10: use PHPStan\Type\Enum\EnumCaseObjectType;
11: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
12: use PHPStan\Type\Traits\NonGenericTypeTrait;
13: use PHPStan\Type\Traits\ObjectTypeTrait;
14: use PHPStan\Type\Traits\OperatorTypeTrait;
15: use PHPStan\Type\Traits\SubstractableTypeTrait;
16: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
17:
18: /** @api */
19: #[InstanceofDeprecated(insteadUse: 'Type::isObject()')]
20: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/ObjectWithoutClassType.cpp')]
21: class ObjectWithoutClassType implements SubtractableType
22: {
23:
24: use ObjectTypeTrait;
25: use NonGenericTypeTrait;
26: use UndecidedComparisonTypeTrait;
27: use NonGeneralizableTypeTrait;
28: use SubstractableTypeTrait;
29: use OperatorTypeTrait;
30:
31: private ?Type $subtractedType;
32:
33: /** @api */
34: public function __construct(
35: ?Type $subtractedType = null,
36: )
37: {
38: if ($subtractedType instanceof NeverType) {
39: $subtractedType = null;
40: }
41:
42: $this->subtractedType = $subtractedType;
43: }
44:
45: public function getReferencedClasses(): array
46: {
47: return [];
48: }
49:
50: public function getObjectClassNames(): array
51: {
52: return [];
53: }
54:
55: public function getObjectClassReflections(): array
56: {
57: return [];
58: }
59:
60: public function getClassStringType(): Type
61: {
62: return new ClassStringType();
63: }
64:
65: public function accepts(Type $type, bool $strictTypes): AcceptsResult
66: {
67: if ($type instanceof CompoundType) {
68: return $type->isAcceptedBy($this, $strictTypes);
69: }
70:
71: return AcceptsResult::createFromBoolean(
72: $type instanceof self || $type instanceof ObjectShapeType || $type->getObjectClassNames() !== [],
73: );
74: }
75:
76: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
77: {
78: if ($type instanceof CompoundType) {
79: return $type->isSubTypeOf($this);
80: }
81:
82: if ($type instanceof self) {
83: if ($this->subtractedType === null) {
84: return IsSuperTypeOfResult::createYes();
85: }
86: if ($type->subtractedType !== null) {
87: $isSuperType = $type->subtractedType->isSuperTypeOf($this->subtractedType);
88: if ($isSuperType->yes()) {
89: return $isSuperType;
90: }
91: }
92:
93: return IsSuperTypeOfResult::createMaybe();
94: }
95:
96: if ($type instanceof ObjectShapeType) {
97: return IsSuperTypeOfResult::createYes();
98: }
99:
100: if ($type->getObjectClassNames() === []) {
101: return IsSuperTypeOfResult::createNo();
102: }
103:
104: if ($this->subtractedType === null) {
105: return IsSuperTypeOfResult::createYes();
106: }
107:
108: return $this->subtractedType->isSuperTypeOf($type)->negate();
109: }
110:
111: public function equals(Type $type): bool
112: {
113: if (!$type instanceof self) {
114: return false;
115: }
116:
117: if ($this->subtractedType === null) {
118: if ($type->subtractedType === null) {
119: return true;
120: }
121:
122: return false;
123: }
124:
125: if ($type->subtractedType === null) {
126: return false;
127: }
128:
129: return $this->subtractedType->equals($type->subtractedType);
130: }
131:
132: public function describe(VerbosityLevel $level): string
133: {
134: return $level->handle(
135: static fn (): string => 'object',
136: static fn (): string => 'object',
137: fn (): string => 'object' . $this->describeSubtractedType($this->subtractedType, $level),
138: );
139: }
140:
141: public function getEnumCases(): array
142: {
143: return [];
144: }
145:
146: public function getEnumCaseObject(): ?EnumCaseObjectType
147: {
148: return null;
149: }
150:
151: public function subtract(Type $type): Type
152: {
153: if ($type instanceof self) {
154: return new NeverType();
155: }
156: if ($this->subtractedType !== null) {
157: $type = TypeCombinator::union($this->subtractedType, $type);
158: }
159:
160: return new self($type);
161: }
162:
163: public function getTypeWithoutSubtractedType(): Type
164: {
165: return new self();
166: }
167:
168: public function changeSubtractedType(?Type $subtractedType): Type
169: {
170: return new self($subtractedType);
171: }
172:
173: public function getSubtractedType(): ?Type
174: {
175: return $this->subtractedType;
176: }
177:
178: public function traverse(callable $cb): Type
179: {
180: $subtractedType = $this->subtractedType !== null ? $cb($this->subtractedType) : null;
181:
182: if ($subtractedType !== $this->subtractedType) {
183: return new self($subtractedType);
184: }
185:
186: return $this;
187: }
188:
189: public function traverseSimultaneously(Type $right, callable $cb): Type
190: {
191: if ($this->subtractedType === null) {
192: return $this;
193: }
194:
195: return new self();
196: }
197:
198: public function tryRemove(Type $typeToRemove): ?Type
199: {
200: // object is the top of the object hierarchy, so removal is exactly the
201: // subtraction - also when an earlier subtraction already lowered
202: // isSuperTypeOf() from yes to maybe, which used to remove nothing at all
203: if ($this->isSuperTypeOf($typeToRemove)->no()) {
204: return null;
205: }
206:
207: return $this->subtract($typeToRemove);
208: }
209:
210: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
211: {
212: if (!$exponent instanceof NeverType && !$this->isSuperTypeOf($exponent)->no()) {
213: return TypeCombinator::union($this, $exponent);
214: }
215:
216: return new BenevolentUnionType([
217: new FloatType(),
218: new IntegerType(),
219: ]);
220: }
221:
222: public function getFiniteTypes(): array
223: {
224: return [];
225: }
226:
227: public function toPhpDocNode(): TypeNode
228: {
229: return new IdentifierTypeNode('object');
230: }
231:
232: public function toNativePhpTypeNode(NativeTypePosition $position)
233: {
234: return $position->isObjectAllowed() ? new Identifier('object') : null;
235: }
236:
237: public function hasTemplateOrLateResolvableType(): bool
238: {
239: return false;
240: }
241:
242: public function toValueSet(): ValueSet
243: {
244: return ValueSet::createObjects($this);
245: }
246:
247: }
248: