| 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\BooleanType; |
| 11: | use PHPStan\Type\CompoundType; |
| 12: | use PHPStan\Type\Constant\ConstantBooleanType; |
| 13: | use PHPStan\Type\Constant\ConstantFloatType; |
| 14: | use PHPStan\Type\Constant\ConstantIntegerType; |
| 15: | use PHPStan\Type\ErrorType; |
| 16: | use PHPStan\Type\IntegerRangeType; |
| 17: | use PHPStan\Type\IntersectionType; |
| 18: | use PHPStan\Type\IsSuperTypeOfResult; |
| 19: | use PHPStan\Type\MixedType; |
| 20: | use PHPStan\Type\Traits\MaybeCallableTypeTrait; |
| 21: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
| 22: | use PHPStan\Type\Traits\NonGenericTypeTrait; |
| 23: | use PHPStan\Type\Traits\NonObjectTypeTrait; |
| 24: | use PHPStan\Type\Traits\NonRemoveableTypeTrait; |
| 25: | use PHPStan\Type\Traits\TruthyBooleanTypeTrait; |
| 26: | use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
| 27: | use PHPStan\Type\Type; |
| 28: | use PHPStan\Type\UnionType; |
| 29: | use PHPStan\Type\VerbosityLevel; |
| 30: | |
| 31: | class NonEmptyArrayType implements CompoundType, AccessoryType |
| 32: | { |
| 33: | |
| 34: | use MaybeCallableTypeTrait; |
| 35: | use NonObjectTypeTrait; |
| 36: | use TruthyBooleanTypeTrait; |
| 37: | use NonGenericTypeTrait; |
| 38: | use UndecidedComparisonCompoundTypeTrait; |
| 39: | use NonRemoveableTypeTrait; |
| 40: | use NonGeneralizableTypeTrait; |
| 41: | |
| 42: | |
| 43: | public function __construct() |
| 44: | { |
| 45: | } |
| 46: | |
| 47: | public function getReferencedClasses(): array |
| 48: | { |
| 49: | return []; |
| 50: | } |
| 51: | |
| 52: | public function getObjectClassNames(): array |
| 53: | { |
| 54: | return []; |
| 55: | } |
| 56: | |
| 57: | public function getObjectClassReflections(): array |
| 58: | { |
| 59: | return []; |
| 60: | } |
| 61: | |
| 62: | public function getArrays(): array |
| 63: | { |
| 64: | return []; |
| 65: | } |
| 66: | |
| 67: | public function getConstantArrays(): array |
| 68: | { |
| 69: | return []; |
| 70: | } |
| 71: | |
| 72: | public function getConstantStrings(): array |
| 73: | { |
| 74: | return []; |
| 75: | } |
| 76: | |
| 77: | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
| 78: | { |
| 79: | if ($type instanceof CompoundType) { |
| 80: | return $type->isAcceptedBy($this, $strictTypes); |
| 81: | } |
| 82: | |
| 83: | $isArray = $type->isArray(); |
| 84: | $isIterableAtLeastOnce = $type->isIterableAtLeastOnce(); |
| 85: | |
| 86: | return new AcceptsResult($isArray->and($isIterableAtLeastOnce), []); |
| 87: | } |
| 88: | |
| 89: | public function isSuperTypeOf(Type $type): IsSuperTypeOfResult |
| 90: | { |
| 91: | if ($this->equals($type)) { |
| 92: | return IsSuperTypeOfResult::createYes(); |
| 93: | } |
| 94: | |
| 95: | if ($type instanceof CompoundType) { |
| 96: | return $type->isSubTypeOf($this); |
| 97: | } |
| 98: | |
| 99: | return new IsSuperTypeOfResult($type->isArray()->and($type->isIterableAtLeastOnce()), []); |
| 100: | } |
| 101: | |
| 102: | public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
| 103: | { |
| 104: | if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { |
| 105: | return $otherType->isSuperTypeOf($this); |
| 106: | } |
| 107: | |
| 108: | return (new IsSuperTypeOfResult($otherType->isArray()->and($otherType->isIterableAtLeastOnce()), [])) |
| 109: | ->and($otherType instanceof self ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createMaybe()); |
| 110: | } |
| 111: | |
| 112: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
| 113: | { |
| 114: | return $this->isSubTypeOf($acceptingType)->toAcceptsResult(); |
| 115: | } |
| 116: | |
| 117: | public function equals(Type $type): bool |
| 118: | { |
| 119: | return $type instanceof self; |
| 120: | } |
| 121: | |
| 122: | public function describe(VerbosityLevel $level): string |
| 123: | { |
| 124: | return 'non-empty-array'; |
| 125: | } |
| 126: | |
| 127: | public function isOffsetAccessible(): TrinaryLogic |
| 128: | { |
| 129: | return TrinaryLogic::createYes(); |
| 130: | } |
| 131: | |
| 132: | public function isOffsetAccessLegal(): TrinaryLogic |
| 133: | { |
| 134: | return TrinaryLogic::createYes(); |
| 135: | } |
| 136: | |
| 137: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
| 138: | { |
| 139: | return TrinaryLogic::createMaybe(); |
| 140: | } |
| 141: | |
| 142: | public function getOffsetValueType(Type $offsetType): Type |
| 143: | { |
| 144: | return new MixedType(); |
| 145: | } |
| 146: | |
| 147: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
| 148: | { |
| 149: | return $this; |
| 150: | } |
| 151: | |
| 152: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
| 153: | { |
| 154: | return $this; |
| 155: | } |
| 156: | |
| 157: | public function unsetOffset(Type $offsetType): Type |
| 158: | { |
| 159: | return new ErrorType(); |
| 160: | } |
| 161: | |
| 162: | public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type |
| 163: | { |
| 164: | return new ErrorType(); |
| 165: | } |
| 166: | |
| 167: | public function getKeysArray(): Type |
| 168: | { |
| 169: | return $this; |
| 170: | } |
| 171: | |
| 172: | public function getValuesArray(): Type |
| 173: | { |
| 174: | return $this; |
| 175: | } |
| 176: | |
| 177: | public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type |
| 178: | { |
| 179: | return $this; |
| 180: | } |
| 181: | |
| 182: | public function fillKeysArray(Type $valueType): Type |
| 183: | { |
| 184: | return $this; |
| 185: | } |
| 186: | |
| 187: | public function flipArray(): Type |
| 188: | { |
| 189: | return $this; |
| 190: | } |
| 191: | |
| 192: | public function intersectKeyArray(Type $otherArraysType): Type |
| 193: | { |
| 194: | return new MixedType(); |
| 195: | } |
| 196: | |
| 197: | public function popArray(): Type |
| 198: | { |
| 199: | return new MixedType(); |
| 200: | } |
| 201: | |
| 202: | public function reverseArray(TrinaryLogic $preserveKeys): Type |
| 203: | { |
| 204: | return $this; |
| 205: | } |
| 206: | |
| 207: | public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type |
| 208: | { |
| 209: | return new MixedType(); |
| 210: | } |
| 211: | |
| 212: | public function shiftArray(): Type |
| 213: | { |
| 214: | return new MixedType(); |
| 215: | } |
| 216: | |
| 217: | public function shuffleArray(): Type |
| 218: | { |
| 219: | return $this; |
| 220: | } |
| 221: | |
| 222: | public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type |
| 223: | { |
| 224: | if ((new ConstantIntegerType(0))->isSuperTypeOf($offsetType)->yes() && $lengthType->isNull()->yes()) { |
| 225: | return $this; |
| 226: | } |
| 227: | |
| 228: | return new MixedType(); |
| 229: | } |
| 230: | |
| 231: | public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type |
| 232: | { |
| 233: | if ( |
| 234: | (new ConstantIntegerType(0))->isSuperTypeOf($lengthType)->yes() |
| 235: | || $replacementType->toArray()->isIterableAtLeastOnce()->yes() |
| 236: | ) { |
| 237: | return $this; |
| 238: | } |
| 239: | |
| 240: | return new MixedType(); |
| 241: | } |
| 242: | |
| 243: | public function isIterable(): TrinaryLogic |
| 244: | { |
| 245: | return TrinaryLogic::createYes(); |
| 246: | } |
| 247: | |
| 248: | public function isIterableAtLeastOnce(): TrinaryLogic |
| 249: | { |
| 250: | return TrinaryLogic::createYes(); |
| 251: | } |
| 252: | |
| 253: | public function getArraySize(): Type |
| 254: | { |
| 255: | return IntegerRangeType::fromInterval(1, null); |
| 256: | } |
| 257: | |
| 258: | public function getIterableKeyType(): Type |
| 259: | { |
| 260: | return new MixedType(); |
| 261: | } |
| 262: | |
| 263: | public function getFirstIterableKeyType(): Type |
| 264: | { |
| 265: | return new MixedType(); |
| 266: | } |
| 267: | |
| 268: | public function getLastIterableKeyType(): Type |
| 269: | { |
| 270: | return new MixedType(); |
| 271: | } |
| 272: | |
| 273: | public function getIterableValueType(): Type |
| 274: | { |
| 275: | return new MixedType(); |
| 276: | } |
| 277: | |
| 278: | public function getFirstIterableValueType(): Type |
| 279: | { |
| 280: | return new MixedType(); |
| 281: | } |
| 282: | |
| 283: | public function getLastIterableValueType(): Type |
| 284: | { |
| 285: | return new MixedType(); |
| 286: | } |
| 287: | |
| 288: | public function isArray(): TrinaryLogic |
| 289: | { |
| 290: | return TrinaryLogic::createYes(); |
| 291: | } |
| 292: | |
| 293: | public function isConstantArray(): TrinaryLogic |
| 294: | { |
| 295: | return TrinaryLogic::createMaybe(); |
| 296: | } |
| 297: | |
| 298: | public function isOversizedArray(): TrinaryLogic |
| 299: | { |
| 300: | return TrinaryLogic::createMaybe(); |
| 301: | } |
| 302: | |
| 303: | public function isList(): TrinaryLogic |
| 304: | { |
| 305: | return TrinaryLogic::createMaybe(); |
| 306: | } |
| 307: | |
| 308: | public function isNull(): TrinaryLogic |
| 309: | { |
| 310: | return TrinaryLogic::createNo(); |
| 311: | } |
| 312: | |
| 313: | public function isConstantValue(): TrinaryLogic |
| 314: | { |
| 315: | return TrinaryLogic::createMaybe(); |
| 316: | } |
| 317: | |
| 318: | public function isConstantScalarValue(): TrinaryLogic |
| 319: | { |
| 320: | return TrinaryLogic::createNo(); |
| 321: | } |
| 322: | |
| 323: | public function getConstantScalarTypes(): array |
| 324: | { |
| 325: | return []; |
| 326: | } |
| 327: | |
| 328: | public function getConstantScalarValues(): array |
| 329: | { |
| 330: | return []; |
| 331: | } |
| 332: | |
| 333: | public function isTrue(): TrinaryLogic |
| 334: | { |
| 335: | return TrinaryLogic::createNo(); |
| 336: | } |
| 337: | |
| 338: | public function isFalse(): TrinaryLogic |
| 339: | { |
| 340: | return TrinaryLogic::createNo(); |
| 341: | } |
| 342: | |
| 343: | public function isBoolean(): TrinaryLogic |
| 344: | { |
| 345: | return TrinaryLogic::createNo(); |
| 346: | } |
| 347: | |
| 348: | public function isFloat(): TrinaryLogic |
| 349: | { |
| 350: | return TrinaryLogic::createNo(); |
| 351: | } |
| 352: | |
| 353: | public function isInteger(): TrinaryLogic |
| 354: | { |
| 355: | return TrinaryLogic::createNo(); |
| 356: | } |
| 357: | |
| 358: | public function isString(): TrinaryLogic |
| 359: | { |
| 360: | return TrinaryLogic::createNo(); |
| 361: | } |
| 362: | |
| 363: | public function isNumericString(): TrinaryLogic |
| 364: | { |
| 365: | return TrinaryLogic::createNo(); |
| 366: | } |
| 367: | |
| 368: | public function isNonEmptyString(): TrinaryLogic |
| 369: | { |
| 370: | return TrinaryLogic::createNo(); |
| 371: | } |
| 372: | |
| 373: | public function isNonFalsyString(): TrinaryLogic |
| 374: | { |
| 375: | return TrinaryLogic::createNo(); |
| 376: | } |
| 377: | |
| 378: | public function isLiteralString(): TrinaryLogic |
| 379: | { |
| 380: | return TrinaryLogic::createNo(); |
| 381: | } |
| 382: | |
| 383: | public function isLowercaseString(): TrinaryLogic |
| 384: | { |
| 385: | return TrinaryLogic::createNo(); |
| 386: | } |
| 387: | |
| 388: | public function isClassString(): TrinaryLogic |
| 389: | { |
| 390: | return TrinaryLogic::createNo(); |
| 391: | } |
| 392: | |
| 393: | public function isUppercaseString(): TrinaryLogic |
| 394: | { |
| 395: | return TrinaryLogic::createNo(); |
| 396: | } |
| 397: | |
| 398: | public function getClassStringObjectType(): Type |
| 399: | { |
| 400: | return new ErrorType(); |
| 401: | } |
| 402: | |
| 403: | public function getObjectTypeOrClassStringObjectType(): Type |
| 404: | { |
| 405: | return new ErrorType(); |
| 406: | } |
| 407: | |
| 408: | public function isVoid(): TrinaryLogic |
| 409: | { |
| 410: | return TrinaryLogic::createNo(); |
| 411: | } |
| 412: | |
| 413: | public function isScalar(): TrinaryLogic |
| 414: | { |
| 415: | return TrinaryLogic::createNo(); |
| 416: | } |
| 417: | |
| 418: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
| 419: | { |
| 420: | if ($type->isArray()->yes() && $type->isIterableAtLeastOnce()->no()) { |
| 421: | return new ConstantBooleanType(false); |
| 422: | } |
| 423: | |
| 424: | return new BooleanType(); |
| 425: | } |
| 426: | |
| 427: | public function toNumber(): Type |
| 428: | { |
| 429: | return new ErrorType(); |
| 430: | } |
| 431: | |
| 432: | public function toAbsoluteNumber(): Type |
| 433: | { |
| 434: | return new ErrorType(); |
| 435: | } |
| 436: | |
| 437: | public function toInteger(): Type |
| 438: | { |
| 439: | return new ConstantIntegerType(1); |
| 440: | } |
| 441: | |
| 442: | public function toFloat(): Type |
| 443: | { |
| 444: | return new ConstantFloatType(1.0); |
| 445: | } |
| 446: | |
| 447: | public function toString(): Type |
| 448: | { |
| 449: | return new ErrorType(); |
| 450: | } |
| 451: | |
| 452: | public function toArray(): Type |
| 453: | { |
| 454: | return $this; |
| 455: | } |
| 456: | |
| 457: | public function toArrayKey(): Type |
| 458: | { |
| 459: | return new ErrorType(); |
| 460: | } |
| 461: | |
| 462: | public function toCoercedArgumentType(bool $strictTypes): Type |
| 463: | { |
| 464: | return $this; |
| 465: | } |
| 466: | |
| 467: | public function traverse(callable $cb): Type |
| 468: | { |
| 469: | return $this; |
| 470: | } |
| 471: | |
| 472: | public function traverseSimultaneously(Type $right, callable $cb): Type |
| 473: | { |
| 474: | return $this; |
| 475: | } |
| 476: | |
| 477: | public function exponentiate(Type $exponent): Type |
| 478: | { |
| 479: | return new ErrorType(); |
| 480: | } |
| 481: | |
| 482: | public function getFiniteTypes(): array |
| 483: | { |
| 484: | return []; |
| 485: | } |
| 486: | |
| 487: | public function toPhpDocNode(): TypeNode |
| 488: | { |
| 489: | return new IdentifierTypeNode('non-empty-array'); |
| 490: | } |
| 491: | |
| 492: | } |
| 493: | |