Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Carbon\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -88,7 +89,11 @@ public function refactor(Node $node): ?Node
return null;
}

$firstArg = $new->getArgs()[0];
$firstArg = $new->getArg('datetime', 0);
if (! $firstArg instanceof Arg) {
return null;
}

if (! $firstArg->value instanceof String_) {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Carbon\Rector\New_;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
Expand Down Expand Up @@ -80,9 +81,9 @@ private function refactorWithClass(New_ $new, string $className): MethodCall|Sta
}

if (count($new->getArgs()) === 1) {
$firstArg = $new->getArgs()[0];
$firstArg = $new->getArg('datetime', 0);

if ($firstArg->value instanceof String_) {
if ($firstArg instanceof Arg && $firstArg->value instanceof String_) {
return $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\BooleanAnd;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\Identical;
Expand Down Expand Up @@ -57,8 +58,12 @@ public function refactor(Node $node): ?Node
/** @var FuncCall $isArrayExpr */
$isArrayExpr = $twoNodeMatch->getFirstExpr();

$firstArgValue = $isArrayExpr->getArgs()[0]
->value;
$firstArg = $isArrayExpr->getArg('value', 0);
if (! $firstArg instanceof Arg) {
return null;
}

$firstArgValue = $firstArg->value;

/** @var Empty_ $emptyOrNotIdenticalNode */
$emptyOrNotIdenticalNode = $twoNodeMatch->getSecondExpr();
Expand Down Expand Up @@ -96,12 +101,12 @@ function (Node $node): bool {
return false;
}

if (isset($node->getArgs()[0])) {
return $node->getArgs()[0]
->value instanceof Variable;
$firstArg = $node->getArg('value', 0);
if (! $firstArg instanceof Arg) {
return false;
}

return false;
return $firstArg->value instanceof Variable;
},
// empty(...)
function (Node $node): bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\Concat;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\MagicConst\Dir;
Expand Down Expand Up @@ -74,7 +75,10 @@ public function refactor(Node $node): ?Concat
return null;
}

$firstArg = $dirnameFuncCall->getArgs()[0];
$firstArg = $dirnameFuncCall->getArg('path', 0);
if (! $firstArg instanceof Arg) {
return null;
}
if (! $firstArg->value instanceof Dir) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -86,7 +87,10 @@ public function refactor(Node $node): ?Node
return null;
}

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

$firstValue = $firstArg->value;
$firstValueStaticType = $this->getType($firstValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ public function refactor(Node $node): ?Node
return null;
}

if (isset($node->getArgs()[2])) {
if ($node->getArg('allow_string', 2) instanceof Arg) {
return null;
}

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

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

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -70,7 +71,10 @@ public function refactor(Node $node): ?Node
return null;
}

$firstArg = $node->getArgs()[0];
$firstArg = $node->getArg('format', 0);
if (! $firstArg instanceof Arg) {
return null;
}
$maskArgument = $firstArg->value;
if (! $maskArgument instanceof String_) {
return null;
Expand All @@ -80,7 +84,10 @@ public function refactor(Node $node): ?Node
return null;
}

$secondArg = $node->getArgs()[1];
$secondArg = $node->getArg('values', 1);
if (! $secondArg instanceof Arg) {
return null;
}
$valueArgument = $secondArg->value;

$valueType = $this->getType($valueArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -44,7 +45,10 @@ public function refactor(Node $node): ?Node
return null;
}

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

if (! $firstArg->value instanceof FuncCall) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->getArgs()[0])) {
$arrayArg = $innerFunCall->getArg('array', 0);
if (! $arrayArg instanceof Arg) {
return null;
}

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

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector\SingleInArrayToCompareRectorTest
Expand Down Expand Up @@ -127,7 +128,7 @@ private function resolveArrayItem(FuncCall $funcCall): ?ArrayItem
return null;
}

