Skip to content

Commit

Permalink
add arrow function support
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 14, 2024
1 parent e0e4083 commit 1d598ea
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

final class IncludeArrowFunction
{
public function run(array $items)
{
return array_map(fn ($item): int => 1000, $items);
}
}

?>
-----
<?php

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

final class IncludeArrowFunction
{
/**
* @return int[]
*/
public function run(array $items)
{
return array_map(fn ($item): int => 1000, $items);
}
}

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

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

final class MultipleTypes
{
public function run(array $items)
{
if (mt_rand(0, 1)) {
return array_map(function ($item): int {
return $item;
}, $items);
}

return array_map(function ($item): string {
return '1000';
}, $items);
}
}

?>
-----
<?php

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

final class MultipleTypes
{
/**
* @return int[]|string[]
*/
public function run(array $items)
{
if (mt_rand(0, 1)) {
return array_map(function ($item): int {
return $item;
}, $items);
}

return array_map(function ($item): string {
return '1000';
}, $items);
}
}

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

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
Expand All @@ -31,7 +33,6 @@ public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ReturnAnalyzer $returnAnalyzer,
private readonly StaticTypeMapper $staticTypeMapper,
// private readonly ReturnPhpDocDecorator $returnPhpDocDecorator,
private readonly TypeFactory $typeFactory,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
Expand Down Expand Up @@ -108,7 +109,7 @@ public function refactor(Node $node): null|Function_|ClassMethod
}

$arrayMapClosure = $this->matchArrayMapClosure($returnScoped->expr);
if (! $arrayMapClosure instanceof Closure) {
if (! $arrayMapClosure instanceof FunctionLike) {
return null;
}

Expand Down Expand Up @@ -141,7 +142,7 @@ private function hasNonArrayReturnType(ClassMethod|Function_ $functionLike): boo
return $functionLike->returnType->toLowerString() !== 'array';
}

private function matchArrayMapClosure(FuncCall $funcCall): ?Closure
private function matchArrayMapClosure(FuncCall $funcCall): Closure|ArrowFunction|null
{
if (! $this->isName($funcCall, 'array_map')) {
return null;
Expand All @@ -153,7 +154,7 @@ private function matchArrayMapClosure(FuncCall $funcCall): ?Closure

// lets infer strict array_map() type
$firstArg = $funcCall->getArgs()[0];
if (! $firstArg->value instanceof Closure) {
if (! $firstArg->value instanceof Closure && ! $firstArg->value instanceof ArrowFunction) {
return null;
}

Expand Down

0 comments on commit 1d598ea

Please sign in to comment.