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

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

use stdClass;

final class WithNamedArg
{
public function process(array $items): void
{
array_walk(callback: function (stdClass $item) {
echo $item->value;
}, array: $items);
}
}

?>
-----
<?php

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

use stdClass;

final class WithNamedArg
{
/**
* @param \stdClass[] $items
*/
public function process(array $items): void
{
array_walk(callback: function (stdClass $item) {
echo $item->value;
}, array: $items);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
Expand All @@ -22,7 +23,6 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\Enum\NativeFuncCallPositions;
Expand All @@ -36,7 +36,6 @@ final class AddParamArrayDocblockBasedOnCallableNativeFuncCallRector extends Abs
{
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly StaticTypeMapper $staticTypeMapper
) {
Expand Down Expand Up @@ -119,17 +118,18 @@ function (Node $subNode) use ($variableNamesWithArrayType, $node, &$paramsWithTy
}

$args = $subNode->getArgs();
if ($this->argsAnalyzer->hasNamedArg($args)) {
return null;
}

if (count($args) < 2) {
return null;
}

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

$arrayArgValue = $args[NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS[$funcCallName]['array']]->value;
$arrayArg = $subNode->getArg('array', NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS[$funcCallName]['array']);
if (! $arrayArg instanceof Arg) {
return null;
}

$arrayArgValue = $arrayArg->value;
if (! $arrayArgValue instanceof Variable) {
return null;
}
Expand All @@ -146,8 +146,12 @@ function (Node $subNode) use ($variableNamesWithArrayType, $node, &$paramsWithTy
return null;
}

$callbackArgValue = $args[NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS[$funcCallName]['callback']]->value;
$callbackArg = $subNode->getArg('callback', NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS[$funcCallName]['callback']);
if (! $callbackArg instanceof Arg) {
return null;
}

$callbackArgValue = $callbackArg->value;
if (! $callbackArgValue instanceof ArrowFunction && ! $callbackArgValue instanceof Closure) {
return null;
}
Expand Down
Loading