|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Accessory; |
| 4 | + |
| 5 | +use PHPStan\TrinaryLogic; |
| 6 | +use PHPStan\Type\CompoundType; |
| 7 | +use PHPStan\Type\CompoundTypeHelper; |
| 8 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 9 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 10 | +use PHPStan\Type\ErrorType; |
| 11 | +use PHPStan\Type\FloatType; |
| 12 | +use PHPStan\Type\IntegerType; |
| 13 | +use PHPStan\Type\IntersectionType; |
| 14 | +use PHPStan\Type\MixedType; |
| 15 | +use PHPStan\Type\StringType; |
| 16 | +use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
| 17 | +use PHPStan\Type\Traits\NonGenericTypeTrait; |
| 18 | +use PHPStan\Type\Traits\NonIterableTypeTrait; |
| 19 | +use PHPStan\Type\Traits\NonObjectTypeTrait; |
| 20 | +use PHPStan\Type\Traits\TruthyBooleanTypeTrait; |
| 21 | +use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
| 22 | +use PHPStan\Type\Type; |
| 23 | +use PHPStan\Type\UnionType; |
| 24 | + |
| 25 | +class AccessoryLiteralStringType implements CompoundType, AccessoryType |
| 26 | +{ |
| 27 | + |
| 28 | + use MaybeCallableTypeTrait; |
| 29 | + use NonObjectTypeTrait; |
| 30 | + use NonIterableTypeTrait; |
| 31 | + use TruthyBooleanTypeTrait; |
| 32 | + use UndecidedComparisonCompoundTypeTrait; |
| 33 | + use NonGenericTypeTrait; |
| 34 | + |
| 35 | + /** @api */ |
| 36 | + public function __construct() |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + public function getReferencedClasses(): array |
| 41 | + { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + public function accepts(Type $type, bool $strictTypes): TrinaryLogic |
| 46 | + { |
| 47 | + if ($type instanceof MixedType) { |
| 48 | + return TrinaryLogic::createNo(); |
| 49 | + } |
| 50 | + if ($type instanceof CompoundType) { |
| 51 | + return CompoundTypeHelper::accepts($type, $this, $strictTypes); |
| 52 | + } |
| 53 | + |
| 54 | + return $type->isLiteralString(); |
| 55 | + } |
| 56 | + |
| 57 | + public function isSuperTypeOf(Type $type): TrinaryLogic |
| 58 | + { |
| 59 | + if ($type instanceof CompoundType) { |
| 60 | + return $type->isSubTypeOf($this); |
| 61 | + } |
| 62 | + |
| 63 | + if ($this->equals($type)) { |
| 64 | + return TrinaryLogic::createYes(); |
| 65 | + } |
| 66 | + |
| 67 | + return $type->isLiteralString(); |
| 68 | + } |
| 69 | + |
| 70 | + public function isSubTypeOf(Type $otherType): TrinaryLogic |
| 71 | + { |
| 72 | + if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { |
| 73 | + return $otherType->isSuperTypeOf($this); |
| 74 | + } |
| 75 | + |
| 76 | + return $otherType->isLiteralString() |
| 77 | + ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()); |
| 78 | + } |
| 79 | + |
| 80 | + public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic |
| 81 | + { |
| 82 | + return $this->isSubTypeOf($acceptingType); |
| 83 | + } |
| 84 | + |
| 85 | + public function equals(Type $type): bool |
| 86 | + { |
| 87 | + return $type instanceof self; |
| 88 | + } |
| 89 | + |
| 90 | + public function describe(\PHPStan\Type\VerbosityLevel $level): string |
| 91 | + { |
| 92 | + return 'literal-string'; |
| 93 | + } |
| 94 | + |
| 95 | + public function isOffsetAccessible(): TrinaryLogic |
| 96 | + { |
| 97 | + return TrinaryLogic::createYes(); |
| 98 | + } |
| 99 | + |
| 100 | + public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
| 101 | + { |
| 102 | + return (new IntegerType())->isSuperTypeOf($offsetType)->and(TrinaryLogic::createMaybe()); |
| 103 | + } |
| 104 | + |
| 105 | + public function getOffsetValueType(Type $offsetType): Type |
| 106 | + { |
| 107 | + if ($this->hasOffsetValueType($offsetType)->no()) { |
| 108 | + return new ErrorType(); |
| 109 | + } |
| 110 | + |
| 111 | + return new StringType(); |
| 112 | + } |
| 113 | + |
| 114 | + public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
| 115 | + { |
| 116 | + return $this; |
| 117 | + } |
| 118 | + |
| 119 | + public function isArray(): TrinaryLogic |
| 120 | + { |
| 121 | + return TrinaryLogic::createNo(); |
| 122 | + } |
| 123 | + |
| 124 | + public function toNumber(): Type |
| 125 | + { |
| 126 | + return new UnionType([ |
| 127 | + $this->toInteger(), |
| 128 | + $this->toFloat(), |
| 129 | + ]); |
| 130 | + } |
| 131 | + |
| 132 | + public function toInteger(): Type |
| 133 | + { |
| 134 | + return new IntegerType(); |
| 135 | + } |
| 136 | + |
| 137 | + public function toFloat(): Type |
| 138 | + { |
| 139 | + return new FloatType(); |
| 140 | + } |
| 141 | + |
| 142 | + public function toString(): Type |
| 143 | + { |
| 144 | + return $this; |
| 145 | + } |
| 146 | + |
| 147 | + public function toArray(): Type |
| 148 | + { |
| 149 | + return new ConstantArrayType( |
| 150 | + [new ConstantIntegerType(0)], |
| 151 | + [$this], |
| 152 | + 1 |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + public function isNumericString(): TrinaryLogic |
| 157 | + { |
| 158 | + return TrinaryLogic::createMaybe(); |
| 159 | + } |
| 160 | + |
| 161 | + public function isNonEmptyString(): TrinaryLogic |
| 162 | + { |
| 163 | + return TrinaryLogic::createMaybe(); |
| 164 | + } |
| 165 | + |
| 166 | + public function isLiteralString(): TrinaryLogic |
| 167 | + { |
| 168 | + return TrinaryLogic::createYes(); |
| 169 | + } |
| 170 | + |
| 171 | + public function traverse(callable $cb): Type |
| 172 | + { |
| 173 | + return $this; |
| 174 | + } |
| 175 | + |
| 176 | + public static function __set_state(array $properties): Type |
| 177 | + { |
| 178 | + return new self(); |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments