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

declare(strict_types=1);

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

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddArrayFunctionClosureParamTypeRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
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 ArrayFilterWithDocblockType
{
/**
* @param 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 ArrayFilterWithDocblockType
{
/**
* @param 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
@@ -0,0 +1,33 @@
<?php

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

final class IncludeArrayMap
{
/**
* @param int[] $items
*/
public function run(array $items)
{
$result = array_map(fn ($item) => $item * 2, $items);
}
}

?>
-----
<?php

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

final class IncludeArrayMap
{
/**
* @param int[] $items
*/
public function run(array $items)
{
$result = array_map(fn (int $item) => $item * 2, $items);
}
}

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

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

final class SkipUnclearParam
{
/**
* @param mixed[] $items
*/
public function run(array $items)
{
$result = array_filter($items, fn ($item) => $item * 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

final class SomeFunction
{
public function run()
{
$array = [1, 2, 3];
$result = array_filter($array, fn ($item) => $item * 2);
}
}

?>
-----
<?php

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

final class SomeFunction
{
public function run()
{
$array = [1, 2, 3];
$result = array_filter($array, fn (int $item) => $item * 2);
}
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector;

return RectorConfig::configure()
->withRules([AddArrayFunctionClosureParamTypeRector::class]);
30 changes: 30 additions & 0 deletions rules/TypeDeclaration/Enum/NativeFuncCallPositions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\Enum;

final class NativeFuncCallPositions
{
/**
* @var array<string, array<string, int>>
*/
public const ARRAY_AND_CALLBACK_POSITIONS = [
'array_walk' => [
'array' => 0,
'callback' => 1,
],
'array_map' => [
'array' => 1,
'callback' => 0,
],
'usort' => [
'array' => 0,
'callback' => 1,
],
'array_filter' => [
'array' => 0,
'callback' => 1,
],
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\Enum\NativeFuncCallPositions;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -33,28 +34,6 @@
*/
final class AddParamArrayDocblockBasedOnCallableNativeFuncCallRector extends AbstractRector
{
/**
* @var array<string, array<string, int>>
*/
private const NATIVE_FUNC_CALLS_WITH_POSITION = [
'array_walk' => [
'array' => 0,
'callback' => 1,
],
'array_map' => [
'array' => 1,
'callback' => 0,
],
'usort' => [
'array' => 0,
'callback' => 1,
],
'array_filter' => [
'array' => 0,
'callback' => 1,
],
];

public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ArgsAnalyzer $argsAnalyzer,
Expand All @@ -70,8 +49,8 @@ public function getRuleDefinition(): RuleDefinition
<<<'CODE_SAMPLE'
function process(array $items): void
{
array_walk($items, function (stdClass $item) {
echo $item->value;
array_walk($items, function (stdClass $item) {
echo $item->value;
});
}
CODE_SAMPLE
Expand All @@ -82,8 +61,8 @@ function process(array $items): void
*/
function process(array $items): void
{
array_walk($items, function (stdClass $item) {
echo $item->value;
array_walk($items, function (stdClass $item) {
echo $item->value;
});
}
CODE_SAMPLE
Expand Down Expand Up @@ -131,7 +110,7 @@ function (Node $subNode) use ($variableNamesWithArrayType, $node, &$paramsWithTy
return null;
}

if (! $this->isNames($subNode, array_keys(self::NATIVE_FUNC_CALLS_WITH_POSITION))) {
if (! $this->isNames($subNode, array_keys(NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS))) {
return null;
}

Expand All @@ -150,7 +129,7 @@ function (Node $subNode) use ($variableNamesWithArrayType, $node, &$paramsWithTy

$funcCallName = (string) $this->getName($subNode);

$arrayArgValue = $args[self::NATIVE_FUNC_CALLS_WITH_POSITION[$funcCallName]['array']]->value;
$arrayArgValue = $args[NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS[$funcCallName]['array']]->value;
if (! $arrayArgValue instanceof Variable) {
return null;
}
Expand All @@ -167,7 +146,7 @@ function (Node $subNode) use ($variableNamesWithArrayType, $node, &$paramsWithTy
return null;
}

$callbackArgValue = $args[self::NATIVE_FUNC_CALLS_WITH_POSITION[$funcCallName]['callback']]->value;
$callbackArgValue = $args[NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS[$funcCallName]['callback']]->value;

if (! $callbackArgValue instanceof ArrowFunction && ! $callbackArgValue instanceof Closure) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\Enum\NativeFuncCallPositions;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\AddArrayFunctionClosureParamTypeRectorTest
*/
final class AddArrayFunctionClosureParamTypeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add array_filter()/array_map() function closure param type, based on passed iterable',
[
new CodeSample(
<<<'CODE_SAMPLE'
$items = [1, 2, 3];
$result = array_filter($items, fn ($item) => $item > 1);
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
$items = [1, 2, 3];
$result = array_filter($items, fn (int $item) => $item > 1
CODE_SAMPLE
),

]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
foreach (NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS as $functionName => $positions) {
if (! $this->isName($node, $functionName)) {
continue;
}

if ($node->isFirstClassCallable()) {
continue;
}

$arrayPosition = $positions['array'];
$callbackPosition = $positions['callback'];

$firstArgExpr = $node->getArgs()[$callbackPosition]
->value;
if (! $firstArgExpr instanceof ArrowFunction && ! $firstArgExpr instanceof Closure) {
continue;
}

$arrowFunction = $firstArgExpr;
$arrowFunctionParam = $arrowFunction->getParams()[0];

// param is known already
if ($arrowFunctionParam->type instanceof Node) {
continue;
}

$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;
}

$arrowFunctionParam->type = $paramType;

return $node;
}

return null;
}

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::SCALAR_TYPES;
}
}
Loading
Loading