if (! isset($funcCall->getArgs()[0])) {
if (! $funcCall->getArg('needle', 0) instanceof Arg) {
return null;
}

Expand All @@ -138,10 +139,11 @@ private function processCompare(ArrayItem $firstArrayItem, FuncCall $funcCall, b
{
$firstArrayItemValue = $firstArrayItem->value;

$firstArg = $funcCall->getArgs()[0];
$firstArg = $funcCall->getArg('needle', 0);
Assert::isInstanceOf($firstArg, Arg::class);

// strict
if (isset($funcCall->getArgs()[2])) {
if ($funcCall->getArg('strict', 2) instanceof Arg) {
return $isNegated
? new NotIdentical($firstArg->value, $firstArrayItemValue)
: new Identical($firstArg->value, $firstArrayItemValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -56,8 +57,9 @@ public function refactor(Node $node): ?Node
return null;
}

$firstArg = $node->getArgs()[0];
if ($firstArg->unpack) {
// unpacked argument is not resolved by getArg()
$firstArg = $node->getArg('format', 0);
if (! $firstArg instanceof Arg) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\Identical;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\Identical;
Expand Down Expand Up @@ -99,8 +100,12 @@ public function refactor(Node $node): ?Node
return null;
}

$variable = $funcCall->getArgs()[0]
->value;
$firstArg = $funcCall->getArg('string', 0);
if (! $firstArg instanceof Arg) {
return null;
}

$variable = $firstArg->value;

// Needs string cast if variable type is not string
// see https://github.com/rectorphp/rector/issues/6700
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function refactor(Node $node): ?Node
}

$function = $node->else;
$secondArg = $function->getArgs()[1] ?? null;
$secondArg = $function->getArg('array', 1);

if (! $secondArg instanceof Arg) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,18 @@ public function refactor(Node $node): ?Node
return null;
}

$firstArgValue = $node->getArgs()[0]
->value;
$secondArgValue = $node->getArgs()[1]
->value;
$firstArg = $node->getArg('callback', 0);
if (! $firstArg instanceof Arg) {
return null;
}

$secondArg = $node->getArg('args', 1);
if (! $secondArg instanceof Arg) {
return null;
}

$firstArgValue = $firstArg->value;
$secondArgValue = $secondArg->value;

if ($firstArgValue instanceof String_) {
$functionName = $this->valueResolver->getValue($firstArgValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -72,12 +73,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->getArgs()[0])) {
$firstArg = $node->getArg('callback', 0);
if (! $firstArg instanceof Arg) {
return null;
}

$firstArgValue = $node->getArgs()[0]
->value;
$firstArgValue = $firstArg->value;
if (! $firstArgValue instanceof Array_) {
return null;
}
Expand Down
17 changes: 14 additions & 3 deletions rules/CodingStyle/Rector/FuncCall/ConsistentImplodeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ public function refactor(Node $node): ?Node
}

if (count($node->getArgs()) === 1) {
$arrayArg = $node->getArg('array', 0);
if (! $arrayArg instanceof Arg) {
return null;
}

// complete default value ''
$node->args[1] = $node->getArgs()[0];
$node->args[1] = $arrayArg;
$node->args[0] = new Arg(new String_(''));

return $node;
Expand All @@ -88,15 +93,21 @@ public function refactor(Node $node): ?Node
return null;
}

$firstArg = $node->getArgs()[0];
$firstArg = $node->getArg('separator', 0);
if (! $firstArg instanceof Arg) {
return null;
}
$firstArgumentValue = $firstArg->value;

$firstArgumentType = $this->getType($firstArgumentValue);
if ($firstArgumentType->isString()->yes()) {
return null;
}

$secondArg = $node->getArgs()[1];
$secondArg = $node->getArg('array', 1);
if (! $secondArg instanceof Arg) {
return null;
}

if ($this->stringTypeAnalyzer->isStringOrUnionStringOnlyType($secondArg->value)) {
$node->args = array_reverse($node->getArgs());
Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/NodeAnalyzer/PropertyWriteonlyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function hasClassDynamicPropertyNames(Class_ $class): bool
$node,
'get_object_vars'
) && ! $node->isFirstClassCallable()) {
$firstArg = $node->getArgs()[0] ?? null;
$firstArg = $node->getArg('object', 0);
if ($firstArg instanceof Arg && $firstArg->value instanceof Variable && $firstArg->value->name === 'this') {
return true;
}
Expand Down
Loading
Loading