| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\PhpDoc; |
| 4: | |
| 5: | use Closure; |
| 6: | use Generator; |
| 7: | use Iterator; |
| 8: | use IteratorAggregate; |
| 9: | use Nette\Utils\Strings; |
| 10: | use PhpParser\Node\Name; |
| 11: | use PHPStan\Analyser\ConstantResolver; |
| 12: | use PHPStan\Analyser\NameScope; |
| 13: | use PHPStan\Analyser\ValueDependencyCollector; |
| 14: | use PHPStan\DependencyInjection\AutowiredParameter; |
| 15: | use PHPStan\DependencyInjection\AutowiredService; |
| 16: | use PHPStan\DependencyInjection\ReportUnsafeArrayStringKeyCastingToggle; |
| 17: | use PHPStan\PhpDoc\Tag\TemplateTag; |
| 18: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode; |
| 19: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode; |
| 20: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFloatNode; |
| 21: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode; |
| 22: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode; |
| 23: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode; |
| 24: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode; |
| 25: | use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode; |
| 26: | use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode; |
| 27: | use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode; |
| 28: | use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; |
| 29: | use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; |
| 30: | use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; |
| 31: | use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode; |
| 32: | use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode; |
| 33: | use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode; |
| 34: | use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode; |
| 35: | use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; |
| 36: | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 37: | use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; |
| 38: | use PHPStan\PhpDocParser\Ast\Type\InvalidTypeNode; |
| 39: | use PHPStan\PhpDocParser\Ast\Type\NameNode; |
| 40: | use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; |
| 41: | use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; |
| 42: | use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode; |
| 43: | use PHPStan\PhpDocParser\Ast\Type\TemplateDeclarationTypeNode; |
| 44: | use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; |
| 45: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 46: | use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 47: | use PHPStan\Reflection\Assertions; |
| 48: | use PHPStan\Reflection\Callables\SimpleImpurePoint; |
| 49: | use PHPStan\Reflection\InitializerExprContext; |
| 50: | use PHPStan\Reflection\InitializerExprTypeResolver; |
| 51: | use PHPStan\Reflection\Native\NativeParameterReflection; |
| 52: | use PHPStan\Reflection\PassedByReference; |
| 53: | use PHPStan\Reflection\ReflectionProvider; |
| 54: | use PHPStan\ShouldNotHappenException; |
| 55: | use PHPStan\TrinaryLogic; |
| 56: | use PHPStan\Type\Accessory\AccessoryArrayListType; |
| 57: | use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType; |
| 58: | use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
| 59: | use PHPStan\Type\Accessory\AccessoryLowercaseStringType; |
| 60: | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
| 61: | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
| 62: | use PHPStan\Type\Accessory\AccessoryNumericStringType; |
| 63: | use PHPStan\Type\Accessory\AccessoryUppercaseStringType; |
| 64: | use PHPStan\Type\Accessory\NonEmptyArrayType; |
| 65: | use PHPStan\Type\ArrayType; |
| 66: | use PHPStan\Type\BenevolentUnionType; |
| 67: | use PHPStan\Type\BooleanType; |
| 68: | use PHPStan\Type\CallableAssertionsHelper; |
| 69: | use PHPStan\Type\CallableType; |
| 70: | use PHPStan\Type\ClassConstantAccessType; |
| 71: | use PHPStan\Type\ClassStringType; |
| 72: | use PHPStan\Type\ClosureType; |
| 73: | use PHPStan\Type\ConditionalType; |
| 74: | use PHPStan\Type\ConditionalTypeForParameter; |
| 75: | use PHPStan\Type\Constant\ConstantArrayType; |
| 76: | use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
| 77: | use PHPStan\Type\Constant\ConstantBooleanType; |
| 78: | use PHPStan\Type\Constant\ConstantFloatType; |
| 79: | use PHPStan\Type\Constant\ConstantIntegerType; |
| 80: | use PHPStan\Type\Constant\ConstantStringType; |
| 81: | use PHPStan\Type\Enum\EnumCaseObjectType; |
| 82: | use PHPStan\Type\ErrorType; |
| 83: | use PHPStan\Type\FloatRangeType; |
| 84: | use PHPStan\Type\FloatType; |
| 85: | use PHPStan\Type\Generic\GenericClassStringType; |
| 86: | use PHPStan\Type\Generic\GenericObjectType; |
| 87: | use PHPStan\Type\Generic\GenericStaticType; |
| 88: | use PHPStan\Type\Generic\TemplateSelfReferenceTarget; |
| 89: | use PHPStan\Type\Generic\TemplateType; |
| 90: | use PHPStan\Type\Generic\TemplateTypeFactory; |
| 91: | use PHPStan\Type\Generic\TemplateTypeMap; |
| 92: | use PHPStan\Type\Generic\TemplateTypeScope; |
| 93: | use PHPStan\Type\Generic\TemplateTypeVariance; |
| 94: | use PHPStan\Type\Helper\GetTemplateTypeType; |
| 95: | use PHPStan\Type\IntegerRangeType; |
| 96: | use PHPStan\Type\IntegerType; |
| 97: | use PHPStan\Type\IntersectionType; |
| 98: | use PHPStan\Type\IterableType; |
| 99: | use PHPStan\Type\KeyOfType; |
| 100: | use PHPStan\Type\LateResolvableArrayShapeType; |
| 101: | use PHPStan\Type\LateResolvableObjectShapeType; |
| 102: | use PHPStan\Type\LateResolvableType; |
| 103: | use PHPStan\Type\MixedType; |
| 104: | use PHPStan\Type\NewObjectType; |
| 105: | use PHPStan\Type\NonAcceptingNeverType; |
| 106: | use PHPStan\Type\NonexistentParentClassType; |
| 107: | use PHPStan\Type\NullType; |
| 108: | use PHPStan\Type\ObjectType; |
| 109: | use PHPStan\Type\ObjectWithoutClassType; |
| 110: | use PHPStan\Type\OffsetAccessType; |
| 111: | use PHPStan\Type\RecursiveType; |
| 112: | use PHPStan\Type\RecursiveTypeReference; |
| 113: | use PHPStan\Type\ResourceType; |
| 114: | use PHPStan\Type\StaticType; |
| 115: | use PHPStan\Type\StaticTypeFactory; |
| 116: | use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType; |
| 117: | use PHPStan\Type\StringNeverAcceptingObjectWithToStringType; |
| 118: | use PHPStan\Type\StringType; |
| 119: | use PHPStan\Type\ThisType; |
| 120: | use PHPStan\Type\Type; |
| 121: | use PHPStan\Type\TypeAliasResolver; |
| 122: | use PHPStan\Type\TypeAliasResolverProvider; |
| 123: | use PHPStan\Type\TypeCombinator; |
| 124: | use PHPStan\Type\TypeTraverser; |
| 125: | use PHPStan\Type\TypeUtils; |
| 126: | use PHPStan\Type\UnionType; |
| 127: | use PHPStan\Type\ValueOfType; |
| 128: | use PHPStan\Type\VoidType; |
| 129: | use Traversable; |
| 130: | use function array_key_exists; |
| 131: | use function array_keys; |
| 132: | use function array_map; |
| 133: | use function array_merge; |
| 134: | use function array_values; |
| 135: | use function count; |
| 136: | use function explode; |
| 137: | use function get_class; |
| 138: | use function in_array; |
| 139: | use function is_nan; |
| 140: | use function max; |
| 141: | use function min; |
| 142: | use function preg_match; |
| 143: | use function preg_quote; |
| 144: | use function str_contains; |
| 145: | use function str_replace; |
| 146: | use function str_starts_with; |
| 147: | use function strtolower; |
| 148: | use function substr; |
| 149: | use const INF; |
| 150: | use const PHP_FLOAT_MAX; |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | #[AutowiredService] |
| 156: | final class TypeNodeResolver |
| 157: | { |
| 158: | |
| 159: | |
| 160: | private array $genericTypeResolvingStack = []; |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | private int $recursiveTypeDepth = 0; |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | public function __construct( |
| 172: | private TypeNodeResolverExtensionRegistryProvider $extensionRegistryProvider, |
| 173: | private ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider, |
| 174: | private TypeAliasResolverProvider $typeAliasResolverProvider, |
| 175: | private ConstantResolver $constantResolver, |
| 176: | private InitializerExprTypeResolver $initializerExprTypeResolver, |
| 177: | #[AutowiredParameter] |
| 178: | private ?string $reportUnsafeArrayStringKeyCasting, |
| 179: | private ValueDependencyCollector $valueDependencyCollector, |
| 180: | ) |
| 181: | { |
| 182: | } |
| 183: | |
| 184: | |
| 185: | public function resolve(TypeNode $typeNode, NameScope $nameScope): Type |
| 186: | { |
| 187: | |
| 188: | |
| 189: | $this->valueDependencyCollector->enterDeclaration($nameScope->getFileName(), $nameScope->getClassName(), $nameScope->getFunctionName()); |
| 190: | $recursiveTypeDepth = $this->recursiveTypeDepth; |
| 191: | $this->recursiveTypeDepth = 0; |
| 192: | try { |
| 193: | return $this->resolveType($typeNode, $nameScope); |
| 194: | } finally { |
| 195: | $this->recursiveTypeDepth = $recursiveTypeDepth; |
| 196: | $this->valueDependencyCollector->leaveDeclaration(); |
| 197: | } |
| 198: | } |
| 199: | |
| 200: | |
| 201: | private function resolveType(TypeNode $typeNode, NameScope $nameScope): Type |
| 202: | { |
| 203: | foreach ($this->extensionRegistryProvider->getRegistry()->getExtensions() as $extension) { |
| 204: | $type = $extension->resolve($typeNode, $nameScope); |
| 205: | if ($type !== null) { |
| 206: | return $type; |
| 207: | } |
| 208: | } |
| 209: | |
| 210: | if ($typeNode instanceof NameNode) { |
| 211: | return $this->resolveNameNode($typeNode, $nameScope); |
| 212: | |
| 213: | } elseif ($typeNode instanceof IdentifierTypeNode) { |
| 214: | return $this->resolveIdentifierTypeNode($typeNode, $nameScope); |
| 215: | |
| 216: | } elseif ($typeNode instanceof ThisTypeNode) { |
| 217: | return $this->resolveThisTypeNode($nameScope); |
| 218: | |
| 219: | } elseif ($typeNode instanceof NullableTypeNode) { |
| 220: | return $this->resolveNullableTypeNode($typeNode, $nameScope); |
| 221: | |
| 222: | } elseif ($typeNode instanceof UnionTypeNode) { |
| 223: | return $this->resolveUnionTypeNode($typeNode, $nameScope); |
| 224: | |
| 225: | } elseif ($typeNode instanceof IntersectionTypeNode) { |
| 226: | return $this->resolveIntersectionTypeNode($typeNode, $nameScope); |
| 227: | |
| 228: | } elseif ($typeNode instanceof ConditionalTypeNode) { |
| 229: | return $this->resolveConditionalTypeNode($typeNode, $nameScope); |
| 230: | |
| 231: | } elseif ($typeNode instanceof ConditionalTypeForParameterNode) { |
| 232: | return $this->resolveConditionalTypeForParameterNode($typeNode, $nameScope); |
| 233: | |
| 234: | } elseif ($typeNode instanceof ArrayTypeNode) { |
| 235: | return $this->resolveArrayTypeNode($typeNode, $nameScope); |
| 236: | |
| 237: | } elseif ($typeNode instanceof GenericTypeNode) { |
| 238: | return $this->resolveGenericTypeNode($typeNode, $nameScope); |
| 239: | |
| 240: | } elseif ($typeNode instanceof CallableTypeNode) { |
| 241: | return $this->resolveCallableTypeNode($typeNode, $nameScope); |
| 242: | |
| 243: | } elseif ($typeNode instanceof ArrayShapeNode) { |
| 244: | return $this->resolveArrayShapeNode($typeNode, $nameScope); |
| 245: | } elseif ($typeNode instanceof ObjectShapeNode) { |
| 246: | return $this->resolveObjectShapeNode($typeNode, $nameScope); |
| 247: | } elseif ($typeNode instanceof ConstTypeNode) { |
| 248: | return $this->resolveConstTypeNode($typeNode, $nameScope); |
| 249: | } elseif ($typeNode instanceof OffsetAccessTypeNode) { |
| 250: | return $this->resolveOffsetAccessNode($typeNode, $nameScope); |
| 251: | } elseif ($typeNode instanceof InvalidTypeNode) { |
| 252: | return new MixedType(true); |
| 253: | } elseif ($typeNode instanceof TemplateDeclarationTypeNode) { |
| 254: | |
| 255: | return new ErrorType(); |
| 256: | } |
| 257: | |
| 258: | return new ErrorType(); |
| 259: | } |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | private function resolveNameNode(NameNode $typeNode, NameScope $nameScope): Type |
| 265: | { |
| 266: | return new ObjectType($nameScope->resolveStringName($typeNode->toCodeString())); |
| 267: | } |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | private function resolveRecursiveTypeNode(GenericTypeNode $typeNode, NameScope $nameScope): Type |
| 275: | { |
| 276: | if (count($typeNode->genericTypes) !== 1) { |
| 277: | return new ErrorType(); |
| 278: | } |
| 279: | |
| 280: | $this->recursiveTypeDepth++; |
| 281: | $id = '#' . $this->recursiveTypeDepth; |
| 282: | try { |
| 283: | $body = $this->resolveType($typeNode->genericTypes[0], $nameScope); |
| 284: | } finally { |
| 285: | $this->recursiveTypeDepth--; |
| 286: | } |
| 287: | |
| 288: | if (!RecursiveType::hasFreeReferences($body, $id)) { |
| 289: | return $body; |
| 290: | } |
| 291: | |
| 292: | if (!RecursiveType::isGuarded($body, $id)) { |
| 293: | return new ErrorType(); |
| 294: | } |
| 295: | |
| 296: | return new RecursiveType($body, $id, null); |
| 297: | } |
| 298: | |
| 299: | |
| 300: | private function resolveOuterRecursionTypeNode(GenericTypeNode $typeNode): Type |
| 301: | { |
| 302: | if (count($typeNode->genericTypes) !== 1) { |
| 303: | return new ErrorType(); |
| 304: | } |
| 305: | |
| 306: | $levelNode = $typeNode->genericTypes[0]; |
| 307: | if ( |
| 308: | !$levelNode instanceof ConstTypeNode |
| 309: | || !$levelNode->constExpr instanceof ConstExprIntegerNode |
| 310: | ) { |
| 311: | return new ErrorType(); |
| 312: | } |
| 313: | |
| 314: | $level = (int) $levelNode->constExpr->value; |
| 315: | if ($level < 1 || $level > $this->recursiveTypeDepth) { |
| 316: | return new ErrorType(); |
| 317: | } |
| 318: | |
| 319: | return new RecursiveTypeReference('#' . ($this->recursiveTypeDepth - $level + 1), null); |
| 320: | } |
| 321: | |
| 322: | private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameScope $nameScope): Type |
| 323: | { |
| 324: | switch (strtolower($typeNode->name)) { |
| 325: | case '__recursion': |
| 326: | if ($this->recursiveTypeDepth === 0) { |
| 327: | return new ErrorType(); |
| 328: | } |
| 329: | |
| 330: | return new RecursiveTypeReference('#' . $this->recursiveTypeDepth, null); |
| 331: | |
| 332: | case 'int': |
| 333: | return new IntegerType(); |
| 334: | |
| 335: | case 'integer': |
| 336: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 337: | |
| 338: | if ($type !== null) { |
| 339: | return $type; |
| 340: | } |
| 341: | |
| 342: | return new IntegerType(); |
| 343: | |
| 344: | case 'positive-int': |
| 345: | return IntegerRangeType::fromInterval(1, null); |
| 346: | |
| 347: | case 'negative-int': |
| 348: | return IntegerRangeType::fromInterval(null, -1); |
| 349: | |
| 350: | case 'non-positive-int': |
| 351: | return IntegerRangeType::fromInterval(null, 0); |
| 352: | |
| 353: | case 'non-negative-int': |
| 354: | return IntegerRangeType::fromInterval(0, null); |
| 355: | |
| 356: | case 'positive-float': |
| 357: | return FloatRangeType::fromInterval(5.0E-324, INF); |
| 358: | |
| 359: | case 'negative-float': |
| 360: | return FloatRangeType::fromInterval(-INF, -5.0E-324); |
| 361: | |
| 362: | case 'non-positive-float': |
| 363: | return FloatRangeType::fromInterval(-INF, 0.0); |
| 364: | |
| 365: | case 'non-negative-float': |
| 366: | return FloatRangeType::fromInterval(0.0, INF); |
| 367: | |
| 368: | case 'finite-float': |
| 369: | return FloatRangeType::fromInterval(-PHP_FLOAT_MAX, PHP_FLOAT_MAX); |
| 370: | |
| 371: | case 'non-zero-int': |
| 372: | return new UnionType([ |
| 373: | IntegerRangeType::fromInterval(null, -1), |
| 374: | IntegerRangeType::fromInterval(1, null), |
| 375: | ]); |
| 376: | |
| 377: | case 'string': |
| 378: | return new StringType(); |
| 379: | |
| 380: | case 'decimal-int-string': |
| 381: | return new IntersectionType([new StringType(), new AccessoryDecimalIntegerStringType()]); |
| 382: | |
| 383: | case 'non-decimal-int-string': |
| 384: | return new IntersectionType([ |
| 385: | new StringType(), |
| 386: | new AccessoryDecimalIntegerStringType(inverse: true), |
| 387: | ]); |
| 388: | |
| 389: | case 'lowercase-string': |
| 390: | return new IntersectionType([new StringType(), new AccessoryLowercaseStringType()]); |
| 391: | |
| 392: | case 'uppercase-string': |
| 393: | return new IntersectionType([new StringType(), new AccessoryUppercaseStringType()]); |
| 394: | |
| 395: | case 'literal-string': |
| 396: | return new IntersectionType([new StringType(), new AccessoryLiteralStringType()]); |
| 397: | |
| 398: | case 'class-string': |
| 399: | case 'interface-string': |
| 400: | case 'trait-string': |
| 401: | return new ClassStringType(); |
| 402: | |
| 403: | case 'enum-string': |
| 404: | return new GenericClassStringType(new ObjectType('UnitEnum')); |
| 405: | |
| 406: | case 'callable-string': |
| 407: | return new IntersectionType([new StringType(), new CallableType()]); |
| 408: | |
| 409: | case 'array-key': |
| 410: | return new BenevolentUnionType([new IntegerType(), new StringType()]); |
| 411: | |
| 412: | case 'scalar': |
| 413: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 414: | |
| 415: | if ($type !== null) { |
| 416: | return $type; |
| 417: | } |
| 418: | |
| 419: | return new UnionType([new IntegerType(), new FloatType(), new StringType(), new BooleanType()]); |
| 420: | |
| 421: | case 'empty-scalar': |
| 422: | return TypeCombinator::intersect( |
| 423: | new UnionType([new IntegerType(), new FloatType(), new StringType(), new BooleanType()]), |
| 424: | StaticTypeFactory::falsey(), |
| 425: | ); |
| 426: | |
| 427: | case 'non-empty-scalar': |
| 428: | return TypeCombinator::remove( |
| 429: | new UnionType([new IntegerType(), new FloatType(), new StringType(), new BooleanType()]), |
| 430: | StaticTypeFactory::falsey(), |
| 431: | ); |
| 432: | |
| 433: | case 'number': |
| 434: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 435: | |
| 436: | if ($type !== null) { |
| 437: | return $type; |
| 438: | } |
| 439: | |
| 440: | return new UnionType([new IntegerType(), new FloatType()]); |
| 441: | |
| 442: | case 'numeric': |
| 443: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 444: | |
| 445: | if ($type !== null) { |
| 446: | return $type; |
| 447: | } |
| 448: | |
| 449: | return new UnionType([ |
| 450: | new IntegerType(), |
| 451: | new FloatType(), |
| 452: | new IntersectionType([ |
| 453: | new StringType(), |
| 454: | new AccessoryNumericStringType(), |
| 455: | ]), |
| 456: | ]); |
| 457: | |
| 458: | case 'numeric-string': |
| 459: | return new IntersectionType([ |
| 460: | new StringType(), |
| 461: | new AccessoryNumericStringType(), |
| 462: | ]); |
| 463: | |
| 464: | case 'non-empty-string': |
| 465: | return new IntersectionType([ |
| 466: | new StringType(), |
| 467: | new AccessoryNonEmptyStringType(), |
| 468: | ]); |
| 469: | |
| 470: | case 'non-empty-lowercase-string': |
| 471: | return new IntersectionType([ |
| 472: | new StringType(), |
| 473: | new AccessoryNonEmptyStringType(), |
| 474: | new AccessoryLowercaseStringType(), |
| 475: | ]); |
| 476: | |
| 477: | case 'non-empty-uppercase-string': |
| 478: | return new IntersectionType([ |
| 479: | new StringType(), |
| 480: | new AccessoryNonEmptyStringType(), |
| 481: | new AccessoryUppercaseStringType(), |
| 482: | ]); |
| 483: | |
| 484: | case 'truthy-string': |
| 485: | case 'non-falsy-string': |
| 486: | return new IntersectionType([ |
| 487: | new StringType(), |
| 488: | new AccessoryNonFalsyStringType(), |
| 489: | ]); |
| 490: | |
| 491: | case 'non-empty-literal-string': |
| 492: | return new IntersectionType([ |
| 493: | new StringType(), |
| 494: | new AccessoryNonEmptyStringType(), |
| 495: | new AccessoryLiteralStringType(), |
| 496: | ]); |
| 497: | |
| 498: | case 'bool': |
| 499: | return new BooleanType(); |
| 500: | |
| 501: | case 'boolean': |
| 502: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 503: | |
| 504: | if ($type !== null) { |
| 505: | return $type; |
| 506: | } |
| 507: | |
| 508: | return new BooleanType(); |
| 509: | |
| 510: | case 'true': |
| 511: | return new ConstantBooleanType(true); |
| 512: | |
| 513: | case 'false': |
| 514: | return new ConstantBooleanType(false); |
| 515: | |
| 516: | case 'null': |
| 517: | return new NullType(); |
| 518: | |
| 519: | case 'float': |
| 520: | return new FloatType(); |
| 521: | |
| 522: | case 'double': |
| 523: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 524: | |
| 525: | if ($type !== null) { |
| 526: | return $type; |
| 527: | } |
| 528: | |
| 529: | return new FloatType(); |
| 530: | |
| 531: | case 'array': |
| 532: | case 'associative-array': |
| 533: | return new ArrayType(new MixedType(), new MixedType()); |
| 534: | |
| 535: | case 'non-empty-array': |
| 536: | return new IntersectionType([ |
| 537: | new ArrayType(new MixedType(), new MixedType()), |
| 538: | new NonEmptyArrayType(), |
| 539: | ]); |
| 540: | |
| 541: | case 'iterable': |
| 542: | return new IterableType(new MixedType(), new MixedType()); |
| 543: | |
| 544: | case 'callable': |
| 545: | return new CallableType(); |
| 546: | |
| 547: | case 'pure-callable': |
| 548: | return new CallableType(isPure: TrinaryLogic::createYes()); |
| 549: | |
| 550: | case 'pure-closure': |
| 551: | return ClosureType::createPure(); |
| 552: | |
| 553: | case 'static-closure': |
| 554: | return new ClosureType(isStatic: TrinaryLogic::createYes()); |
| 555: | |
| 556: | case 'static-pure-closure': |
| 557: | return new ClosureType(impurePoints: [], isStatic: TrinaryLogic::createYes()); |
| 558: | |
| 559: | case 'resource': |
| 560: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 561: | |
| 562: | if ($type !== null) { |
| 563: | return $type; |
| 564: | } |
| 565: | |
| 566: | return new ResourceType(); |
| 567: | |
| 568: | case 'open-resource': |
| 569: | case 'closed-resource': |
| 570: | return new ResourceType(); |
| 571: | |
| 572: | case 'mixed': |
| 573: | return new MixedType(true); |
| 574: | |
| 575: | case 'non-empty-mixed': |
| 576: | return new MixedType(true, StaticTypeFactory::falsey()); |
| 577: | |
| 578: | case 'void': |
| 579: | return new VoidType(); |
| 580: | |
| 581: | case 'object': |
| 582: | return new ObjectWithoutClassType(); |
| 583: | |
| 584: | case 'callable-object': |
| 585: | return new IntersectionType([new ObjectWithoutClassType(), new CallableType()]); |
| 586: | |
| 587: | case 'callable-array': |
| 588: | return new IntersectionType([new ArrayType(new MixedType(), new MixedType()), new CallableType()]); |
| 589: | |
| 590: | case 'never': |
| 591: | case 'noreturn': |
| 592: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 593: | |
| 594: | if ($type !== null) { |
| 595: | return $type; |
| 596: | } |
| 597: | |
| 598: | return new NonAcceptingNeverType(); |
| 599: | |
| 600: | case 'never-return': |
| 601: | case 'never-returns': |
| 602: | case 'no-return': |
| 603: | return new NonAcceptingNeverType(); |
| 604: | |
| 605: | case 'list': |
| 606: | return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new MixedType()), new AccessoryArrayListType()]); |
| 607: | case 'non-empty-list': |
| 608: | return new IntersectionType([ |
| 609: | new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new MixedType()), |
| 610: | new NonEmptyArrayType(), |
| 611: | new AccessoryArrayListType(), |
| 612: | ]); |
| 613: | |
| 614: | case 'empty': |
| 615: | $type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope); |
| 616: | if ($type !== null) { |
| 617: | return $type; |
| 618: | } |
| 619: | |
| 620: | return StaticTypeFactory::falsey(); |
| 621: | case '__stringandstringable': |
| 622: | return new StringAlwaysAcceptingObjectWithToStringType(); |
| 623: | case '__stringnotstringable': |
| 624: | return new StringNeverAcceptingObjectWithToStringType(); |
| 625: | } |
| 626: | |
| 627: | if ($nameScope->getClassName() !== null) { |
| 628: | switch (strtolower($typeNode->name)) { |
| 629: | case 'self': |
| 630: | return new ObjectType($nameScope->getClassName()); |
| 631: | |
| 632: | case 'static': |
| 633: | if ($this->getReflectionProvider()->hasClass($nameScope->getClassName())) { |
| 634: | $classReflection = $this->getReflectionProvider()->getClass($nameScope->getClassName()); |
| 635: | |
| 636: | return new StaticType($classReflection); |
| 637: | } |
| 638: | |
| 639: | return new ErrorType(); |
| 640: | case 'parent': |
| 641: | if ($this->getReflectionProvider()->hasClass($nameScope->getClassName())) { |
| 642: | $classReflection = $this->getReflectionProvider()->getClass($nameScope->getClassName()); |
| 643: | $parentClass = $classReflection->getNativeReflection()->getParentClass(); |
| 644: | if ($parentClass !== false) { |
| 645: | return new ObjectType($parentClass->getName()); |
| 646: | } |
| 647: | } |
| 648: | |
| 649: | return new NonexistentParentClassType(); |
| 650: | } |
| 651: | } |
| 652: | |
| 653: | if (!$nameScope->shouldBypassTypeAliases()) { |
| 654: | $typeAlias = $this->getTypeAliasResolver()->resolveTypeAlias($typeNode->name, $nameScope); |
| 655: | if ($typeAlias !== null) { |
| 656: | return $typeAlias; |
| 657: | } |
| 658: | } |
| 659: | |
| 660: | $templateType = $nameScope->resolveTemplateTypeName($typeNode->name); |
| 661: | if ($templateType !== null) { |
| 662: | return $templateType; |
| 663: | } |
| 664: | |
| 665: | $stringName = $nameScope->resolveStringName($typeNode->name); |
| 666: | if (str_contains($stringName, '-') && !str_starts_with($stringName, 'OCI-')) { |
| 667: | return new ErrorType(); |
| 668: | } |
| 669: | |
| 670: | if ($this->mightBeConstant($typeNode->name) && !$this->getReflectionProvider()->hasClass($stringName)) { |
| 671: | $constType = $this->tryResolveConstant($typeNode->name, $nameScope); |
| 672: | if ($constType !== null) { |
| 673: | return $constType; |
| 674: | } |
| 675: | } |
| 676: | |
| 677: | return new ObjectType($stringName); |
| 678: | } |
| 679: | |
| 680: | private function mightBeConstant(string $name): bool |
| 681: | { |
| 682: | return preg_match('((?:^|\\\\)[A-Z_][A-Z0-9_]*$)', $name) > 0; |
| 683: | } |
| 684: | |
| 685: | private function tryResolveConstant(string $name, NameScope $nameScope): ?Type |
| 686: | { |
| 687: | foreach ($nameScope->resolveConstantNames($name) as $constName) { |
| 688: | $nameNode = new Name\FullyQualified(explode('\\', $constName)); |
| 689: | $constType = $this->constantResolver->resolveConstant($nameNode, null); |
| 690: | if ($constType !== null) { |
| 691: | return $constType; |
| 692: | } |
| 693: | } |
| 694: | |
| 695: | return null; |
| 696: | } |
| 697: | |
| 698: | private function tryResolvePseudoTypeClassType(IdentifierTypeNode $typeNode, NameScope $nameScope): ?Type |
| 699: | { |
| 700: | if ($nameScope->hasUseAlias($typeNode->name)) { |
| 701: | return new ObjectType($nameScope->resolveStringName($typeNode->name)); |
| 702: | } |
| 703: | |
| 704: | if ($nameScope->getNamespace() === null) { |
| 705: | return null; |
| 706: | } |
| 707: | |
| 708: | $className = $nameScope->resolveStringName($typeNode->name); |
| 709: | |
| 710: | if ($this->getReflectionProvider()->hasClass($className)) { |
| 711: | return new ObjectType($className); |
| 712: | } |
| 713: | |
| 714: | return null; |
| 715: | } |
| 716: | |
| 717: | private function resolveThisTypeNode(NameScope $nameScope): Type |
| 718: | { |
| 719: | $className = $nameScope->getClassName(); |
| 720: | if ($className !== null) { |
| 721: | if ($this->getReflectionProvider()->hasClass($className)) { |
| 722: | return new ThisType($this->getReflectionProvider()->getClass($className)); |
| 723: | } |
| 724: | } |
| 725: | |
| 726: | return new ErrorType(); |
| 727: | } |
| 728: | |
| 729: | private function resolveNullableTypeNode(NullableTypeNode $typeNode, NameScope $nameScope): Type |
| 730: | { |
| 731: | return TypeCombinator::union($this->resolveType($typeNode->type, $nameScope), new NullType()); |
| 732: | } |
| 733: | |
| 734: | private function resolveUnionTypeNode(UnionTypeNode $typeNode, NameScope $nameScope): Type |
| 735: | { |
| 736: | $iterableTypeNodes = []; |
| 737: | $otherTypeNodes = []; |
| 738: | |
| 739: | foreach ($typeNode->types as $innerTypeNode) { |
| 740: | if ($innerTypeNode instanceof ArrayTypeNode) { |
| 741: | $iterableTypeNodes[] = $innerTypeNode->type; |
| 742: | } else { |
| 743: | $otherTypeNodes[] = $innerTypeNode; |
| 744: | } |
| 745: | } |
| 746: | |
| 747: | $otherTypeTypes = $this->resolveMultiple($otherTypeNodes, $nameScope); |
| 748: | if (count($iterableTypeNodes) > 0) { |
| 749: | $arrayTypeTypes = $this->resolveMultiple($iterableTypeNodes, $nameScope); |
| 750: | $arrayTypeType = TypeCombinator::union(...$arrayTypeTypes); |
| 751: | $addArray = true; |
| 752: | |
| 753: | foreach ($otherTypeTypes as &$type) { |
| 754: | if (!$type->isIterable()->yes() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) { |
| 755: | continue; |
| 756: | } |
| 757: | |
| 758: | if ($type instanceof ObjectType && !$type instanceof GenericObjectType) { |
| 759: | $type = new IntersectionType([$type, new IterableType(new MixedType(), $arrayTypeType)]); |
| 760: | } elseif ($type instanceof ArrayType) { |
| 761: | $type = new ArrayType(new MixedType(), $arrayTypeType); |
| 762: | } elseif ($type instanceof ConstantArrayType) { |
| 763: | $type = new ArrayType(new MixedType(), $arrayTypeType); |
| 764: | } elseif ($type instanceof IterableType) { |
| 765: | $type = new IterableType(new MixedType(), $arrayTypeType); |
| 766: | } else { |
| 767: | continue; |
| 768: | } |
| 769: | |
| 770: | $addArray = false; |
| 771: | } |
| 772: | |
| 773: | if ($addArray) { |
| 774: | $otherTypeTypes[] = new ArrayType(new MixedType(), $arrayTypeType); |
| 775: | } |
| 776: | } |
| 777: | |
| 778: | return TypeCombinator::union(...$otherTypeTypes); |
| 779: | } |
| 780: | |
| 781: | private function resolveIntersectionTypeNode(IntersectionTypeNode $typeNode, NameScope $nameScope): Type |
| 782: | { |
| 783: | $types = $this->resolveMultiple($typeNode->types, $nameScope); |
| 784: | $result = $types[0]; |
| 785: | for ($i = 1, $count = count($types); $i < $count; $i++) { |
| 786: | $result = TypeCombinator::intersect($result, $types[$i]); |
| 787: | } |
| 788: | return $result; |
| 789: | } |
| 790: | |
| 791: | private function resolveConditionalTypeNode(ConditionalTypeNode $typeNode, NameScope $nameScope): Type |
| 792: | { |
| 793: | return new ConditionalType( |
| 794: | $this->resolveType($typeNode->subjectType, $nameScope), |
| 795: | $this->resolveType($typeNode->targetType, $nameScope), |
| 796: | $this->resolveType($typeNode->if, $nameScope), |
| 797: | $this->resolveType($typeNode->else, $nameScope), |
| 798: | $typeNode->negated, |
| 799: | ); |
| 800: | } |
| 801: | |
| 802: | private function resolveConditionalTypeForParameterNode(ConditionalTypeForParameterNode $typeNode, NameScope $nameScope): Type |
| 803: | { |
| 804: | return new ConditionalTypeForParameter( |
| 805: | $typeNode->parameterName, |
| 806: | $this->resolveType($typeNode->targetType, $nameScope), |
| 807: | $this->resolveType($typeNode->if, $nameScope), |
| 808: | $this->resolveType($typeNode->else, $nameScope), |
| 809: | $typeNode->negated, |
| 810: | ); |
| 811: | } |
| 812: | |
| 813: | private function resolveArrayTypeNode(ArrayTypeNode $typeNode, NameScope $nameScope): Type |
| 814: | { |
| 815: | $itemType = $this->resolveType($typeNode->type, $nameScope); |
| 816: | return new ArrayType((new BenevolentUnionType([new IntegerType(), new StringType()]))->toArrayKey(), $itemType); |
| 817: | } |
| 818: | |
| 819: | private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $nameScope): Type |
| 820: | { |
| 821: | $mainTypeName = $typeNode->type instanceof NameNode ? null : strtolower($typeNode->type->name); |
| 822: | if ( |
| 823: | $mainTypeName === 'class-string-map' |
| 824: | || (isset($typeNode->genericTypes[0]) && $typeNode->genericTypes[0] instanceof TemplateDeclarationTypeNode) |
| 825: | ) { |
| 826: | return $this->resolveDependentArrayTypeNode($typeNode, $mainTypeName, $nameScope); |
| 827: | } |
| 828: | |
| 829: | if ($mainTypeName === '__recursive') { |
| 830: | return $this->resolveRecursiveTypeNode($typeNode, $nameScope); |
| 831: | } |
| 832: | |
| 833: | if ($mainTypeName === '__recursion') { |
| 834: | return $this->resolveOuterRecursionTypeNode($typeNode); |
| 835: | } |
| 836: | |
| 837: | $genericTypes = $this->resolveMultiple($typeNode->genericTypes, $nameScope); |
| 838: | |
| 839: | if ( |
| 840: | !$typeNode->type instanceof NameNode |
| 841: | && !$nameScope->shouldBypassTypeAliases() |
| 842: | && ( |
| 843: | $nameScope->hasTypeAlias($typeNode->type->name) |
| 844: | || $this->getTypeAliasResolver()->hasTypeAlias($typeNode->type->name, null) |
| 845: | ) |
| 846: | ) { |
| 847: | $aliasType = $this->getTypeAliasResolver()->resolveTypeAliasWithArguments($typeNode->type->name, $genericTypes, $nameScope); |
| 848: | if ($aliasType !== null) { |
| 849: | return $aliasType; |
| 850: | } |
| 851: | } |
| 852: | $variances = array_map( |
| 853: | static function (string $variance): TemplateTypeVariance { |
| 854: | switch ($variance) { |
| 855: | case GenericTypeNode::VARIANCE_INVARIANT: |
| 856: | return TemplateTypeVariance::createInvariant(); |
| 857: | case GenericTypeNode::VARIANCE_COVARIANT: |
| 858: | return TemplateTypeVariance::createCovariant(); |
| 859: | case GenericTypeNode::VARIANCE_CONTRAVARIANT: |
| 860: | return TemplateTypeVariance::createContravariant(); |
| 861: | case GenericTypeNode::VARIANCE_BIVARIANT: |
| 862: | return TemplateTypeVariance::createBivariant(); |
| 863: | } |
| 864: | }, |
| 865: | $typeNode->variances, |
| 866: | ); |
| 867: | |
| 868: | if (in_array($mainTypeName, ['array', 'non-empty-array'], true)) { |
| 869: | if (count($genericTypes) === 1) { |
| 870: | $arrayType = new ArrayType((new BenevolentUnionType([new IntegerType(), new StringType()]))->toArrayKey(), $genericTypes[0]); |
| 871: | } elseif (count($genericTypes) === 2) { |
| 872: | $keyType = $this->transformUnsafeArrayKey($genericTypes[0]); |
| 873: | $finiteTypes = $keyType->getFiniteTypes(); |
| 874: | if ( |
| 875: | count($finiteTypes) === 1 |
| 876: | && ($finiteTypes[0] instanceof ConstantStringType || $finiteTypes[0] instanceof ConstantIntegerType) |
| 877: | ) { |
| 878: | $arrayBuilder = ConstantArrayTypeBuilder::createEmpty(); |
| 879: | $arrayBuilder->setOffsetValueType($finiteTypes[0], $genericTypes[1], true); |
| 880: | $arrayType = $arrayBuilder->getArray(); |
| 881: | } else { |
| 882: | $arrayType = new ArrayType($keyType, $genericTypes[1]); |
| 883: | } |
| 884: | } else { |
| 885: | return new ErrorType(); |
| 886: | } |
| 887: | |
| 888: | if ($mainTypeName === 'non-empty-array') { |
| 889: | return TypeCombinator::intersect($arrayType, new NonEmptyArrayType()); |
| 890: | } |
| 891: | |
| 892: | return $arrayType; |
| 893: | } elseif (in_array($mainTypeName, ['list', 'non-empty-list'], true)) { |
| 894: | if (count($genericTypes) === 1) { |
| 895: | $listType = new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $genericTypes[0]), new AccessoryArrayListType()]); |
| 896: | if ($mainTypeName === 'non-empty-list') { |
| 897: | return TypeCombinator::intersect($listType, new NonEmptyArrayType()); |
| 898: | } |
| 899: | |
| 900: | return $listType; |
| 901: | } |
| 902: | |
| 903: | return new ErrorType(); |
| 904: | } elseif ($mainTypeName === 'iterable') { |
| 905: | if (count($genericTypes) === 1) { |
| 906: | return new IterableType(new MixedType(true), $genericTypes[0]); |
| 907: | |
| 908: | } |
| 909: | |
| 910: | if (count($genericTypes) === 2) { |
| 911: | return new IterableType($genericTypes[0], $genericTypes[1]); |
| 912: | } |
| 913: | } elseif (in_array($mainTypeName, ['class-string', 'interface-string'], true)) { |
| 914: | if (count($genericTypes) === 1) { |
| 915: | $genericType = $genericTypes[0]; |
| 916: | if ($genericType->isObject()->yes() || $genericType instanceof MixedType) { |
| 917: | return new GenericClassStringType($genericType); |
| 918: | } |
| 919: | } |
| 920: | |
| 921: | return new ErrorType(); |
| 922: | } elseif ($mainTypeName === 'enum-string') { |
| 923: | if (count($genericTypes) === 1) { |
| 924: | $genericType = $genericTypes[0]; |
| 925: | return new GenericClassStringType(TypeCombinator::intersect($genericType, new ObjectType('UnitEnum'))); |
| 926: | } |
| 927: | |
| 928: | return new ErrorType(); |
| 929: | } elseif ($mainTypeName === 'int') { |
| 930: | if (count($genericTypes) === 2) { |
| 931: | |
| 932: | if ($genericTypes[0] instanceof ConstantIntegerType) { |
| 933: | $min = $genericTypes[0]->getValue(); |
| 934: | } elseif ($typeNode->genericTypes[0] instanceof IdentifierTypeNode && $typeNode->genericTypes[0]->name === 'min') { |
| 935: | $min = null; |
| 936: | } else { |
| 937: | return new ErrorType(); |
| 938: | } |
| 939: | |
| 940: | if ($genericTypes[1] instanceof ConstantIntegerType) { |
| 941: | $max = $genericTypes[1]->getValue(); |
| 942: | } elseif ($typeNode->genericTypes[1] instanceof IdentifierTypeNode && $typeNode->genericTypes[1]->name === 'max') { |
| 943: | $max = null; |
| 944: | } else { |
| 945: | return new ErrorType(); |
| 946: | } |
| 947: | |
| 948: | return IntegerRangeType::fromInterval($min, $max); |
| 949: | } |
| 950: | } elseif ($mainTypeName === 'float') { |
| 951: | if (count($genericTypes) === 2) { |
| 952: | $min = $this->resolveFloatRangeBound($genericTypes[0], $typeNode->genericTypes[0], 'min', -INF); |
| 953: | $max = $this->resolveFloatRangeBound($genericTypes[1], $typeNode->genericTypes[1], 'max', INF); |
| 954: | if ($min === null || $max === null) { |
| 955: | return new ErrorType(); |
| 956: | } |
| 957: | |
| 958: | return FloatRangeType::fromInterval($min, $max); |
| 959: | } |
| 960: | } elseif ($mainTypeName === 'key-of') { |
| 961: | if (count($genericTypes) === 1) { |
| 962: | $type = new KeyOfType($genericTypes[0]); |
| 963: | return $type->isResolvable() ? $type->resolve() : $type; |
| 964: | } |
| 965: | |
| 966: | return new ErrorType(); |
| 967: | } elseif ($mainTypeName === 'value-of') { |
| 968: | if (count($genericTypes) === 1) { |
| 969: | $type = new ValueOfType($genericTypes[0]); |
| 970: | |
| 971: | return $type->isResolvable() ? $type->resolve() : $type; |
| 972: | } |
| 973: | |
| 974: | return new ErrorType(); |
| 975: | } elseif ($mainTypeName === 'int-mask-of') { |
| 976: | if (count($genericTypes) === 1) { |
| 977: | $maskType = $this->expandIntMaskToType($genericTypes[0]); |
| 978: | if ($maskType !== null) { |
| 979: | return $maskType; |
| 980: | } |
| 981: | } |
| 982: | |
| 983: | return new ErrorType(); |
| 984: | } elseif ($mainTypeName === 'int-mask') { |
| 985: | if (count($genericTypes) > 0) { |
| 986: | $maskType = $this->expandIntMaskToType(TypeCombinator::union(...$genericTypes)); |
| 987: | if ($maskType !== null) { |
| 988: | return $maskType; |
| 989: | } |
| 990: | } |
| 991: | |
| 992: | return new ErrorType(); |
| 993: | } elseif ($mainTypeName === '__benevolent') { |
| 994: | if (count($genericTypes) === 1) { |
| 995: | return TypeUtils::toBenevolentUnion($genericTypes[0]); |
| 996: | } |
| 997: | return new ErrorType(); |
| 998: | } elseif ($mainTypeName === 'template-type') { |
| 999: | if (count($genericTypes) === 3) { |
| 1000: | $result = []; |
| 1001: | |
| 1002: | foreach ($genericTypes[1]->getObjectClassNames() as $ancestorClassName) { |
| 1003: | foreach ($genericTypes[2]->getConstantStrings() as $templateTypeName) { |
| 1004: | $result[] = new GetTemplateTypeType($genericTypes[0], $ancestorClassName, $templateTypeName->getValue()); |
| 1005: | } |
| 1006: | } |
| 1007: | |
| 1008: | return TypeCombinator::union(...$result); |
| 1009: | } |
| 1010: | |
| 1011: | return new ErrorType(); |
| 1012: | } elseif ($mainTypeName === 'new') { |
| 1013: | if (count($genericTypes) === 1) { |
| 1014: | $type = new NewObjectType($genericTypes[0]); |
| 1015: | return $type->isResolvable() ? $type->resolve() : $type; |
| 1016: | } |
| 1017: | |
| 1018: | return new ErrorType(); |
| 1019: | } elseif ($mainTypeName === 'static') { |
| 1020: | if ($nameScope->getClassName() !== null && $this->getReflectionProvider()->hasClass($nameScope->getClassName())) { |
| 1021: | $classReflection = $this->getReflectionProvider()->getClass($nameScope->getClassName()); |
| 1022: | |
| 1023: | return new GenericStaticType($classReflection, $genericTypes, null, $variances); |
| 1024: | } |
| 1025: | |
| 1026: | return new ErrorType(); |
| 1027: | } |
| 1028: | |
| 1029: | $mainType = $typeNode->type instanceof NameNode |
| 1030: | ? $this->resolveNameNode($typeNode->type, $nameScope) |
| 1031: | : $this->resolveIdentifierTypeNode($typeNode->type, $nameScope); |
| 1032: | $mainTypeObjectClassNames = $mainType->getObjectClassNames(); |
| 1033: | if (count($mainTypeObjectClassNames) > 1) { |
| 1034: | if ($mainType instanceof TemplateType) { |
| 1035: | return new ErrorType(); |
| 1036: | } |
| 1037: | throw new ShouldNotHappenException(); |
| 1038: | } |
| 1039: | $mainTypeClassName = $mainTypeObjectClassNames[0] ?? null; |
| 1040: | |
| 1041: | if ($mainTypeClassName !== null) { |
| 1042: | if (!$this->getReflectionProvider()->hasClass($mainTypeClassName)) { |
| 1043: | return new GenericObjectType($mainTypeClassName, $genericTypes, variances: $variances); |
| 1044: | } |
| 1045: | |
| 1046: | $classReflection = $this->getReflectionProvider()->getClass($mainTypeClassName); |
| 1047: | if ($classReflection->isGeneric()) { |
| 1048: | $templateTypes = array_values($classReflection->getTemplateTypeMap()->getTypes()); |
| 1049: | $genericTypes = $this->fillTemplateDefaults($mainTypeClassName, $templateTypes, $genericTypes); |
| 1050: | |
| 1051: | if (in_array($mainTypeClassName, [ |
| 1052: | Traversable::class, |
| 1053: | IteratorAggregate::class, |
| 1054: | Iterator::class, |
| 1055: | ], true)) { |
| 1056: | if (count($genericTypes) === 1) { |
| 1057: | return new GenericObjectType($mainTypeClassName, [ |
| 1058: | new MixedType(true), |
| 1059: | $genericTypes[0], |
| 1060: | ], variances: [ |
| 1061: | TemplateTypeVariance::createInvariant(), |
| 1062: | $variances[0], |
| 1063: | ]); |
| 1064: | } |
| 1065: | |
| 1066: | if (count($genericTypes) === 2) { |
| 1067: | return new GenericObjectType($mainTypeClassName, [ |
| 1068: | $genericTypes[0], |
| 1069: | $genericTypes[1], |
| 1070: | ], variances: [ |
| 1071: | $variances[0], |
| 1072: | $variances[1], |
| 1073: | ]); |
| 1074: | } |
| 1075: | } |
| 1076: | if ($mainTypeClassName === Generator::class) { |
| 1077: | if (count($genericTypes) === 1) { |
| 1078: | $mixed = new MixedType(true); |
| 1079: | return new GenericObjectType($mainTypeClassName, [ |
| 1080: | $mixed, |
| 1081: | $genericTypes[0], |
| 1082: | $mixed, |
| 1083: | $mixed, |
| 1084: | ], variances: [ |
| 1085: | TemplateTypeVariance::createInvariant(), |
| 1086: | $variances[0], |
| 1087: | TemplateTypeVariance::createInvariant(), |
| 1088: | TemplateTypeVariance::createInvariant(), |
| 1089: | ]); |
| 1090: | } |
| 1091: | |
| 1092: | if (count($genericTypes) === 2) { |
| 1093: | $mixed = new MixedType(true); |
| 1094: | return new GenericObjectType($mainTypeClassName, [ |
| 1095: | $genericTypes[0], |
| 1096: | $genericTypes[1], |
| 1097: | $mixed, |
| 1098: | $mixed, |
| 1099: | ], variances: [ |
| 1100: | $variances[0], |
| 1101: | $variances[1], |
| 1102: | TemplateTypeVariance::createInvariant(), |
| 1103: | TemplateTypeVariance::createInvariant(), |
| 1104: | ]); |
| 1105: | } |
| 1106: | } |
| 1107: | |
| 1108: | if (!$mainType->isIterable()->yes()) { |
| 1109: | return new GenericObjectType($mainTypeClassName, $genericTypes, variances: $variances); |
| 1110: | } |
| 1111: | |
| 1112: | if ( |
| 1113: | count($genericTypes) !== 1 |
| 1114: | || $classReflection->getTemplateTypeMap()->count() === 1 |
| 1115: | ) { |
| 1116: | return new GenericObjectType($mainTypeClassName, $genericTypes, variances: $variances); |
| 1117: | } |
| 1118: | } |
| 1119: | } |
| 1120: | |
| 1121: | if ($mainType->isIterable()->yes()) { |
| 1122: | if ($mainTypeClassName !== null) { |
| 1123: | if (isset($this->genericTypeResolvingStack[$mainTypeClassName])) { |
| 1124: | return new ErrorType(); |
| 1125: | } |
| 1126: | |
| 1127: | $this->genericTypeResolvingStack[$mainTypeClassName] = true; |
| 1128: | } |
| 1129: | |
| 1130: | try { |
| 1131: | if (count($genericTypes) === 1) { |
| 1132: | return TypeCombinator::intersect( |
| 1133: | $mainType, |
| 1134: | new IterableType(new MixedType(true), $genericTypes[0]), |
| 1135: | ); |
| 1136: | } |
| 1137: | |
| 1138: | if (count($genericTypes) === 2) { |
| 1139: | return TypeCombinator::intersect( |
| 1140: | $mainType, |
| 1141: | new IterableType($genericTypes[0], $genericTypes[1]), |
| 1142: | ); |
| 1143: | } |
| 1144: | } finally { |
| 1145: | if ($mainTypeClassName !== null) { |
| 1146: | unset($this->genericTypeResolvingStack[$mainTypeClassName]); |
| 1147: | } |
| 1148: | } |
| 1149: | } |
| 1150: | |
| 1151: | if ($mainTypeClassName !== null) { |
| 1152: | return new GenericObjectType($mainTypeClassName, $genericTypes, variances: $variances); |
| 1153: | } |
| 1154: | |
| 1155: | return new ErrorType(); |
| 1156: | } |
| 1157: | |
| 1158: | private function transformUnsafeArrayKey(Type $keyType): Type |
| 1159: | { |
| 1160: | |
| 1161: | |
| 1162: | if ($this->isUnresolvedArrayKey($keyType)) { |
| 1163: | return $keyType; |
| 1164: | } |
| 1165: | |
| 1166: | if ($this->reportUnsafeArrayStringKeyCasting === ReportUnsafeArrayStringKeyCastingToggle::PREVENT) { |
| 1167: | if (!$keyType->isSuperTypeOf(new IntegerType())->yes()) { |
| 1168: | $keyType = TypeTraverser::map($keyType, static function (Type $type, callable $traverse) { |
| 1169: | if ($type instanceof UnionType || $type instanceof IntersectionType) { |
| 1170: | return $traverse($type); |
| 1171: | } |
| 1172: | |
| 1173: | if ($type instanceof StringType) { |
| 1174: | return TypeCombinator::intersect($type, new AccessoryDecimalIntegerStringType(inverse: true)); |
| 1175: | } |
| 1176: | |
| 1177: | return $type; |
| 1178: | }); |
| 1179: | } |
| 1180: | } |
| 1181: | |
| 1182: | return TypeCombinator::intersect($keyType->toArrayKey(), new UnionType([ |
| 1183: | new IntegerType(), |
| 1184: | new StringType(), |
| 1185: | ]))->toArrayKey(); |
| 1186: | } |
| 1187: | |
| 1188: | private function isUnresolvedArrayKey(Type $type): bool |
| 1189: | { |
| 1190: | $contains = false; |
| 1191: | $validKeys = true; |
| 1192: | $arrayKeyType = new UnionType([new IntegerType(), new StringType()]); |
| 1193: | TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$contains, &$validKeys, $arrayKeyType): Type { |
| 1194: | if (!$validKeys) { |
| 1195: | return $type; |
| 1196: | } |
| 1197: | |
| 1198: | if ($type instanceof LateResolvableType && !$type->isResolvable()) { |
| 1199: | $contains = true; |
| 1200: | $validKeys = $arrayKeyType->isSuperTypeOf($type->resolve())->yes(); |
| 1201: | return $type; |
| 1202: | } |
| 1203: | |
| 1204: | return $traverse($type); |
| 1205: | }); |
| 1206: | |
| 1207: | return $contains && $validKeys; |
| 1208: | } |
| 1209: | |
| 1210: | |
| 1211: | |
| 1212: | |
| 1213: | |
| 1214: | |
| 1215: | |
| 1216: | |
| 1217: | private function resolveDependentArrayTypeNode(GenericTypeNode $typeNode, ?string $mainTypeName, NameScope $nameScope): Type |
| 1218: | { |
| 1219: | if ( |
| 1220: | !in_array($mainTypeName, ['array', 'non-empty-array', 'class-string-map'], true) |
| 1221: | || count($typeNode->genericTypes) !== 2 |
| 1222: | || $typeNode->variances !== [GenericTypeNode::VARIANCE_INVARIANT, GenericTypeNode::VARIANCE_INVARIANT] |
| 1223: | ) { |
| 1224: | return new ErrorType(); |
| 1225: | } |
| 1226: | |
| 1227: | [$keyNode, $valueNode] = $typeNode->genericTypes; |
| 1228: | if ($keyNode instanceof TemplateDeclarationTypeNode) { |
| 1229: | $name = $keyNode->name; |
| 1230: | $bound = $this->resolveType($keyNode->bound, $nameScope); |
| 1231: | } elseif ($mainTypeName === 'class-string-map' && $keyNode instanceof IdentifierTypeNode) { |
| 1232: | $name = $keyNode->name; |
| 1233: | $bound = new ObjectWithoutClassType(); |
| 1234: | } else { |
| 1235: | return new ErrorType(); |
| 1236: | } |
| 1237: | if ($name === '') { |
| 1238: | return new ErrorType(); |
| 1239: | } |
| 1240: | |
| 1241: | if ($mainTypeName === 'class-string-map') { |
| 1242: | if (!$bound->isObject()->yes()) { |
| 1243: | return new ErrorType(); |
| 1244: | } |
| 1245: | } elseif (!$this->isArrayKeyTemplateBound($bound)) { |
| 1246: | return new ErrorType(); |
| 1247: | } |
| 1248: | |
| 1249: | [$template, $valueType] = $this->resolveWithArrayKeyTemplate($name, $bound, (string) $typeNode, $valueNode, $nameScope); |
| 1250: | $keyType = $mainTypeName === 'class-string-map' ? new GenericClassStringType($template) : $template; |
| 1251: | |
| 1252: | $arrayType = new ArrayType($keyType, $valueType); |
| 1253: | if ($mainTypeName === 'non-empty-array') { |
| 1254: | return TypeCombinator::intersect($arrayType, new NonEmptyArrayType()); |
| 1255: | } |
| 1256: | |
| 1257: | return $arrayType; |
| 1258: | } |
| 1259: | |
| 1260: | private function isArrayKeyTemplateBound(Type $bound): bool |
| 1261: | { |
| 1262: | return (new UnionType([new IntegerType(), new StringType()]))->isSuperTypeOf($bound)->yes(); |
| 1263: | } |
| 1264: | |
| 1265: | |
| 1266: | |
| 1267: | |
| 1268: | |
| 1269: | |
| 1270: | |
| 1271: | private function resolveWithArrayKeyTemplate(string $name, Type $bound, string $declaration, TypeNode $valueNode, NameScope $nameScope): array |
| 1272: | { |
| 1273: | $template = TemplateTypeFactory::create( |
| 1274: | TemplateTypeScope::createWithArrayKey($declaration), |
| 1275: | $name, |
| 1276: | $bound, |
| 1277: | TemplateTypeVariance::createInvariant(), |
| 1278: | ); |
| 1279: | $valueNameScope = $nameScope->withTemplateTypeMap( |
| 1280: | new TemplateTypeMap([$name => $template]), |
| 1281: | $nameScope->getTemplateTags(), |
| 1282: | ); |
| 1283: | |
| 1284: | return [$template, $this->resolveType($valueNode, $valueNameScope)]; |
| 1285: | } |
| 1286: | |
| 1287: | |
| 1288: | |
| 1289: | |
| 1290: | |
| 1291: | |
| 1292: | |
| 1293: | |
| 1294: | |
| 1295: | |
| 1296: | private function fillTemplateDefaults(string $className, array $templateTypes, array $genericTypes): array |
| 1297: | { |
| 1298: | $arguments = []; |
| 1299: | foreach ($templateTypes as $i => $templateType) { |
| 1300: | if (!$templateType instanceof TemplateType || !array_key_exists($i, $genericTypes)) { |
| 1301: | continue; |
| 1302: | } |
| 1303: | |
| 1304: | $arguments[$templateType->getName()] = $genericTypes[$i]; |
| 1305: | } |
| 1306: | |
| 1307: | |
| 1308: | $omittedDefaults = []; |
| 1309: | for ($i = count($genericTypes), $templateTypesCount = count($templateTypes); $i < $templateTypesCount; $i++) { |
| 1310: | $templateType = $templateTypes[$i]; |
| 1311: | if (!$templateType instanceof TemplateType || $templateType->getDefault() === null) { |
| 1312: | continue; |
| 1313: | } |
| 1314: | |
| 1315: | $omittedDefaults[$templateType->getName()] = $templateType->getDefault(); |
| 1316: | } |
| 1317: | |
| 1318: | |
| 1319: | |
| 1320: | |
| 1321: | $resolving = []; |
| 1322: | $argument = static function (string $name) use (&$argument, &$arguments, &$resolving, $omittedDefaults, $className): ?Type { |
| 1323: | if (array_key_exists($name, $arguments)) { |
| 1324: | return $arguments[$name]; |
| 1325: | } |
| 1326: | if (!array_key_exists($name, $omittedDefaults) || array_key_exists($name, $resolving)) { |
| 1327: | return null; |
| 1328: | } |
| 1329: | |
| 1330: | $resolving[$name] = true; |
| 1331: | $arguments[$name] = TypeTraverser::map($omittedDefaults[$name], static function (Type $type, callable $traverse) use (&$argument, $className): Type { |
| 1332: | if (!$type instanceof TemplateType) { |
| 1333: | return $traverse($type); |
| 1334: | } |
| 1335: | |
| 1336: | |
| 1337: | if ($type->getScope()->getClassName() !== $className || $type->getScope()->getFunctionName() !== null) { |
| 1338: | return $type; |
| 1339: | } |
| 1340: | |
| 1341: | return $argument($type->getName()) ?? $type; |
| 1342: | }); |
| 1343: | unset($resolving[$name]); |
| 1344: | |
| 1345: | return $arguments[$name]; |
| 1346: | }; |
| 1347: | |
| 1348: | foreach (array_keys($omittedDefaults) as $name) { |
| 1349: | $genericTypes[] = $argument($name) ?? $omittedDefaults[$name]; |
| 1350: | } |
| 1351: | |
| 1352: | return $genericTypes; |
| 1353: | } |
| 1354: | |
| 1355: | |
| 1356: | |
| 1357: | |
| 1358: | |
| 1359: | |
| 1360: | |
| 1361: | |
| 1362: | |
| 1363: | |
| 1364: | public function resolveTemplateTags(array $valueNodes, NameScope $nameScope, TemplateTypeScope $templateTypeScope, Type $boundWhenMissing, array $localTypeAliasNodes = []): array |
| 1365: | { |
| 1366: | if ($localTypeAliasNodes !== []) { |
| 1367: | foreach ($valueNodes as $name => [$valueNode, $variance]) { |
| 1368: | $valueNodes[$name] = [ |
| 1369: | new TemplateTagValueNode( |
| 1370: | $valueNode->name, |
| 1371: | $valueNode->bound !== null ? TemplateBoundAliasInliner::inline($valueNode->bound, $localTypeAliasNodes, $valueNodes) : null, |
| 1372: | $valueNode->description, |
| 1373: | $valueNode->default !== null ? TemplateBoundAliasInliner::inline($valueNode->default, $localTypeAliasNodes, $valueNodes) : null, |
| 1374: | $valueNode->lowerBound, |
| 1375: | ), |
| 1376: | $variance, |
| 1377: | ]; |
| 1378: | } |
| 1379: | } |
| 1380: | |
| 1381: | $this->valueDependencyCollector->enterDeclaration($nameScope->getFileName(), $nameScope->getClassName(), $nameScope->getFunctionName()); |
| 1382: | try { |
| 1383: | return $this->resolveTemplateTagsInDeclaration($valueNodes, $nameScope, $templateTypeScope, $boundWhenMissing); |
| 1384: | } finally { |
| 1385: | $this->valueDependencyCollector->leaveDeclaration(); |
| 1386: | } |
| 1387: | } |
| 1388: | |
| 1389: | |
| 1390: | |
| 1391: | |
| 1392: | |
| 1393: | private function resolveTemplateTagsInDeclaration(array $valueNodes, NameScope $nameScope, TemplateTypeScope $templateTypeScope, Type $boundWhenMissing): array |
| 1394: | { |
| 1395: | $resolvedTags = []; |
| 1396: | $resolvedTypes = []; |
| 1397: | $plainValueNodes = array_map(static fn (array $pair): TemplateTagValueNode => $pair[0], $valueNodes); |
| 1398: | $order = TemplateTagDependencyOrder::order($plainValueNodes); |
| 1399: | $circularBounds = TemplateTagDependencyOrder::findBareCycles($plainValueNodes, false); |
| 1400: | $circularDefaults = TemplateTagDependencyOrder::findBareCycles($plainValueNodes, true); |
| 1401: | foreach ($order as $group) { |
| 1402: | $selfReferenceTargets = []; |
| 1403: | foreach ($group as $groupName) { |
| 1404: | $selfReferenceTargets[$groupName] = new TemplateSelfReferenceTarget($templateTypeScope, $groupName, $valueNodes[$groupName][1]); |
| 1405: | } |
| 1406: | foreach ($group as $name) { |
| 1407: | [$valueNode, $variance] = $valueNodes[$name]; |
| 1408: | |
| 1409: | |
| 1410: | |
| 1411: | $groupTypes = $resolvedTypes; |
| 1412: | foreach ($group as $groupName) { |
| 1413: | if (array_key_exists($groupName, $groupTypes)) { |
| 1414: | continue; |
| 1415: | } |
| 1416: | $groupTypes[$groupName] = $selfReferenceTargets[$groupName]->getReference(); |
| 1417: | } |
| 1418: | $tagNameScope = $nameScope->withTemplateTypeMap(new TemplateTypeMap($groupTypes), array_merge($nameScope->getTemplateTags(), $resolvedTags)); |
| 1419: | |
| 1420: | foreach (array_keys($valueNodes) as $declaredName) { |
| 1421: | if (array_key_exists($declaredName, $groupTypes)) { |
| 1422: | continue; |
| 1423: | } |
| 1424: | $tagNameScope = $tagNameScope->unsetTemplateType($declaredName); |
| 1425: | } |
| 1426: | |
| 1427: | $circularBoundPath = $circularBounds[$name] ?? null; |
| 1428: | $circularDefaultPath = $circularDefaults[$name] ?? null; |
| 1429: | $tag = new TemplateTag( |
| 1430: | $valueNode->name, |
| 1431: | $valueNode->bound !== null && $circularBoundPath === null ? $this->resolveType($valueNode->bound, $tagNameScope) : $boundWhenMissing, |
| 1432: | $valueNode->default !== null && $circularDefaultPath === null ? $this->resolveType($valueNode->default, $tagNameScope) : null, |
| 1433: | $variance, |
| 1434: | $circularBoundPath, |
| 1435: | $circularDefaultPath, |
| 1436: | ); |
| 1437: | $resolvedTags[$name] = $tag; |
| 1438: | $resolvedTypes[$name] = TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag); |
| 1439: | $selfReferenceTargets[$name]->setTemplate($resolvedTypes[$name]); |
| 1440: | } |
| 1441: | } |
| 1442: | |
| 1443: | $tags = []; |
| 1444: | foreach (array_keys($valueNodes) as $name) { |
| 1445: | $tags[$name] = $resolvedTags[$name]; |
| 1446: | } |
| 1447: | |
| 1448: | return $tags; |
| 1449: | } |
| 1450: | |
| 1451: | private function resolveCallableTypeNode(CallableTypeNode $typeNode, NameScope $nameScope): Type |
| 1452: | { |
| 1453: | $templateTags = []; |
| 1454: | |
| 1455: | if (count($typeNode->templateTypes) > 0) { |
| 1456: | $templateTypeScope = TemplateTypeScope::createWithType((string) $typeNode); |
| 1457: | $valueNodes = []; |
| 1458: | foreach ($typeNode->templateTypes as $templateType) { |
| 1459: | $valueNodes[$templateType->name] = [$templateType, TemplateTypeVariance::createInvariant()]; |
| 1460: | } |
| 1461: | $templateTags = $this->resolveTemplateTagsInDeclaration($valueNodes, $nameScope, $templateTypeScope, new MixedType()); |
| 1462: | |
| 1463: | $templateTypeMap = new TemplateTypeMap(array_map( |
| 1464: | static fn (TemplateTag $tag): Type => TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag), |
| 1465: | $templateTags, |
| 1466: | )); |
| 1467: | |
| 1468: | $nameScope = $nameScope->withTemplateTypeMap($templateTypeMap, $templateTags); |
| 1469: | } else { |
| 1470: | $templateTypeMap = TemplateTypeMap::createEmpty(); |
| 1471: | } |
| 1472: | |
| 1473: | $mainType = $this->resolveType($typeNode->identifier, $nameScope); |
| 1474: | |
| 1475: | $isVariadic = false; |
| 1476: | $parameters = array_values(array_map( |
| 1477: | function (CallableTypeParameterNode $parameterNode) use ($nameScope, &$isVariadic): NativeParameterReflection { |
| 1478: | $isVariadic = $isVariadic || $parameterNode->isVariadic; |
| 1479: | $parameterName = $parameterNode->parameterName; |
| 1480: | if (str_starts_with($parameterName, '$')) { |
| 1481: | $parameterName = substr($parameterName, 1); |
| 1482: | } |
| 1483: | |
| 1484: | return new NativeParameterReflection( |
| 1485: | $parameterName, |
| 1486: | $parameterNode->isOptional || $parameterNode->isVariadic, |
| 1487: | $this->resolveType($parameterNode->type, $nameScope), |
| 1488: | $parameterNode->isReference ? PassedByReference::createCreatesNewVariable() : PassedByReference::createNo(), |
| 1489: | $parameterNode->isVariadic, |
| 1490: | defaultValue: null, |
| 1491: | ); |
| 1492: | }, |
| 1493: | $typeNode->parameters, |
| 1494: | )); |
| 1495: | |
| 1496: | $assertions = $this->resolveCallableReturnTypeAssertions($typeNode, $nameScope, $parameters); |
| 1497: | if ($assertions !== null) { |
| 1498: | $returnType = new BooleanType(); |
| 1499: | } else { |
| 1500: | $returnType = $this->resolveType($typeNode->returnType, $nameScope); |
| 1501: | } |
| 1502: | |
| 1503: | if ($mainType instanceof CallableType) { |
| 1504: | $pure = $mainType->isPure(); |
| 1505: | if ($pure->yes() && $returnType->isVoid()->yes()) { |
| 1506: | return new ErrorType(); |
| 1507: | } |
| 1508: | |
| 1509: | return new CallableType($parameters, $returnType, $isVariadic, $templateTypeMap, templateTags: $templateTags, isPure: $pure, assertions: $assertions); |
| 1510: | |
| 1511: | } elseif ( |
| 1512: | $mainType instanceof ObjectType |
| 1513: | && $mainType->getClassName() === Closure::class |
| 1514: | ) { |
| 1515: | return new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, templateTags: $templateTags, impurePoints: [ |
| 1516: | new SimpleImpurePoint( |
| 1517: | 'functionCall', |
| 1518: | 'call to a Closure', |
| 1519: | false, |
| 1520: | ), |
| 1521: | ], assertions: $assertions); |
| 1522: | } elseif ($mainType instanceof ClosureType) { |
| 1523: | $closure = new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, templateTags: $templateTags, impurePoints: $mainType->getImpurePoints(), invalidateExpressions: $mainType->getInvalidateExpressions(), usedVariables: $mainType->getUsedVariables(), acceptsNamedArguments: $mainType->acceptsNamedArguments(), mustUseReturnValue: $mainType->mustUseReturnValue(), assertions: $assertions, isStatic: $mainType->isStaticClosure()); |
| 1524: | if ($closure->isPure()->yes() && $returnType->isVoid()->yes()) { |
| 1525: | return new ErrorType(); |
| 1526: | } |
| 1527: | |
| 1528: | return $closure; |
| 1529: | } |
| 1530: | |
| 1531: | return new ErrorType(); |
| 1532: | } |
| 1533: | |
| 1534: | |
| 1535: | |
| 1536: | |
| 1537: | |
| 1538: | |
| 1539: | |
| 1540: | private function resolveCallableReturnTypeAssertions(CallableTypeNode $typeNode, NameScope $nameScope, array $parameters): ?Assertions |
| 1541: | { |
| 1542: | $returnTypeNode = $typeNode->returnType; |
| 1543: | if (!$returnTypeNode instanceof ConditionalTypeForParameterNode) { |
| 1544: | return null; |
| 1545: | } |
| 1546: | |
| 1547: | foreach ($parameters as $parameter) { |
| 1548: | if ('$' . $parameter->getName() !== $returnTypeNode->parameterName) { |
| 1549: | continue; |
| 1550: | } |
| 1551: | |
| 1552: | return CallableAssertionsHelper::createAssertionsFromConditional( |
| 1553: | $returnTypeNode->parameterName, |
| 1554: | $this->resolveType($returnTypeNode->targetType, $nameScope), |
| 1555: | $returnTypeNode->negated, |
| 1556: | $this->resolveType($returnTypeNode->if, $nameScope), |
| 1557: | $this->resolveType($returnTypeNode->else, $nameScope), |
| 1558: | ); |
| 1559: | } |
| 1560: | |
| 1561: | return null; |
| 1562: | } |
| 1563: | |
| 1564: | private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $nameScope): Type |
| 1565: | { |
| 1566: | $items = []; |
| 1567: | foreach ($typeNode->items as $itemNode) { |
| 1568: | $items[] = [ |
| 1569: | $this->resolveArrayShapeOffsetType($itemNode, $nameScope), |
| 1570: | $this->resolveType($itemNode->valueType, $nameScope), |
| 1571: | $itemNode->optional, |
| 1572: | ]; |
| 1573: | } |
| 1574: | |
| 1575: | $unsealed = null; |
| 1576: | if (!$typeNode->sealed && $typeNode->unsealedType !== null && $typeNode->unsealedType->keyType instanceof TemplateDeclarationTypeNode) { |
| 1577: | |
| 1578: | $keyNode = $typeNode->unsealedType->keyType; |
| 1579: | $bound = $this->resolveType($keyNode->bound, $nameScope); |
| 1580: | if ($keyNode->name === '' || !$this->isArrayKeyTemplateBound($bound)) { |
| 1581: | return new ErrorType(); |
| 1582: | } |
| 1583: | |
| 1584: | $unsealed = $this->resolveWithArrayKeyTemplate($keyNode->name, $bound, (string) $typeNode, $typeNode->unsealedType->valueType, $nameScope); |
| 1585: | } elseif (!$typeNode->sealed) { |
| 1586: | |
| 1587: | |
| 1588: | $unsealedKeyType = $typeNode->unsealedType === null || $typeNode->unsealedType->keyType === null |
| 1589: | ? null |
| 1590: | : $this->transformUnsafeArrayKey($this->resolveType($typeNode->unsealedType->keyType, $nameScope)); |
| 1591: | |
| 1592: | $unsealedValueType = $typeNode->unsealedType === null |
| 1593: | ? new MixedType() |
| 1594: | : $this->resolveType($typeNode->unsealedType->valueType, $nameScope); |
| 1595: | |
| 1596: | $unsealed = [$unsealedKeyType, $unsealedValueType]; |
| 1597: | } |
| 1598: | |
| 1599: | return LateResolvableArrayShapeType::create($items, $unsealed, $typeNode->kind); |
| 1600: | } |
| 1601: | |
| 1602: | private function resolveArrayShapeOffsetType(ArrayShapeItemNode $itemNode, NameScope $nameScope): ?Type |
| 1603: | { |
| 1604: | if ($itemNode->keyName instanceof ConstExprIntegerNode) { |
| 1605: | return new ConstantIntegerType((int) $itemNode->keyName->value); |
| 1606: | } elseif ($itemNode->keyName instanceof IdentifierTypeNode) { |
| 1607: | $templateType = $nameScope->resolveTemplateTypeName($itemNode->keyName->name); |
| 1608: | if ($templateType !== null) { |
| 1609: | return $templateType; |
| 1610: | } |
| 1611: | |
| 1612: | return new ConstantStringType($itemNode->keyName->name); |
| 1613: | } elseif ($itemNode->keyName instanceof ConstExprStringNode) { |
| 1614: | return new ConstantStringType($itemNode->keyName->value); |
| 1615: | } elseif ($itemNode->keyName instanceof ConstFetchNode) { |
| 1616: | $constExpr = $itemNode->keyName; |
| 1617: | if ($constExpr->className === '') { |
| 1618: | throw new ShouldNotHappenException(); |
| 1619: | } |
| 1620: | |
| 1621: | $isStatic = false; |
| 1622: | if ($nameScope->getClassName() !== null) { |
| 1623: | switch (strtolower($constExpr->className)) { |
| 1624: | case 'static': |
| 1625: | $className = $nameScope->getClassName(); |
| 1626: | $isStatic = true; |
| 1627: | break; |
| 1628: | |
| 1629: | case 'self': |
| 1630: | $className = $nameScope->getClassName(); |
| 1631: | break; |
| 1632: | |
| 1633: | case 'parent': |
| 1634: | if ($this->getReflectionProvider()->hasClass($nameScope->getClassName())) { |
| 1635: | $classReflection = $this->getReflectionProvider()->getClass($nameScope->getClassName()); |
| 1636: | if ($classReflection->getParentClass() === null) { |
| 1637: | return new ErrorType(); |
| 1638: | |
| 1639: | } |
| 1640: | |
| 1641: | $className = $classReflection->getParentClass()->getName(); |
| 1642: | } |
| 1643: | break; |
| 1644: | } |
| 1645: | } |
| 1646: | |
| 1647: | if (!isset($className)) { |
| 1648: | $className = $nameScope->resolveStringName($constExpr->className); |
| 1649: | } |
| 1650: | |
| 1651: | if (!$this->getReflectionProvider()->hasClass($className)) { |
| 1652: | return new ErrorType(); |
| 1653: | } |
| 1654: | $classReflection = $this->getReflectionProvider()->getClass($className); |
| 1655: | |
| 1656: | if ($isStatic && $classReflection->isFinal()) { |
| 1657: | $isStatic = false; |
| 1658: | } |
| 1659: | |
| 1660: | $constantName = $constExpr->name; |
| 1661: | if (strtolower($constantName) === 'class') { |
| 1662: | if ($isStatic) { |
| 1663: | return new GenericClassStringType(new StaticType($classReflection)); |
| 1664: | } |
| 1665: | |
| 1666: | return new ConstantStringType($classReflection->getName(), true); |
| 1667: | } |
| 1668: | |
| 1669: | if (!$classReflection->hasConstant($constantName)) { |
| 1670: | return new ErrorType(); |
| 1671: | } |
| 1672: | |
| 1673: | if ($isStatic) { |
| 1674: | return new ClassConstantAccessType(new StaticType($classReflection), $constantName); |
| 1675: | } |
| 1676: | |
| 1677: | $reflectionConstant = $classReflection->getNativeReflection()->getReflectionConstant($constantName); |
| 1678: | if ($reflectionConstant === false) { |
| 1679: | return new ErrorType(); |
| 1680: | } |
| 1681: | $declaringClass = $reflectionConstant->getDeclaringClass(); |
| 1682: | |
| 1683: | return $this->initializerExprTypeResolver->getType($reflectionConstant->getValueExpression(), InitializerExprContext::fromClass($declaringClass->getName(), $declaringClass->getFileName() ?: null)); |
| 1684: | } elseif ($itemNode->keyName !== null) { |
| 1685: | throw new ShouldNotHappenException('Unsupported key node type: ' . get_class($itemNode->keyName)); |
| 1686: | } |
| 1687: | |
| 1688: | return null; |
| 1689: | } |
| 1690: | |
| 1691: | private function resolveObjectShapeNode(ObjectShapeNode $typeNode, NameScope $nameScope): Type |
| 1692: | { |
| 1693: | $items = []; |
| 1694: | foreach ($typeNode->items as $itemNode) { |
| 1695: | if ($itemNode->keyName instanceof IdentifierTypeNode) { |
| 1696: | $keyType = $nameScope->resolveTemplateTypeName($itemNode->keyName->name) |
| 1697: | ?? new ConstantStringType($itemNode->keyName->name); |
| 1698: | } else { |
| 1699: | $keyType = new ConstantStringType($itemNode->keyName->value); |
| 1700: | } |
| 1701: | |
| 1702: | $items[] = [$keyType, $this->resolveType($itemNode->valueType, $nameScope), $itemNode->optional]; |
| 1703: | } |
| 1704: | |
| 1705: | return LateResolvableObjectShapeType::create($items); |
| 1706: | } |
| 1707: | |
| 1708: | private function resolveConstTypeNode(ConstTypeNode $typeNode, NameScope $nameScope): Type |
| 1709: | { |
| 1710: | $constExpr = $typeNode->constExpr; |
| 1711: | if ($constExpr instanceof ConstExprArrayNode) { |
| 1712: | throw new ShouldNotHappenException(); |
| 1713: | } |
| 1714: | |
| 1715: | if ( |
| 1716: | $constExpr instanceof ConstExprFalseNode |
| 1717: | || $constExpr instanceof ConstExprTrueNode |
| 1718: | || $constExpr instanceof ConstExprNullNode |
| 1719: | ) { |
| 1720: | throw new ShouldNotHappenException(); |
| 1721: | } |
| 1722: | |
| 1723: | if ($constExpr instanceof ConstFetchNode) { |
| 1724: | if ($constExpr->className === '') { |
| 1725: | throw new ShouldNotHappenException(); |
| 1726: | } |
| 1727: | |
| 1728: | $isStatic = false; |
| 1729: | if ($nameScope->getClassName() !== null) { |
| 1730: | switch (strtolower($constExpr->className)) { |
| 1731: | case 'static': |
| 1732: | $className = $nameScope->getClassName(); |
| 1733: | $isStatic = true; |
| 1734: | break; |
| 1735: | |
| 1736: | case 'self': |
| 1737: | $className = $nameScope->getClassName(); |
| 1738: | break; |
| 1739: | |
| 1740: | case 'parent': |
| 1741: | if ($this->getReflectionProvider()->hasClass($nameScope->getClassName())) { |
| 1742: | $classReflection = $this->getReflectionProvider()->getClass($nameScope->getClassName()); |
| 1743: | if ($classReflection->getParentClass() === null) { |
| 1744: | return new ErrorType(); |
| 1745: | |
| 1746: | } |
| 1747: | |
| 1748: | $className = $classReflection->getParentClass()->getName(); |
| 1749: | } |
| 1750: | break; |
| 1751: | } |
| 1752: | } |
| 1753: | |
| 1754: | if (!isset($className)) { |
| 1755: | $templateType = $nameScope->resolveTemplateTypeName($constExpr->className); |
| 1756: | if ($templateType instanceof TemplateType) { |
| 1757: | if (strtolower($constExpr->name) === 'class') { |
| 1758: | return new GenericClassStringType($templateType); |
| 1759: | } |
| 1760: | |
| 1761: | |
| 1762: | |
| 1763: | $boundClassReflections = $templateType->getBound()->getObjectClassReflections(); |
| 1764: | if (count($boundClassReflections) !== 1 || !$boundClassReflections[0]->isFinal()) { |
| 1765: | return new ClassConstantAccessType($templateType, $constExpr->name); |
| 1766: | } |
| 1767: | |
| 1768: | $className = $boundClassReflections[0]->getName(); |
| 1769: | } |
| 1770: | } |
| 1771: | |
| 1772: | if (!isset($className)) { |
| 1773: | $className = $nameScope->resolveStringName($constExpr->className); |
| 1774: | } |
| 1775: | |
| 1776: | if (!$this->getReflectionProvider()->hasClass($className)) { |
| 1777: | return new ErrorType(); |
| 1778: | } |
| 1779: | |
| 1780: | $classReflection = $this->getReflectionProvider()->getClass($className); |
| 1781: | |
| 1782: | if ($isStatic && $classReflection->isFinal()) { |
| 1783: | $isStatic = false; |
| 1784: | } |
| 1785: | |
| 1786: | $constantName = $constExpr->name; |
| 1787: | if (strtolower($constantName) === 'class') { |
| 1788: | if ($isStatic) { |
| 1789: | return new GenericClassStringType(new StaticType($classReflection)); |
| 1790: | } |
| 1791: | |
| 1792: | return new ConstantStringType($classReflection->getName(), true); |
| 1793: | } |
| 1794: | |
| 1795: | |
| 1796: | |
| 1797: | if ($isStatic) { |
| 1798: | return new ClassConstantAccessType(new StaticType($classReflection), $constantName); |
| 1799: | } |
| 1800: | |
| 1801: | if (Strings::contains($constantName, '*')) { |
| 1802: | |
| 1803: | $pattern = '{^' . str_replace('\\*', '.*?', preg_quote($constantName)) . '$}D'; |
| 1804: | $constantTypes = []; |
| 1805: | foreach ($classReflection->getNativeReflection()->getReflectionConstants() as $reflectionConstant) { |
| 1806: | $classConstantName = $reflectionConstant->getName(); |
| 1807: | if (Strings::match($classConstantName, $pattern) === null) { |
| 1808: | continue; |
| 1809: | } |
| 1810: | |
| 1811: | if ($classReflection->isEnum() && $classReflection->hasEnumCase($classConstantName)) { |
| 1812: | $constantTypes[] = new EnumCaseObjectType($classReflection->getName(), $classConstantName); |
| 1813: | continue; |
| 1814: | } |
| 1815: | |
| 1816: | $declaringClassName = $reflectionConstant->getDeclaringClass()->getName(); |
| 1817: | if (!$this->getReflectionProvider()->hasClass($declaringClassName)) { |
| 1818: | continue; |
| 1819: | } |
| 1820: | |
| 1821: | $constantTypes[] = $this->initializerExprTypeResolver->getType( |
| 1822: | $reflectionConstant->getValueExpression(), |
| 1823: | InitializerExprContext::fromClassReflection( |
| 1824: | $this->getReflectionProvider()->getClass($declaringClassName), |
| 1825: | ), |
| 1826: | ); |
| 1827: | } |
| 1828: | |
| 1829: | if (count($constantTypes) === 0) { |
| 1830: | return new ErrorType(); |
| 1831: | } |
| 1832: | |
| 1833: | return TypeCombinator::union(...$constantTypes); |
| 1834: | } |
| 1835: | |
| 1836: | if (!$classReflection->hasConstant($constantName)) { |
| 1837: | return new ErrorType(); |
| 1838: | } |
| 1839: | |
| 1840: | if ($classReflection->isEnum() && $classReflection->hasEnumCase($constantName)) { |
| 1841: | return new EnumCaseObjectType($classReflection->getName(), $constantName); |
| 1842: | } |
| 1843: | |
| 1844: | $reflectionConstant = $classReflection->getNativeReflection()->getReflectionConstant($constantName); |
| 1845: | if ($reflectionConstant === false) { |
| 1846: | return new ErrorType(); |
| 1847: | } |
| 1848: | $declaringClass = $reflectionConstant->getDeclaringClass(); |
| 1849: | |
| 1850: | return $this->initializerExprTypeResolver->getType($reflectionConstant->getValueExpression(), InitializerExprContext::fromClass($declaringClass->getName(), $declaringClass->getFileName() ?: null)); |
| 1851: | } |
| 1852: | |
| 1853: | if ($constExpr instanceof ConstExprFloatNode) { |
| 1854: | return new ConstantFloatType((float) $constExpr->value); |
| 1855: | } |
| 1856: | |
| 1857: | if ($constExpr instanceof ConstExprIntegerNode) { |
| 1858: | return new ConstantIntegerType((int) $constExpr->value); |
| 1859: | } |
| 1860: | |
| 1861: | if ($constExpr instanceof ConstExprStringNode) { |
| 1862: | return new ConstantStringType($constExpr->value); |
| 1863: | } |
| 1864: | |
| 1865: | return new ErrorType(); |
| 1866: | } |
| 1867: | |
| 1868: | private function resolveOffsetAccessNode(OffsetAccessTypeNode $typeNode, NameScope $nameScope): Type |
| 1869: | { |
| 1870: | $type = $this->resolveType($typeNode->type, $nameScope); |
| 1871: | $offset = $this->resolveType($typeNode->offset, $nameScope); |
| 1872: | |
| 1873: | if ($type->isOffsetAccessible()->no() || $type->hasOffsetValueType($offset)->no()) { |
| 1874: | return new ErrorType(); |
| 1875: | } |
| 1876: | |
| 1877: | return new OffsetAccessType($type, $offset); |
| 1878: | } |
| 1879: | |
| 1880: | private function resolveFloatRangeBound(Type $type, TypeNode $typeNode, string $keyword, float $keywordValue): ?float |
| 1881: | { |
| 1882: | if ($typeNode instanceof IdentifierTypeNode && $typeNode->name === $keyword) { |
| 1883: | return $keywordValue; |
| 1884: | } |
| 1885: | |
| 1886: | if ($type instanceof ConstantIntegerType) { |
| 1887: | return (float) $type->getValue(); |
| 1888: | } |
| 1889: | |
| 1890: | if ($type instanceof ConstantFloatType && !is_nan($type->getValue())) { |
| 1891: | return $type->getValue(); |
| 1892: | } |
| 1893: | |
| 1894: | return null; |
| 1895: | } |
| 1896: | |
| 1897: | private function expandIntMaskToType(Type $type): ?Type |
| 1898: | { |
| 1899: | $ints = array_map(static fn (ConstantIntegerType $type) => $type->getValue(), TypeUtils::getConstantIntegers($type)); |
| 1900: | if (count($ints) === 0) { |
| 1901: | return null; |
| 1902: | } |
| 1903: | |
| 1904: | $values = []; |
| 1905: | |
| 1906: | foreach ($ints as $int) { |
| 1907: | if ($int !== 0 && !array_key_exists($int, $values)) { |
| 1908: | foreach ($values as $value) { |
| 1909: | $computedValue = $value | $int; |
| 1910: | $values[$computedValue] = $computedValue; |
| 1911: | } |
| 1912: | } |
| 1913: | |
| 1914: | $values[$int] = $int; |
| 1915: | } |
| 1916: | |
| 1917: | $values[0] = 0; |
| 1918: | |
| 1919: | $min = min($values); |
| 1920: | $max = max($values); |
| 1921: | |
| 1922: | if ($max - $min === count($values) - 1) { |
| 1923: | return IntegerRangeType::fromInterval($min, $max); |
| 1924: | } |
| 1925: | |
| 1926: | if (count($values) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { |
| 1927: | return IntegerRangeType::fromInterval($min, $max); |
| 1928: | } |
| 1929: | |
| 1930: | return TypeCombinator::union(...array_map(static fn ($value) => new ConstantIntegerType($value), $values)); |
| 1931: | } |
| 1932: | |
| 1933: | |
| 1934: | |
| 1935: | |
| 1936: | |
| 1937: | |
| 1938: | public function resolveMultiple(array $typeNodes, NameScope $nameScope): array |
| 1939: | { |
| 1940: | $types = []; |
| 1941: | foreach ($typeNodes as $typeNode) { |
| 1942: | $types[] = $this->resolveType($typeNode, $nameScope); |
| 1943: | } |
| 1944: | |
| 1945: | return $types; |
| 1946: | } |
| 1947: | |
| 1948: | private function getReflectionProvider(): ReflectionProvider |
| 1949: | { |
| 1950: | return $this->reflectionProviderProvider->getReflectionProvider(); |
| 1951: | } |
| 1952: | |
| 1953: | private function getTypeAliasResolver(): TypeAliasResolver |
| 1954: | { |
| 1955: | return $this->typeAliasResolverProvider->getTypeAliasResolver(); |
| 1956: | } |
| 1957: | |
| 1958: | } |
| 1959: | |