Skip to content

Commit

Permalink
Make use of getArgs()
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 13, 2023
1 parent 3209c44 commit 10913e0
Show file tree
Hide file tree
Showing 21 changed files with 43 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function refactor(Node $node): ?FuncCall
return null;
}

$args = array_merge([new Arg($node->name)], $node->args);
$args = array_merge([new Arg($node->name)], $node->getArgs());

return new FuncCall(new Name('call_user_func'), $args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @changelog https://wiki.php.net/rfc/class_name_scalars
*
* @see Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\DowngradeClassConstantToStringRectorTest
* @see \Rector\Tests\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector\DowngradeClassConstantToStringRectorTest
*/
final class DowngradeClassConstantToStringRector extends AbstractRector
{
Expand Down
14 changes: 3 additions & 11 deletions rules/DowngradePhp55/Rector/FuncCall/DowngradeBoolvalRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,19 @@ public function refactor(Node $node): ?Node

private function refactorBoolval(FuncCall $funcCall): ?Bool_
{
if (! isset($funcCall->args[0])) {
if (! isset($funcCall->getArgs()[0])) {
return null;
}

if (! $funcCall->args[0] instanceof Arg) {
return null;
}

return new Bool_($funcCall->args[0]->value);
return new Bool_($funcCall->getArgs()[0]->value);
}

private function refactorAsCallback(FuncCall $funcCall): ?FuncCall
{
$functionLikeReflection = null;
$refactored = false;

foreach ($funcCall->args as $position => $arg) {
if (! $arg instanceof Arg) {
continue;
}

foreach ($funcCall->getArgs() as $position => $arg) {
if (! $this->isBoolvalReference($arg)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
namespace Rector\DowngradePhp70\Rector\Expression;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -23,11 +21,6 @@
*/
final class DowngradeDefineArrayConstantRector extends AbstractRector
{
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer
) {
}

/**
* @return array<class-string<Node>>
*/
Expand Down Expand Up @@ -72,13 +65,11 @@ public function refactor(Node $node): ?Node
}

$funcCall = $node->expr;

if ($this->shouldSkip($funcCall)) {
return null;
}

/** @var Arg[] $args */
$args = $funcCall->args;
$args = $funcCall->getArgs();

/** @var String_ $arg0 */
$arg0 = $args[0]->value;
Expand All @@ -96,13 +87,7 @@ private function shouldSkip(FuncCall $funcCall): bool
return true;
}

$args = $funcCall->args;

if (! $this->argsAnalyzer->isArgsInstanceInArgsPositions($args, [0, 1])) {
return true;
}

/** @var Arg[] $args */
$args = $funcCall->getArgs();
if (! $args[0]->value instanceof String_) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,7 @@ private function getLevelsArg(FuncCall $funcCall): ?Arg
return null;
}

if (! isset($funcCall->args[1])) {
return null;
}

if (! $funcCall->args[1] instanceof Arg) {
return null;
}

return $funcCall->args[1];
return $funcCall->getArgs()[1] ?? null;
}

private function getLevelsRealValue(Arg $levelsArg): ?int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,13 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->args[0])) {
return null;
}

if (! $node->args[0] instanceof Arg) {
if (! isset($node->getArgs()[0])) {
return null;
}

/** @var Array_ $options */
$options = $node->args[0]->value;
$options = $node->getArgs()[0]
->value;

foreach ($options->items as $option) {
if (! $option instanceof ArrayItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function refactor(Node $node): ?FuncCall
return null;
}

$args = array_merge([new Arg($node->name)], $node->args);
$args = array_merge([new Arg($node->name)], $node->getArgs());

return new FuncCall(new Name('call_user_func'), $args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function refactor(Node $node): ?FuncCall
}

$methodCall = $this->createBindToCall($node);
$args = [new Arg($methodCall), ...array_slice($node->args, 1)];
$args = [new Arg($methodCall), ...array_slice($node->getArgs(), 1)];

return new FuncCall(new Name('call_user_func'), $args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function refactor(Node $node): ?Node

$this->namespacedNameDecorator->decorate($class);

return new New_(new Name($className), $node->args);
return new New_(new Name($className), $node->getArgs());
}

private function createAnonymousClassName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\DowngradePhp71\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
Expand Down Expand Up @@ -68,18 +67,15 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->args[0])) {
if (! isset($node->getArgs()[0])) {
return null;
}

if (! $node->args[0] instanceof Arg) {
return null;
}
$expr = $node->getArgs()[0]
->value;

/** @var mixed $arg */
$arg = $node->args[0]->value;
$funcCall = $this->nodeFactory->createFuncCall('is_array', [$arg]);
$instanceof = new Instanceof_($arg, new FullyQualified('Traversable'));
$funcCall = $this->nodeFactory->createFuncCall('is_array', [$expr]);
$instanceof = new Instanceof_($expr, new FullyQualified('Traversable'));

return new BooleanOr($funcCall, $instanceof);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->args[0])) {
return null;
}

