Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of getArgs() #55

Merged
merged 2 commits into from
May 13, 2023
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
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\DowngradePhp70\Rector\Expression\DowngradeDefineArrayConstantRector\Fixture;

function skipFirstClassCallable()
{
$firstClassCallable = define(...);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will show error when enable zend.assertion in CI https://www.php.net/manual/en/ini.core.php#ini.zend.assertions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the example setting assertion setting in console:

# disable
php > ini_set('zend.assertions', 0);
php > assert(true === false);

# enable
php > ini_set('zend.assertions', 1);
php > assert(true === false);
PHP Warning:  Uncaught AssertionError: assert(true === false) in php shell code:1
Stack trace:
#0 php shell code(1): assert(false, 'assert(true ===...')
#1 {main}
  thrown in php shell code on line 1

Warning: Uncaught AssertionError: assert(true === false) in php shell code:1
Stack trace:
#0 php shell code(1): assert(false, 'assert(true ===...')
#1 {main}
  thrown in php shell code on line 1
``

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, isFirstCallable() already called on shouldSkip() method, so it works

if ($funcCall->isFirstClassCallable()) {
return true;
}

}
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 @@ -92,17 +83,15 @@ public function refactor(Node $node): ?Node

private function shouldSkip(FuncCall $funcCall): bool
{
if (! $this->isName($funcCall, 'define')) {
if ($funcCall->isFirstClassCallable()) {
return true;
}

$args = $funcCall->args;

if (! $this->argsAnalyzer->isArgsInstanceInArgsPositions($args, [0, 1])) {
if (! $this->isName($funcCall, 'define')) {
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 Expand Up @@ -106,14 +103,11 @@ private function shouldSkip(FuncCall $funcCall): bool
return true;
}

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

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

return ! $funcCall->args[0]->value instanceof Array_;
return ! $funcCall->getArgs()[0]
->value instanceof Array_;
}
}
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 @@ -4,7 +4,6 @@

namespace Rector\DowngradePhp72\NodeAnalyzer;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\If_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand Down Expand Up @@ -39,14 +38,10 @@ public function detect(FuncCall $funcCall, string $functionName): bool
/** @var FuncCall $functionExists */
$functionExists = $firstParentIf->cond;

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

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

return $this->valueResolver->isValue($functionExists->args[0]->value, $functionName);
return $this->valueResolver->isValue($functionExists->getArgs()[0]->value, $functionName);
}
}
5 changes: 3 additions & 2 deletions rules/DowngradePhp72/NodeManipulator/BitwiseFlagCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ private function assignThirdArgsValue(FuncCall $funcCall, BitwiseOr $bitwiseOr,
$bitwiseOr = $bitwiseOr->right;
}

if (! $funcCall->args[3] instanceof Arg) {
$fourthArg = $funcCall->getArgs()[3] ?? null;
if (! $fourthArg instanceof Arg) {
return;
}

$funcCall->args[3]->value = $bitwiseOr;
$fourthArg->value = $bitwiseOr;
}
}
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;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast\Array_;
Expand Down Expand Up @@ -88,15 +87,12 @@ public function refactor(Node $node): ?Node

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

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

$originalArray = $funcCall->args[0]->value;
$originalArray = $funcCall->getArgs()[0]
->value;
$array = $this->resolveCastedArray($originalArray);

if ($originalArray !== $array) {
Expand All @@ -117,15 +113,12 @@ private function refactorArrayKeyFirst(FuncCall $funcCall): ?FuncCall

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

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

$originalArray = $funcCall->args[0]->value;
$originalArray = $funcCall->getArgs()[0]
->value;
$array = $this->resolveCastedArray($originalArray);

if ($originalArray !== $array) {
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
Loading