diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/covert-object-return.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/covert-object-return.php.inc new file mode 100644 index 00000000000..d632b292705 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/covert-object-return.php.inc @@ -0,0 +1,40 @@ + 100, + 'key2' => $this->item + ]; + } +} + +?> +----- + + */ + public function toArray(): array + { + return [ + 'key1' => 100, + 'key2' => $this->item + ]; + } +} + +?> diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Sourde/SomeObjectInDirectArray.php b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Sourde/SomeObjectInDirectArray.php new file mode 100644 index 00000000000..3ae7c85169c --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Sourde/SomeObjectInDirectArray.php @@ -0,0 +1,9 @@ + 1) { $generalizedUnionType = new UnionType($uniqueGeneralizedUnionTypes); - // avoid too huge print in docblock - $unionedDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode( - $generalizedUnionType - ); + $shortUnionedDocType = $this->resolveNameShortDocTypeNode($generalizedUnionType); - // too long if (strlen( - (string) $unionedDocType + (string) $shortUnionedDocType ) > self::MAX_PRINTED_UNION_DOC_LENGTH && $this->avoidPrintedDocblockTrimming( $generalizedUnionType ) === false) { @@ -221,4 +220,24 @@ private function avoidPrintedDocblockTrimming(UnionType $unionType): bool return $unionType->getObjectClassNames() !== []; } + + private function resolveNameShortDocTypeNode(UnionType $unionType): TypeNode + { + // we have to converet name to short here, to make sure the not FQN, but short name is counted to the full length + $objectShortGeneralizedUnionType = TypeTraverser::map($unionType, function ( + Type $type, + callable $traverseCallback + ): Type { + if ($type instanceof ObjectType && str_contains($type->getClassName(), '\\')) { + // after last "\\" + $shortClassName = substr($type->getClassName(), strrpos($type->getClassName(), '\\') + 1); + + return new ShortenedObjectType($shortClassName, $type->getClassName()); + } + + return $traverseCallback($type, $traverseCallback); + }); + + return $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($objectShortGeneralizedUnionType); + } } diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php index 450d21d3e66..978acccb496 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php @@ -113,8 +113,10 @@ public function refactor(Node $node): ?Node return null; } - if ($returnedType->getReferencedClasses() !== []) { - // better handled by shared-interface/class rule, to avoid turning objects to mixed + // better handled by shared-interface/class rule, to avoid turning objects to mixed + if ($returnedType->getReferencedClasses() !== [] && count($returnedType->getReferencedClasses()) === count( + $returnedType->getValueTypes() + )) { return null; }