Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;

final class CoverObjectReturn
{
private ?\Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Sourde\SomeObjectInDirectArray $item = null;

public function toArray(): array
{
return [
'key1' => 100,
'key2' => $this->item
];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;

final class CoverObjectReturn
{
private ?\Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Sourde\SomeObjectInDirectArray $item = null;

/**
* @return array<string, \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Sourde\SomeObjectInDirectArray|int|null>
*/
public function toArray(): array
{
return [
'key1' => 100,
'key2' => $this->item
];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Sourde;

final class SomeObjectInDirectArray
{
}
31 changes: 25 additions & 6 deletions rules/Privatization/TypeManipulator/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Privatization\TypeManipulator;

use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
Expand All @@ -18,13 +19,15 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\PHPStan\TypeHasher;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;

final readonly class TypeNormalizer
{
Expand Down Expand Up @@ -138,14 +141,10 @@
if (count($uniqueGeneralizedUnionTypes) > 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) {
Expand Down Expand Up @@ -221,4 +220,24 @@

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

Check warning on line 226 in rules/Privatization/TypeManipulator/TypeNormalizer.php

View workflow job for this annotation

GitHub Actions / Check for typos

"converet" should be "convert".
$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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading