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
Expand Up @@ -32,9 +32,6 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNe

final class DeepNestedArray
{
/**
* @return non-empty-array<array{c: mixed}>[]
*/
public function test(): array {
$arr = [];
$rows = $this->getRows();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class UnionReservationArrayShapesNoDoc
{
public function getReservations($items, $withInvoice = false)
{
$data = [];
foreach ($items as $item) {
if ($withInvoice) {
$data[] = [
'currency' => 'EUR',
'creation_date' => 'd',
'guarantee' => 1,
'invoice_recipient_id' => 5,
'invoice_free_text' => 't',
'status' => 'ok',
'type' => 'a',
];
} else {
$data[] = [
'currency' => 'EUR',
'creation_date' => 'd',
'guarantee' => 1,
'status' => 'ok',
'type' => 'a',
];
}
}

return $data;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture;

final class UnionReservationArrayShapesNoDoc
{
public function getReservations($items, $withInvoice = false): array
{
$data = [];
foreach ($items as $item) {
if ($withInvoice) {
$data[] = [
'currency' => 'EUR',
'creation_date' => 'd',
'guarantee' => 1,
'invoice_recipient_id' => 5,
'invoice_free_text' => 't',
'status' => 'ok',
'type' => 'a',
];
} else {
$data[] = [
'currency' => 'EUR',
'creation_date' => 'd',
'guarantee' => 1,
'status' => 'ok',
'type' => 'a',
];
}
}

return $data;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\NodeNameResolver\NodeNameResolver;
Expand Down Expand Up @@ -204,55 +202,22 @@ private function matchArrayAssignedVariable(array $stmts): array

private function shouldAddReturnArrayDocType(Type $arrayType): bool
{
// a union of multiple distinct array shapes produces a noisy doc type, skip it
if ($this->hasNoisyArrayShapeUnion($arrayType)) {
return false;
}

if ($arrayType instanceof ConstantArrayType) {
if ($arrayType->getIterableValueType() instanceof NeverType) {
return false;
}

// handle only simple arrays
if (! $arrayType->getIterableKeyType()->isInteger()->yes()) {
return false;
}
}

return true;
// only plain list<> and generic array<> doc types are worth adding;
// array shapes produce noisy, fragile doc types, skip them
return ! $this->hasArrayShape($arrayType);
}

private function hasNoisyArrayShapeUnion(Type $type): bool
private function hasArrayShape(Type $type): bool
{
$isNoisy = false;
TypeTraverser::map($type, function (Type $currentType, callable $traverse) use (&$isNoisy): Type {
if ($currentType instanceof UnionType && $this->hasMultipleArrayVariants($currentType)) {
$isNoisy = true;
$hasArrayShape = false;
TypeTraverser::map($type, function (Type $currentType, callable $traverse) use (&$hasArrayShape): Type {
if ($currentType instanceof ConstantArrayType && $currentType->getKeyTypes() !== [] && ! $currentType->isList()->yes()) {
$hasArrayShape = true;
}

return $traverse($currentType);
});

return $isNoisy;
}

private function hasMultipleArrayVariants(UnionType $unionType): bool
{
$arrayVariantCount = 0;
foreach ($unionType->getTypes() as $type) {
if (! $type->isArray()->yes()) {
continue;
}

// an empty array [] collapses into the sibling variant, ignore it
if ($type->getIterableValueType() instanceof NeverType) {
continue;
}

++$arrayVariantCount;
}

return $arrayVariantCount >= 2;
return $hasArrayShape;
}
}
Loading