if (! $node->args[0] instanceof Arg) {
if (! isset($node->getArgs()[0])) {
return null;
}

$tempVariable = $this->namedVariableFactory->createVariable($node, 'callable');
$expression = new Expression(new Assign($tempVariable, $node->args[0]->value));
$expression = new Expression(new Assign($tempVariable, $node->getArgs()[0]->value));

$this->nodesToAddCollector->addNodeBeforeNode($expression, $node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node

$this->nodesToAddCollector->addNodeBeforeNode($assign, $node);

return new FuncCall($variable, $node->args);
return new FuncCall($variable, $node->getArgs());
}

private function createClosure(): Closure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\DowngradePhp73\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
Expand Down Expand Up @@ -56,16 +55,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->args[0])) {
if (! isset($node->getArgs()[0])) {
return null;
}

if (! $node->args[0] instanceof Arg) {
return null;
}

$isArrayFuncCall = $this->nodeFactory->createFuncCall('is_array', $node->args);
$instanceof = new Instanceof_($node->args[0]->value, new FullyQualified('Countable'));
$isArrayFuncCall = $this->nodeFactory->createFuncCall('is_array', $node->getArgs());
$instanceof = new Instanceof_($node->getArgs()[0]->value, new FullyQualified('Countable'));

return new BooleanOr($isArrayFuncCall, $instanceof);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
if ($node->getArgs() !== []) {
$lastArgumentPosition = array_key_last($node->args);
$lastArgumentPosition = array_key_last($node->getArgs());

$last = $node->args[$lastArgumentPosition];
$last = $node->getArgs()[$lastArgumentPosition];
if (! $this->followedByCommaAnalyzer->isFollowed($this->file, $last)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -48,11 +47,6 @@ final class SetCookieOptionsArrayToArgumentsRector extends AbstractRector

private int $highestIndex = 1;

public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -118,24 +112,20 @@ private function composeNewArgs(FuncCall $funcCall): array
{
$this->highestIndex = 1;

if (! $this->argsAnalyzer->isArgsInstanceInArgsPositions($funcCall->args, [0, 1, 2])) {
if (count($funcCall->getArgs()) < 3) {
return [];
}

/** @var Arg $firstArg */
$firstArg = $funcCall->args[0];

/** @var Arg $secondArg */
$secondArg = $funcCall->args[1];
$firstArg = $funcCall->getArgs()[0];
$secondArg = $funcCall->getArgs()[1];

$newArgs = [$firstArg, $secondArg];

/** @var Arg $thirdArg */
$thirdArg = $funcCall->args[2];
$thirdArg = $funcCall->getArgs()[2];

/** @var Array_ $optionsArray */
$optionsArray = $thirdArg->value;
/** @var ArrayItem|null $arrayItem */

foreach ($optionsArray->items as $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var Arg $secondArg */
$secondArg = $node->args[1];
$secondArg = $node->getArgs()[1];
$allowableTagsParam = $secondArg->value;

if ($allowableTagsParam instanceof Array_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @changelog https://www.php.net/manual/en/function.number-format.php#refsect1-function.number-format-changelog
*
* @see Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeNumberFormatNoFourthArgRector\DowngradeNumberFormatNoFourthArgRectorTest
* @see \Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeNumberFormatNoFourthArgRector\DowngradeNumberFormatNoFourthArgRectorTest
*/
final class DowngradeNumberFormatNoFourthArgRector extends AbstractRector
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,21 @@
namespace Rector\DowngradePhp80\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/str_contains
*
* @see Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeStrContainsRector\DowngradeStrContainsRectorTest
* @see \Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeStrContainsRector\DowngradeStrContainsRectorTest
*/
final class DowngradeStrContainsRector extends AbstractRector
{
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -77,17 +70,14 @@ public function refactor(Node $node): Identical | NotIdentical | null
return null;
}

if (! $this->argsAnalyzer->isArgsInstanceInArgsPositions($funcCall->args, [0, 1])) {
if (count($funcCall->getArgs()) < 2) {
return null;
}

/** @var Arg $firstArg */
$firstArg = $funcCall->args[0];
$haystack = $firstArg->value;

/** @var Arg $secondArg */
$secondArg = $funcCall->args[1];
$needle = $secondArg->value;
$haystack = $funcCall->getArgs()[0]
->value;
$needle = $funcCall->getArgs()[1]
->value;

$funcCall = $this->nodeFactory->createFuncCall('strpos', [$haystack, $needle]);

Expand Down
Loading

0 comments on commit 10913e0

Please sign in to comment.