Skip to content

Commit

Permalink
[DowngradePhp74][DowngradePhp81] Handle ArrowFunctionToAnonymousFunct…
Browse files Browse the repository at this point in the history
…ionRector+DowngradeArrayIsListRector
  • Loading branch information
samsonasik committed Nov 7, 2023
1 parent 4056166 commit 0b70a6a
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
namespace Rector\DowngradePhp74\Rector\ArrowFunction;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Analyser\Scope;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -114,6 +117,35 @@ public function refactor(Node $node): Closure
return new Throw_($throw->expr);
});

$this->appendUsesFromInsertedVariable($node->expr, $anonymousFunctionFactory);

return $anonymousFunctionFactory;
}

private function appendUsesFromInsertedVariable(Expr $expr, Closure $anonymousFunctionFactory): void
{
$this->traverseNodesWithCallable($expr, function (Node $subNode) use ($anonymousFunctionFactory) {
if (! $subNode instanceof Variable) {
return null;
}

$variableName = $this->getName($subNode);
if ($variableName === null) {
return null;
}

if ($subNode->hasAttribute(AttributeKey::ORIGINAL_NODE)) {
return null;
}

$scope = $subNode->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

if (! $scope->hasVariableType($variableName)->yes()) {
$anonymousFunctionFactory->uses[] = new ClosureUse(new Variable($variableName));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\DowngradeArrayIsListArrowFunction;

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

final class DowngradeArrayIsListArrowFunctionTest 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,51 @@
<?php

namespace Rector\Tests\Issues\DowngradeArrayIsListArrowFunction\Fixture;

class Fixture
{
public function run($items)
{
return array_map(
fn($item) => is_array($item) && !array_is_list($item)? (object) $this->convertToObject($item) : ($item),
$items
);
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\DowngradeArrayIsListArrowFunction\Fixture;

class Fixture
{
public function run($items)
{
$arrayIsListFunction = function (array $array) : bool {
if (function_exists('array_is_list')) {
return array_is_list($array);
}
if ($array === []) {
return true;
}
$current_key = 0;
foreach ($array as $key => $noop) {
if ($key !== $current_key) {
return false;
}
++$current_key;
}
return true;
};
return array_map(
function ($item) use ($arrayIsListFunction) {
return is_array($item) && !$arrayIsListFunction($item)? (object) $this->convertToObject($item) : ($item);
},
$items
);
}
}

?>
Loading

0 comments on commit 0b70a6a

Please sign in to comment.