Skip to content

Commit

Permalink
[CodingStyle] Add variadic method call unpack to CallUserFuncCallToVa…
Browse files Browse the repository at this point in the history
…riadicRector
  • Loading branch information
TomasVotruba committed May 3, 2021
1 parent d689800 commit dcd4726
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector\Fixture;

use Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector\Source\Redirector;

final class ArrayLocalMethod
{
private $redirector;

public function __construct()
{
$this->redirector = new Redirector();
}

public function run()
{
$args = \func_get_args();
call_user_func_array([$this->redirector, 'redirect'], $args);
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector\Fixture;

use Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector\Source\Redirector;

final class ArrayLocalMethod
{
private $redirector;

public function __construct()
{
$this->redirector = new Redirector();
}

public function run()
{
$args = \func_get_args();
$this->redirector->redirect(...$args);
}
}

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

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector\Source;

final class Redirector
{
public function redirect($first, $second)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\DowngradePhp70\Rector\Coalesce\DowngradeStrictTypeDeclarationRector\Fixture;

class PropertyFetch
final class ComparePropertyFetch
{
private $a;
private $b;
Expand All @@ -19,7 +19,7 @@ class PropertyFetch

namespace Rector\Tests\DowngradePhp70\Rector\Coalesce\DowngradeStrictTypeDeclarationRector\Fixture;

class PropertyFetch
final class ComparePropertyFetch
{
private $a;
private $b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;

class MethodCall
final class DispatcherCall
{
/** @var \League\Event\EventDispatcher */
private $dispatcher;
Expand All @@ -24,7 +24,7 @@ class MethodCall

namespace Rector\Tests\LeagueEvent\Rector\MethodCall\DispatchStringToObjectRector\Fixture;

class MethodCall
final class DispatcherCall
{
/** @var \League\Event\EventDispatcher */
private $dispatcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://www.php.net/manual/en/function.call-user-func-array.php#117655
* @changelog https://3v4l.org/UMP6b
*
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector\CallUserFuncCallToVariadicRectorTest
*/
Expand Down Expand Up @@ -62,13 +69,50 @@ public function refactor(Node $node): ?Node
return null;
}

$functionName = $this->valueResolver->getValue($node->args[0]->value);
if (! is_string($functionName)) {
return null;
$firstArgValue = $node->args[0]->value;
$secondArgValue = $node->args[1]->value;

if ($firstArgValue instanceof String_) {
$functionName = $this->valueResolver->getValue($firstArgValue);
return $this->createFuncCall($secondArgValue, $functionName);
}

// method call
if ($firstArgValue instanceof Array_) {
if (count($firstArgValue->items) !== 2) {
return null;
}

$firstItem = $firstArgValue->items[0];
$secondItem = $firstArgValue->items[1];

if (! $firstItem instanceof ArrayItem) {
return null;
}

if (! $secondItem instanceof ArrayItem) {
return null;
}

if ($firstItem->value instanceof PropertyFetch) {
if (! $secondItem->value instanceof String_) {
return null;
}

$string = $secondItem->value;
$methodName = $string->value;

return new MethodCall($firstItem->value, $methodName, [new Arg($secondArgValue, false, true)]);
}
}

return null;
}

private function createFuncCall(Expr $expr, string $functionName): FuncCall
{
$args = [];
$args[] = new Arg($node->args[1]->value, false, true);
$args[] = new Arg($expr, false, true);

return $this->nodeFactory->createFuncCall($functionName, $args);
}
Expand Down

0 comments on commit dcd4726

Please sign in to comment.