Skip to content

Commit

Permalink
[Cleanup] Make use of getArgs() directly (#3766)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 8, 2023
1 parent 103148c commit 13d81f9
Show file tree
Hide file tree
Showing 30 changed files with 107 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -101,23 +100,15 @@ public function refactor(Node $node): ?Node

private function createArrayKeyExists(FuncCall $inArrayFuncCall, FuncCall $arrayKeysFuncCall): ?FuncCall
{
if (! isset($inArrayFuncCall->args[0])) {
if (! isset($inArrayFuncCall->getArgs()[0])) {
return null;
}

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

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

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

$arguments = [$inArrayFuncCall->args[0], $arrayKeysFuncCall->args[0]];
$arguments = [$inArrayFuncCall->getArgs()[0], $arrayKeysFuncCall->getArgs()[0]];

return new FuncCall(new Name('array_key_exists'), $arguments);
}
Expand Down
9 changes: 2 additions & 7 deletions rules/CodeQuality/Rector/FuncCall/BoolvalToTypeCastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -64,14 +63,10 @@ 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;
}

return new Bool_($node->args[0]->value);
return new Bool_($node->getArgs()[0]->value);
}
}
14 changes: 6 additions & 8 deletions rules/CodeQuality/Rector/FuncCall/FloatvalToTypeCastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Cast\Double;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -77,16 +76,15 @@ public function refactor(Node $node): ?Node
return null;
}

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

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

$double = new Double($node->args[0]->value);
$double = new Double($firstArg->value);
$double->setAttribute(AttributeKey::KIND, Double::KIND_FLOAT);

return $double;
}
}
8 changes: 2 additions & 6 deletions rules/CodeQuality/Rector/FuncCall/IntvalToTypeCastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,10 @@ public function refactor(Node $node): ?Node
}
}

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

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

return new Int_($node->args[0]->value);
return new Int_($node->getArgs()[0]->value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ public function refactor(Node $node): ?Node
return null;
}

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

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

$firstArgumentStaticType = $this->getType($node->args[0]->value);
$firstArgumentStaticType = $this->getType($firstArg->value);
if (! $firstArgumentStaticType->isString()->yes()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -41,16 +40,14 @@ public function refactor(Node $node): ?Node
return null;
}

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

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

/** @var FuncCall $innerFuncCall */
$innerFuncCall = $node->args[0]->value;
$innerFuncCall = $firstArg->value;

if (! $this->isName($innerFuncCall, 'func_get_args')) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,11 @@ 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;
}

$node->args[1] = $innerFunCall->args[0];
$node->args[1] = $innerFunCall->getArgs()[0];

return $node;
}
Expand Down
14 changes: 5 additions & 9 deletions rules/CodeQuality/Rector/FuncCall/SimplifyStrposLowerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -42,26 +41,23 @@ 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;
}

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

/** @var FuncCall $innerFuncCall */
$innerFuncCall = $node->args[0]->value;
$innerFuncCall = $firstArg->value;
if (! $this->isName($innerFuncCall, 'strtolower')) {
return null;
}

// pop 1 level up
$node->args[0] = $innerFuncCall->args[0];
$node->args[0] = $innerFuncCall->getArgs()[0];
$node->name = new Name('stripos');

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,19 @@ 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;
}

$firstArrayItemValue = $firstArrayItem->value;

$firstArg = $node->getArgs()[0];

// strict
if (isset($node->args[2])) {
return new Identical($node->args[0]->value, $firstArrayItemValue);
if (isset($node->getArgs()[2])) {
return new Identical($firstArg->value, $firstArrayItemValue);
}

return new Equal($node->args[0]->value, $firstArrayItemValue);
return new Equal($firstArg->value, $firstArrayItemValue);
}
}
9 changes: 2 additions & 7 deletions rules/CodeQuality/Rector/FuncCall/StrvalToTypeCastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -64,14 +63,10 @@ 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;
}

return new String_($node->args[0]->value);
return new String_($node->getArgs()[0]->value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\Identical;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
Expand Down Expand Up @@ -79,15 +78,13 @@ public function refactor(Node $node): ?Node
/** @var FuncCall $secondExpr */
$secondExpr = $twoNodeMatch->getSecondExpr();

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

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

$varNode = $secondExpr->args[0]->value;
$varNode = $firstArg->value;

if ($firstExpr instanceof String_) {
$className = $this->valueResolver->getValue($firstExpr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodingStyle\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
Expand Down Expand Up @@ -71,15 +70,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;
}

$firstArgValue = $node->args[0]->value;
$firstArgValue = $node->getArgs()[0]
->value;
if (! $firstArgValue instanceof Array_) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodingStyle\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Greater;
Expand Down Expand Up @@ -70,16 +69,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 Expr $expr */
$expr = $node->args[0]->value;
$expr = $node->getArgs()[0]
->value;

// not pass array type, skip
if (! $this->isArray($expr)) {
Expand Down Expand Up @@ -110,20 +106,19 @@ private function processMarkTruthyNegation(BooleanNot $booleanNot): ?Identical
return null;
}

if (! $this->isName($booleanNot->expr, 'count')) {
return null;
}
$funcCall = $booleanNot->expr;

if (! isset($booleanNot->expr->args[0])) {
if (! $this->isName($funcCall, 'count')) {
return null;
}

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

/** @var Expr $expr */
$expr = $booleanNot->expr->args[0]->value;
$expr = $funcCall->getArgs()[0]
->value;

// not pass array type, skip
if (! $this->isArray($expr)) {
Expand Down
Loading

0 comments on commit 13d81f9

Please sign in to comment.