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,60 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject;
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;

final class UnionObjects
{
public function go()
{
$this->run($this->getUnion());
}

private function run(array $items)
{
}

/**
* @return SomeReturnedObject[]|AnotherReturnedObject[]
*/
private function getUnion(): array
{
return [];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject;
use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;

final class UnionObjects
{
public function go()
{
$this->run($this->getUnion());
}

/**
* @param \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject[]|\Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject[] $items
*/
private function run(array $items)
{
}

/**
* @return SomeReturnedObject[]|AnotherReturnedObject[]
*/
private function getUnion(): array
{
return [];
}
}

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

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source;

final class AnotherReturnedObject
{

}
23 changes: 22 additions & 1 deletion rules/Privatization/TypeManipulator/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ public function generalizeConstantTypes(Type $type): Type

if (count($uniqueGeneralizedUnionTypes) > 1) {
$generalizedUnionType = new UnionType($uniqueGeneralizedUnionTypes);

// avoid too huge print in docblock
$unionedDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$generalizedUnionType
);

// too long
if (strlen((string) $unionedDocType) > self::MAX_PRINTED_UNION_DOC_LENGHT) {
if (strlen(
(string) $unionedDocType
) > self::MAX_PRINTED_UNION_DOC_LENGHT && $this->avoidPrintedDocblockTrimming(
$generalizedUnionType
) === false) {
$alwaysKnownArrayType = $this->narrowToAlwaysKnownArrayType($generalizedUnionType);
if ($alwaysKnownArrayType instanceof ArrayType) {
return $alwaysKnownArrayType;
Expand Down Expand Up @@ -164,4 +169,20 @@ private function narrowToAlwaysKnownArrayType(UnionType $unionType): ?ArrayType
);
return new ArrayType($arrayUniqueKeyType, new MixedType());
}

/**
* Is object only? avoid trimming, as auto import handles it better
*/
private function avoidPrintedDocblockTrimming(UnionType $unionType): bool
{
if ($unionType->getConstantScalarTypes() !== []) {
return false;
}

if ($unionType->getConstantArrays() !== []) {
return false;
}

return $unionType->getObjectClassNames() !== [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\TypeDeclarationDocblocks\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -93,11 +95,7 @@ public function refactor(Node $node): ?Node
$classMethodParameterTypes = $this->callTypesResolver->resolveTypesFromCalls($methodCalls);

foreach ($classMethod->getParams() as $parameterPosition => $param) {
if ($param->type === null) {
continue;
}

if (! $this->isName($param->type, 'array')) {
if (! $this->hasParamArrayType($param)) {
continue;
}

Expand All @@ -118,6 +116,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($resolvedParameterType instanceof ArrayType && $resolvedParameterType->getItemType() instanceof MixedType && $resolvedParameterType->getKeyType() instanceof MixedType) {
continue;
}

// in case of array type declaration, null cannot be passed or is already casted
$resolvedParameterType = TypeCombinator::removeNull($resolvedParameterType);

Expand All @@ -140,4 +142,13 @@ public function refactor(Node $node): ?Node

return $node;
}

private function hasParamArrayType(Param $param): bool
{
if (! $param->type instanceof Node) {
return false;
}

return $this->isName($param->type, 'array');
}
}
Loading