|
93 | 93 | use PHPStan\Type\TypeCombinator; |
94 | 94 | use PHPStan\Type\TypehintHelper; |
95 | 95 | use PHPStan\Type\TypeResult; |
96 | | -use PHPStan\Type\TypeTraverser; |
97 | 96 | use PHPStan\Type\TypeUtils; |
98 | 97 | use PHPStan\Type\TypeWithClassName; |
99 | 98 | use PHPStan\Type\UnionType; |
@@ -2451,54 +2450,7 @@ public function getClassConstFetchTypeByReflection(Name|Expr $class, string $con |
2451 | 2450 | } |
2452 | 2451 |
|
2453 | 2452 | if (strtolower($constantName) === 'class') { |
2454 | | - return TypeTraverser::map( |
2455 | | - $constantClassType, |
2456 | | - function (Type $type, callable $traverse): Type { |
2457 | | - if ($type instanceof UnionType || $type instanceof IntersectionType) { |
2458 | | - return $traverse($type); |
2459 | | - } |
2460 | | - |
2461 | | - if ($type instanceof NullType) { |
2462 | | - return $type; |
2463 | | - } |
2464 | | - |
2465 | | - if ($type instanceof EnumCaseObjectType) { |
2466 | | - return new IntersectionType([ |
2467 | | - new GenericClassStringType(new ObjectType($type->getClassName())), |
2468 | | - new AccessoryLiteralStringType(), |
2469 | | - ]); |
2470 | | - } |
2471 | | - |
2472 | | - $objectClassNames = $type->getObjectClassNames(); |
2473 | | - if (count($objectClassNames) > 1) { |
2474 | | - throw new ShouldNotHappenException(); |
2475 | | - } |
2476 | | - |
2477 | | - if ($type instanceof TemplateType && $objectClassNames === []) { |
2478 | | - return new IntersectionType([ |
2479 | | - new GenericClassStringType($type), |
2480 | | - new AccessoryLiteralStringType(), |
2481 | | - ]); |
2482 | | - } elseif ($objectClassNames !== [] && $this->getReflectionProvider()->hasClass($objectClassNames[0])) { |
2483 | | - $reflection = $this->getReflectionProvider()->getClass($objectClassNames[0]); |
2484 | | - if ($reflection->isFinalByKeyword()) { |
2485 | | - return new ConstantStringType($reflection->getName(), true); |
2486 | | - } |
2487 | | - |
2488 | | - return new IntersectionType([ |
2489 | | - new GenericClassStringType($type), |
2490 | | - new AccessoryLiteralStringType(), |
2491 | | - ]); |
2492 | | - } elseif ($type->isObject()->yes()) { |
2493 | | - return new IntersectionType([ |
2494 | | - new ClassStringType(), |
2495 | | - new AccessoryLiteralStringType(), |
2496 | | - ]); |
2497 | | - } |
2498 | | - |
2499 | | - return new ErrorType(); |
2500 | | - }, |
2501 | | - ); |
| 2453 | + return $constantClassType->toClassConstantType($this->getReflectionProvider()); |
2502 | 2454 | } |
2503 | 2455 |
|
2504 | 2456 | if ($constantClassType->isClassString()->yes()) { |
@@ -2699,33 +2651,7 @@ public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): Type |
2699 | 2651 |
|
2700 | 2652 | public function getBitwiseNotTypeFromType(Type $exprType): Type |
2701 | 2653 | { |
2702 | | - return TypeTraverser::map($exprType, static function (Type $type, callable $traverse): Type { |
2703 | | - if ($type instanceof UnionType || $type instanceof IntersectionType) { |
2704 | | - return $traverse($type); |
2705 | | - } |
2706 | | - if ($type instanceof ConstantStringType) { |
2707 | | - return new ConstantStringType(~$type->getValue()); |
2708 | | - } |
2709 | | - if ($type->isString()->yes()) { |
2710 | | - $accessories = []; |
2711 | | - if (!$type->isNonEmptyString()->yes()) { |
2712 | | - return new StringType(); |
2713 | | - } |
2714 | | - |
2715 | | - $accessories[] = new AccessoryNonEmptyStringType(); |
2716 | | - // it is not useful to apply numeric and literal strings here. |
2717 | | - // numeric string isn't certainly kept numeric: 3v4l.org/JERDB |
2718 | | - |
2719 | | - return new IntersectionType([new StringType(), ...$accessories]); |
2720 | | - } |
2721 | | - if ($type instanceof ConstantIntegerType || $type instanceof ConstantFloatType) { |
2722 | | - return new ConstantIntegerType(~ (int) $type->getValue()); |
2723 | | - } |
2724 | | - if ($type->isInteger()->yes() || $type->isFloat()->yes()) { |
2725 | | - return new IntegerType(); |
2726 | | - } |
2727 | | - return new ErrorType(); |
2728 | | - }); |
| 2654 | + return $exprType->toBitwiseNotType(); |
2729 | 2655 | } |
2730 | 2656 |
|
2731 | 2657 | private function resolveName(Name $name, ?ClassReflection $classReflection): string |
|
0 commit comments