| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type\Accessory; |
| 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\AcceptsResult; |
| 10: | use PHPStan\Type\BenevolentUnionType; |
| 11: | use PHPStan\Type\BooleanType; |
| 12: | use PHPStan\Type\CompoundType; |
| 13: | use PHPStan\Type\Constant\ConstantArrayType; |
| 14: | use PHPStan\Type\Constant\ConstantIntegerType; |
| 15: | use PHPStan\Type\Constant\ConstantStringType; |
| 16: | use PHPStan\Type\ErrorType; |
| 17: | use PHPStan\Type\FloatType; |
| 18: | use PHPStan\Type\GeneralizePrecision; |
| 19: | use PHPStan\Type\IntegerType; |
| 20: | use PHPStan\Type\IntersectionType; |
| 21: | use PHPStan\Type\ObjectWithoutClassType; |
| 22: | use PHPStan\Type\StringType; |
| 23: | use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
| 24: | use PHPStan\Type\Traits\NonArrayTypeTrait; |
| 25: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
| 26: | use PHPStan\Type\Traits\NonIterableTypeTrait; |
| 27: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
| 28: | use PHPStan\Type\Traits\UndecidedBooleanTypeTrait; |
| 29: | use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
| 30: | use PHPStan\Type\Type; |
| 31: | use PHPStan\Type\TypeCombinator; |
| 32: | use PHPStan\Type\UnionType; |
| 33: | use PHPStan\Type\VerbosityLevel; |
| 34: | |
| 35: | class AccessoryNonEmptyStringType implements CompoundType, AccessoryType |
| 36: | { |
| 37: | |
| 38: | use MaybeCallableTypeTrait; |
| 39: | use NonArrayTypeTrait; |
| 40: | use NonObjectTypeTrait; |
| 41: | use NonIterableTypeTrait; |
| 42: | use UndecidedComparisonCompoundTypeTrait; |
| 43: | use NonGenericTypeTrait; |
| 44: | use UndecidedBooleanTypeTrait; |
| 45: | |
| 46: | |
| 47: | public function __construct() |
| 48: | { |
| 49: | } |
| 50: | |
| 51: | public function getReferencedClasses(): array |
| 52: | { |
| 53: | return []; |
| 54: | } |
| 55: | |
| 56: | public function getObjectClassNames(): array |
| 57: | { |
| 58: | return []; |
| 59: | } |
| 60: | |
| 61: | public function getObjectClassReflections(): array |
| 62: | { |
| 63: | return []; |
| 64: | } |
| 65: | |
| 66: | public function getConstantStrings(): array |
| 67: | { |
| 68: | return []; |
| 69: | } |
| 70: | |
| 71: | public function accepts(Type $type, bool $strictTypes): TrinaryLogic |
| 72: | { |
| 73: | return $this->acceptsWithReason($type, $strictTypes)->result; |
| 74: | } |
| 75: | |
| 76: | public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult |
| 77: | { |
| 78: | if ($type instanceof CompoundType) { |
| 79: | return $type->isAcceptedWithReasonBy($this, $strictTypes); |
| 80: | } |
| 81: | |
| 82: | return new AcceptsResult($type->isNonEmptyString(), []); |
| 83: | } |
| 84: | |
| 85: | public function isSuperTypeOf(Type $type): TrinaryLogic |
| 86: | { |
| 87: | if ($type instanceof CompoundType) { |
| 88: | return $type->isSubTypeOf($this); |
| 89: | } |
| 90: | |
| 91: | if ($this->equals($type)) { |
| 92: | return TrinaryLogic::createYes(); |
| 93: | } |
| 94: | |
| 95: | if ($type->isNonFalsyString()->yes()) { |
| 96: | return TrinaryLogic::createYes(); |
| 97: | } |
| 98: | |
| 99: | return $type->isNonEmptyString(); |
| 100: | } |
| 101: | |
| 102: | public function isSubTypeOf(Type $otherType): TrinaryLogic |
| 103: | { |
| 104: | if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { |
| 105: | return $otherType->isSuperTypeOf($this); |
| 106: | } |
| 107: | |
| 108: | return $otherType->isNonEmptyString() |
| 109: | ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()); |
| 110: | } |
| 111: | |
| 112: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic |
| 113: | { |
| 114: | return $this->isAcceptedWithReasonBy($acceptingType, $strictTypes)->result; |
| 115: | } |
| 116: | |
| 117: | public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
| 118: | { |
| 119: | return new AcceptsResult($this->isSubTypeOf($acceptingType), []); |
| 120: | } |
| 121: | |
| 122: | public function equals(Type $type): bool |
| 123: | { |
| 124: | return $type instanceof self; |
| 125: | } |
| 126: | |
| 127: | public function describe(VerbosityLevel $level): string |
| 128: | { |
| 129: | return 'non-empty-string'; |
| 130: | } |
| 131: | |
| 132: | public function isOffsetAccessible(): TrinaryLogic |
| 133: | { |
| 134: | return TrinaryLogic::createYes(); |
| 135: | } |
| 136: | |
| 137: | public function isOffsetAccessLegal(): TrinaryLogic |
| 138: | { |
| 139: | return TrinaryLogic::createYes(); |
| 140: | } |
| 141: | |
| 142: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
| 143: | { |
| 144: | return $offsetType->isInteger()->and(TrinaryLogic::createMaybe()); |
| 145: | } |
| 146: | |
| 147: | public function getOffsetValueType(Type $offsetType): Type |
| 148: | { |
| 149: | if ($this->hasOffsetValueType($offsetType)->no()) { |
| 150: | return new ErrorType(); |
| 151: | } |
| 152: | |
| 153: | if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes()) { |
| 154: | return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]); |
| 155: | } |
| 156: | |
| 157: | return new StringType(); |
| 158: | } |
| 159: | |
| 160: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
| 161: | { |
| 162: | $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues); |
| 163: | |
| 164: | if ($stringOffset instanceof ErrorType) { |
| 165: | return $stringOffset; |
| 166: | } |
| 167: | |
| 168: | return $this; |
| 169: | } |
| 170: | |
| 171: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
| 172: | { |
| 173: | return $this; |
| 174: | } |
| 175: | |
| 176: | public function unsetOffset(Type $offsetType): Type |
| 177: | { |
| 178: | return new ErrorType(); |
| 179: | } |
| 180: | |
| 181: | public function toNumber(): Type |
| 182: | { |
| 183: | return new ErrorType(); |
| 184: | } |
| 185: | |
| 186: | public function toAbsoluteNumber(): Type |
| 187: | { |
| 188: | return new ErrorType(); |
| 189: | } |
| 190: | |
| 191: | public function toInteger(): Type |
| 192: | { |
| 193: | return new IntegerType(); |
| 194: | } |
| 195: | |
| 196: | public function toFloat(): Type |
| 197: | { |
| 198: | return new FloatType(); |
| 199: | } |
| 200: | |
| 201: | public function toString(): Type |
| 202: | { |
| 203: | return $this; |
| 204: | } |
| 205: | |
| 206: | public function toArray(): Type |
| 207: | { |
| 208: | return new ConstantArrayType( |
| 209: | [new ConstantIntegerType(0)], |
| 210: | [$this], |
| 211: | [1], |
| 212: | [], |
| 213: | TrinaryLogic::createYes(), |
| 214: | ); |
| 215: | } |
| 216: | |
| 217: | public function toArrayKey(): Type |
| 218: | { |
| 219: | return $this; |
| 220: | } |
| 221: | |
| 222: | public function isNull(): TrinaryLogic |
| 223: | { |
| 224: | return TrinaryLogic::createNo(); |
| 225: | } |
| 226: | |
| 227: | public function isConstantValue(): TrinaryLogic |
| 228: | { |
| 229: | return TrinaryLogic::createMaybe(); |
| 230: | } |
| 231: | |
| 232: | public function isConstantScalarValue(): TrinaryLogic |
| 233: | { |
| 234: | return TrinaryLogic::createMaybe(); |
| 235: | } |
| 236: | |
| 237: | public function getConstantScalarTypes(): array |
| 238: | { |
| 239: | return []; |
| 240: | } |
| 241: | |
| 242: | public function getConstantScalarValues(): array |
| 243: | { |
| 244: | return []; |
| 245: | } |
| 246: | |
| 247: | public function isTrue(): TrinaryLogic |
| 248: | { |
| 249: | return TrinaryLogic::createNo(); |
| 250: | } |
| 251: | |
| 252: | public function isFalse(): TrinaryLogic |
| 253: | { |
| 254: | return TrinaryLogic::createNo(); |
| 255: | } |
| 256: | |
| 257: | public function isBoolean(): TrinaryLogic |
| 258: | { |
| 259: | return TrinaryLogic::createNo(); |
| 260: | } |
| 261: | |
| 262: | public function isFloat(): TrinaryLogic |
| 263: | { |
| 264: | return TrinaryLogic::createNo(); |
| 265: | } |
| 266: | |
| 267: | public function isInteger(): TrinaryLogic |
| 268: | { |
| 269: | return TrinaryLogic::createNo(); |
| 270: | } |
| 271: | |
| 272: | public function isString(): TrinaryLogic |
| 273: | { |
| 274: | return TrinaryLogic::createYes(); |
| 275: | } |
| 276: | |
| 277: | public function isNumericString(): TrinaryLogic |
| 278: | { |
| 279: | return TrinaryLogic::createMaybe(); |
| 280: | } |
| 281: | |
| 282: | public function isNonEmptyString(): TrinaryLogic |
| 283: | { |
| 284: | return TrinaryLogic::createYes(); |
| 285: | } |
| 286: | |
| 287: | public function isNonFalsyString(): TrinaryLogic |
| 288: | { |
| 289: | return TrinaryLogic::createMaybe(); |
| 290: | } |
| 291: | |
| 292: | public function isLiteralString(): TrinaryLogic |
| 293: | { |
| 294: | return TrinaryLogic::createMaybe(); |
| 295: | } |
| 296: | |
| 297: | public function isClassStringType(): TrinaryLogic |
| 298: | { |
| 299: | return TrinaryLogic::createMaybe(); |
| 300: | } |
| 301: | |
| 302: | public function getClassStringObjectType(): Type |
| 303: | { |
| 304: | return new ObjectWithoutClassType(); |
| 305: | } |
| 306: | |
| 307: | public function getObjectTypeOrClassStringObjectType(): Type |
| 308: | { |
| 309: | return new ObjectWithoutClassType(); |
| 310: | } |
| 311: | |
| 312: | public function isVoid(): TrinaryLogic |
| 313: | { |
| 314: | return TrinaryLogic::createNo(); |
| 315: | } |
| 316: | |
| 317: | public function isScalar(): TrinaryLogic |
| 318: | { |
| 319: | return TrinaryLogic::createYes(); |
| 320: | } |
| 321: | |
| 322: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
| 323: | { |
| 324: | return new BooleanType(); |
| 325: | } |
| 326: | |
| 327: | public function traverse(callable $cb): Type |
| 328: | { |
| 329: | return $this; |
| 330: | } |
| 331: | |
| 332: | public function traverseSimultaneously(Type $right, callable $cb): Type |
| 333: | { |
| 334: | return $this; |
| 335: | } |
| 336: | |
| 337: | public function generalize(GeneralizePrecision $precision): Type |
| 338: | { |
| 339: | return new StringType(); |
| 340: | } |
| 341: | |
| 342: | public static function __set_state(array $properties): Type |
| 343: | { |
| 344: | return new self(); |
| 345: | } |
| 346: | |
| 347: | public function tryRemove(Type $typeToRemove): ?Type |
| 348: | { |
| 349: | if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') { |
| 350: | return TypeCombinator::intersect($this, new AccessoryNonFalsyStringType()); |
| 351: | } |
| 352: | |
| 353: | return null; |
| 354: | } |
| 355: | |
| 356: | public function exponentiate(Type $exponent): Type |
| 357: | { |
| 358: | return new BenevolentUnionType([ |
| 359: | new FloatType(), |
| 360: | new IntegerType(), |
| 361: | ]); |
| 362: | } |
| 363: | |
| 364: | public function getFiniteTypes(): array |
| 365: | { |
| 366: | return []; |
| 367: | } |
| 368: | |
| 369: | public function toPhpDocNode(): TypeNode |
| 370: | { |
| 371: | return new IdentifierTypeNode('non-empty-string'); |
| 372: | } |
| 373: | |
| 374: | } |
| 375: | |