|
3 | 3 | declare (strict_types=1); |
4 | 4 | namespace Rector\Privatization\TypeManipulator; |
5 | 5 |
|
| 6 | +use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
6 | 7 | use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
7 | 8 | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
8 | 9 | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
|
17 | 18 | use PHPStan\Type\IntegerType; |
18 | 19 | use PHPStan\Type\MixedType; |
19 | 20 | use PHPStan\Type\NeverType; |
| 21 | +use PHPStan\Type\ObjectType; |
20 | 22 | use PHPStan\Type\StringType; |
21 | 23 | use PHPStan\Type\Type; |
22 | 24 | use PHPStan\Type\TypeTraverser; |
23 | 25 | use PHPStan\Type\UnionType; |
24 | 26 | use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; |
25 | 27 | use Rector\NodeTypeResolver\PHPStan\TypeHasher; |
26 | 28 | use Rector\StaticTypeMapper\StaticTypeMapper; |
| 29 | +use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; |
27 | 30 | final class TypeNormalizer |
28 | 31 | { |
29 | 32 | /** |
@@ -121,10 +124,8 @@ public function generalizeConstantTypes(Type $type): Type |
121 | 124 | $uniqueGeneralizedUnionTypes = $this->typeFactory->uniquateTypes($generalizedUnionedTypes); |
122 | 125 | if (count($uniqueGeneralizedUnionTypes) > 1) { |
123 | 126 | $generalizedUnionType = new UnionType($uniqueGeneralizedUnionTypes); |
124 | | - // avoid too huge print in docblock |
125 | | - $unionedDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($generalizedUnionType); |
126 | | - // too long |
127 | | - if (strlen((string) $unionedDocType) > self::MAX_PRINTED_UNION_DOC_LENGTH && $this->avoidPrintedDocblockTrimming($generalizedUnionType) === \false) { |
| 127 | + $shortUnionedDocType = $this->resolveNameShortDocTypeNode($generalizedUnionType); |
| 128 | + if (strlen((string) $shortUnionedDocType) > self::MAX_PRINTED_UNION_DOC_LENGTH && $this->avoidPrintedDocblockTrimming($generalizedUnionType) === \false) { |
128 | 129 | $alwaysKnownArrayType = $this->narrowToAlwaysKnownArrayType($generalizedUnionType); |
129 | 130 | if ($alwaysKnownArrayType instanceof ArrayType) { |
130 | 131 | return $alwaysKnownArrayType; |
@@ -180,4 +181,17 @@ private function avoidPrintedDocblockTrimming(UnionType $unionType): bool |
180 | 181 | } |
181 | 182 | return $unionType->getObjectClassNames() !== []; |
182 | 183 | } |
| 184 | + private function resolveNameShortDocTypeNode(UnionType $unionType): TypeNode |
| 185 | + { |
| 186 | + // we have to converet name to short here, to make sure the not FQN, but short name is counted to the full length |
| 187 | + $objectShortGeneralizedUnionType = TypeTraverser::map($unionType, function (Type $type, callable $traverseCallback): Type { |
| 188 | + if ($type instanceof ObjectType && strpos($type->getClassName(), '\\') !== \false) { |
| 189 | + // after last "\\" |
| 190 | + $shortClassName = (string) substr($type->getClassName(), strrpos($type->getClassName(), '\\') + 1); |
| 191 | + return new ShortenedObjectType($shortClassName, $type->getClassName()); |
| 192 | + } |
| 193 | + return $traverseCallback($type, $traverseCallback); |
| 194 | + }); |
| 195 | + return $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($objectShortGeneralizedUnionType); |
| 196 | + } |
183 | 197 | } |
0 commit comments