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

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture;

final class ArrayFilterWithDocblockListType
{
/**
* @param list<int> $items
*/
public function run(array $items)
{
$result = array_filter($items, fn ($item) => $item * 2);
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture;

final class ArrayFilterWithDocblockListType
{
/**
* @param list<int> $items
*/
public function run(array $items)
{
$result = array_filter($items, fn (int $item) => $item * 2);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -26,7 +29,7 @@
final class AddArrayFunctionClosureParamTypeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper
private readonly StaticTypeMapper $staticTypeMapper,
) {
}

Expand Down Expand Up @@ -73,6 +76,8 @@ public function refactor(Node $node): ?Node
return null;
}

$hasChanged = false;

foreach (NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS as $functionName => $positions) {
if (! $this->isName($node, $functionName)) {
continue;
Expand All @@ -96,27 +101,29 @@ public function refactor(Node $node): ?Node
}

$passedExprType = $this->getType($node->getArgs()[$arrayPosition]->value);
if ($passedExprType instanceof ConstantArrayType || $passedExprType instanceof ArrayType) {
$singlePassedExprType = $passedExprType->getItemType();

if ($singlePassedExprType instanceof MixedType) {
continue;
}

$paramType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$singlePassedExprType,
TypeKind::PARAM
);

if (! $paramType instanceof Node) {
continue;
}
$singlePassedExprType = $this->resolveArrayItemType($passedExprType);
if (! $singlePassedExprType instanceof Type) {
continue;
}
if ($singlePassedExprType instanceof MixedType) {
continue;
}

$arrowFunctionParam->type = $paramType;
$paramType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$singlePassedExprType,
TypeKind::PARAM
);

return $node;
if (! $paramType instanceof Node) {
continue;
}

$hasChanged = true;
$arrowFunctionParam->type = $paramType;
}

if ($hasChanged === false) {
return null;
}

Expand All @@ -127,4 +134,31 @@ public function provideMinPhpVersion(): int
{
return PhpVersionFeature::SCALAR_TYPES;
}

private function resolveArrayItemType(Type $mainType): ?Type
{
if ($mainType instanceof ConstantArrayType || $mainType instanceof ArrayType) {
return $mainType->getItemType();
}

if ($mainType instanceof IntersectionType) {
foreach ($mainType->getTypes() as $subType) {
if ($subType instanceof AccessoryArrayListType) {
continue;
}

if (! $subType->isArray()->yes()) {
continue;
}

if (! $subType instanceof ArrayType) {
continue;
}

return $subType->getItemType();
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Rector\TypeDeclaration\Rector\FuncCall;

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
Expand Down
Loading