1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Turbo\ShadowedByTurboExtension;
6: use function sprintf;
7:
8: /** @api */
9: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/ErrorType.cpp')]
10: class ErrorType extends MixedType
11: {
12:
13: /**
14: * @api
15: * @param Type|null $witnessType What some path is known to produce, see MixedType::__construct().
16: */
17: public function __construct(private ?string $reason = null, ?Type $witnessType = null)
18: {
19: parent::__construct(witnessType: $witnessType);
20: }
21:
22: public function getReason(): ?string
23: {
24: return $this->reason;
25: }
26:
27: public function describe(VerbosityLevel $level): string
28: {
29: return $level->handle(
30: fn (): string => parent::describe($level),
31: fn (): string => parent::describe($level),
32: fn (): string => '*ERROR*' . $this->describeWitnessType($level),
33: );
34: }
35:
36: private function describeWitnessType(VerbosityLevel $level): string
37: {
38: $witnessType = $this->getWitnessType();
39: if ($witnessType === null) {
40: return '';
41: }
42:
43: if ($witnessType instanceof UnionType) {
44: return sprintf('+(%s)', $witnessType->describe($level));
45: }
46:
47: return sprintf('+%s', $witnessType->describe($level));
48: }
49:
50: public function getIterableKeyType(): Type
51: {
52: return new ErrorType();
53: }
54:
55: public function getIterableValueType(): Type
56: {
57: return new ErrorType();
58: }
59:
60: public function subtract(Type $type): Type
61: {
62: $witnessType = $this->getWitnessType();
63:
64: return $this->recreate(false, null, $witnessType !== null ? TypeCombinator::remove($witnessType, $type) : null);
65: }
66:
67: public function equals(Type $type): bool
68: {
69: if (!$type instanceof self) {
70: return false;
71: }
72:
73: $witnessType = $this->getWitnessType();
74: $otherWitnessType = $type->getWitnessType();
75: if ($witnessType === null || $otherWitnessType === null) {
76: return $witnessType === $otherWitnessType;
77: }
78:
79: return $witnessType->equals($otherWitnessType);
80: }
81:
82: /** An error has nothing subtracted and is not an explicit mixed. */
83: protected function recreate(bool $isExplicitMixed, ?Type $subtractedType, ?Type $witnessType): MixedType
84: {
85: return new self($this->reason, $witnessType);
86: }
87:
88: public function toNativePhpTypeNode(NativeTypePosition $position)
89: {
90: return null;
91: }
92:
93: }
94: