Skip to content

Commit

Permalink
[cleanup] Make use of getArgs(), remove ignores from phpstan.neon (#3770
Browse files Browse the repository at this point in the history
)

* make use of getArgs()

* solve later
  • Loading branch information
TomasVotruba committed May 8, 2023
1 parent f6c4edc commit 5a32ed9
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 46 deletions.
8 changes: 2 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,13 @@ parameters:
paths:
- rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php

# waits for phpstan upgrade to php-parser 4.13
- '#Access to an undefined property PhpParser\\Node\\Arg\|PhpParser\\Node\\VariadicPlaceholder\:\:\$value#'
- '#(.*?), array<PhpParser\\Node\\Arg\|PhpParser\\Node\\VariadicPlaceholder\> given#'

# share configuration to avoid duplication in 5 rules
-
message: '#Instead of abstract class, use specific service with composition#'
path: rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php

# internal tag
- '#Unknown PHPDoc tag\: @phpstan\-rule#'

- '#Access to an undefined property PhpParser\\Node\\Arg\|PhpParser\\Node\\VariadicPlaceholder\:\:\$value#'

# many internal cases
- '#Calling (.*?) is not covered by backward compatibility promise\. The method might change in a minor PHPStan version#'
Expand Down
4 changes: 2 additions & 2 deletions rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ private function processArgs(
return null;
}

$argValue = $this->valueResolver->getValue($expr->args[$position]->value);
$argValue = $this->valueResolver->getValue($expr->getArgs()[$position]->value);

if (is_scalar(
$replaceArgumentDefaultValue->getValueBefore()
) && $argValue === $replaceArgumentDefaultValue->getValueBefore()) {
$expr->args[$position] = $this->normalizeValueToArgument($replaceArgumentDefaultValue->getValueAfter());
} elseif (is_array($replaceArgumentDefaultValue->getValueBefore())) {
$newArgs = $this->processArrayReplacement($expr->args, $replaceArgumentDefaultValue);
$newArgs = $this->processArrayReplacement($expr->getArgs(), $replaceArgumentDefaultValue);

if (is_array($newArgs)) {
$expr->args = $newArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\BooleanAnd;

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

/** @var Expr $firstArgValue */
$firstArgValue = $isArrayExpr->args[0]->value;
$firstArgValue = $isArrayExpr->getArgs()[0]
->value;

/** @var Empty_ $emptyOrNotIdenticalNode */
$emptyOrNotIdenticalNode = $twoNodeMatch->getSecondExpr();

if ($emptyOrNotIdenticalNode->expr instanceof FuncCall && $this->nodeComparator->areNodesEqual(
$emptyOrNotIdenticalNode->expr->args[0]->value,
$emptyOrNotIdenticalNode->expr->getArgs()[0]
->value,
$firstArgValue
)) {
return new Identical($emptyOrNotIdenticalNode->expr, new Array_());
Expand Down
3 changes: 2 additions & 1 deletion rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ private function resolveNewConditionNode(Expr $expr, bool $isNegated): ?BinaryOp

private function resolveCount(bool $isNegated, FuncCall $funcCall): Identical | Greater | null
{
$countedType = $this->getType($funcCall->args[0]->value);
$countedType = $this->getType($funcCall->getArgs()[0]->value);

if ($countedType->isArray()->yes()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
/**
* @changelog https://github.com/symplify/phpstan-rules/blob/main/docs/rules_overview.md#explicitmethodcallovermagicgetsetrule
*
* @inspired by \Rector\Transform\Rector\Assign\GetAndSetToMethodCallRector
* @phpstan-rule https://github.com/symplify/phpstan-rules/blob/main/src/Rules/Explicit/ExplicitMethodCallOverMagicGetSetRule.php
*
* @see \Rector\Tests\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector\ExplicitMethodCallOverMagicGetSetRectorTest
*/
final class ExplicitMethodCallOverMagicGetSetRector extends AbstractScopeAwareRector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ public function refactor(Node $node): ?Node
return null;
}

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

$node->name = new Name('mysqli_connect');
$node->args[0]->value = $this->joinStringWithNode('p:', $node->getArgs()[0]->value);
$node->args[0]->value = $this->joinStringWithNode('p:', $firstArg->value);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function refactor(Node $node): ?Node
continue;
}

$args = $node->args;
$args = $node->getArgs();
if (
$args === [] || ! $this->isProbablyMysql($args[0]->value)
) {
Expand Down
2 changes: 1 addition & 1 deletion rules/Php70/Rector/FuncCall/MultiDirnameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function matchNestedDirnameFuncCall(FuncCall $funcCall): ?FuncCall
return null;
}

$args = $funcCall->args;
$args = $funcCall->getArgs();
if (count($args) >= 3) {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions rules/Php71/Rector/FuncCall/CountOnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ private function shouldSkip(FuncCall $funcCall): bool
return true;
}

if (! $funcCall->args[0]->value instanceof Variable) {
$firstArg = $funcCall->getArgs()[0];
if (! $firstArg->value instanceof Variable) {
return false;
}

Expand All @@ -196,7 +197,7 @@ private function shouldSkip(FuncCall $funcCall): bool
}
}

return $this->variableAnalyzer->isStaticOrGlobal($funcCall->args[0]->value);
return $this->variableAnalyzer->isStaticOrGlobal($firstArg->value);
}

private function castToArray(Expr $countedExpr, FuncCall $funcCall): FuncCall
Expand Down
3 changes: 2 additions & 1 deletion rules/Php72/Rector/While_/WhileEachToForeachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public function refactor(Node $node): ?Node
return null;
}

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

$foreachedExpr = count($listNode->items) === 1 ? $this->nodeFactory->createFuncCall(
'array_keys',
[$firstArg]
Expand Down
7 changes: 6 additions & 1 deletion rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ function (Node $subNode) use ($funcCall): bool {
return true;
}

return $this->nodeComparator->areNodesEqual($subNode->args[0]->value, $funcCall->args[0]->value);
return $this->nodeComparator->areNodesEqual(
$subNode->getArgs()[0]
->value,
$funcCall->getArgs()[0]
->value
);
}
);

Expand Down
3 changes: 2 additions & 1 deletion rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ private function isFirstValueStringOrArray(FuncCall $funcCall): bool
return false;
}

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

$value = $this->valueResolver->getValue($firstArg->value);
if (is_string($value)) {
return true;
Expand Down
12 changes: 4 additions & 8 deletions rules/Php73/Rector/FuncCall/SetCookieRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand Down Expand Up @@ -116,21 +115,18 @@ private function shouldSkip(FuncCall $funcCall): bool
}

/**
* @return Arg[]|VariadicPlaceholder[]
* @return Arg[]
*/
private function composeNewArgs(FuncCall $funcCall): array
{
$items = [];

$args = $funcCall->args;
$args = $funcCall->getArgs();

$newArgs = [];
$newArgs[] = $args[0];
$newArgs[] = $args[1];
$newArgs = [$args[0], $args[1]];

unset($args[0]);
unset($args[1]);

$items = [];
foreach ($args as $idx => $arg) {
$newKey = new String_(self::KNOWN_OPTIONS[$idx]);
$items[] = new ArrayItem($arg->value, $newKey);
Expand Down
15 changes: 9 additions & 6 deletions rules/Php80/NodeManipulator/ResourceReturnToObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ private function processFuncCall(FuncCall $funcCall, array $collectionFunctionTo
return null;
}

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

$argResourceValue = $firstArg->value;

return new Instanceof_($argResourceValue, new FullyQualified($objectInstanceCheck));
}

Expand All @@ -74,8 +76,8 @@ private function processFuncCall(FuncCall $funcCall, array $collectionFunctionTo
*/
private function resolveArgValueType(FuncCall $funcCall, array $collectionFunctionToReturnObject): ?Type
{
/** @var Expr $argResourceValue */
$argResourceValue = $funcCall->args[0]->value;
$argResourceValue = $funcCall->getArgs()[0]
->value;
$argValueType = $this->nodeTypeResolver->getType($argResourceValue);

// if detected type is not FullyQualifiedObjectType, it still can be a resource to object, when:
Expand Down Expand Up @@ -194,8 +196,9 @@ private function processBooleanOr(BooleanOr $booleanOr, array $collectionFunctio
return null;
}

/** @var Expr $argResourceValue */
$argResourceValue = $funCall->args[0]->value;
$argResourceValue = $funCall->getArgs()[0]
->value;

/** @var Instanceof_ $instanceof */
if (! $this->isInstanceOfObjectCheck($instanceof, $argResourceValue, $objectInstanceCheck)) {
return null;
Expand Down
8 changes: 5 additions & 3 deletions rules/Php80/Rector/NotIdentical/StrContainsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ public function refactor(Node $node): ?Node
return null;
}

if (isset($funcCall->args[2])) {
if ($this->isName($funcCall->name, 'strpos') && $this->isPositiveInteger($funcCall->args[2]->value)) {
if (isset($funcCall->getArgs()[2])) {
$secondArg = $funcCall->getArgs()[2];

if ($this->isName($funcCall->name, 'strpos') && $this->isPositiveInteger($secondArg->value)) {
$funcCall->args[0] = new Arg($this->nodeFactory->createFuncCall(
'substr',
[$funcCall->args[0], $funcCall->args[2]]
[$funcCall->args[0], $secondArg]
));
}

Expand Down
7 changes: 5 additions & 2 deletions rules/Php80/Rector/Ternary/GetDebugTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ private function areValuesIdentical(Ternary $ternary): bool
{
/** @var FuncCall $isObjectFuncCall */
$isObjectFuncCall = $ternary->cond;
$firstExpr = $isObjectFuncCall->args[0]->value;

$firstExpr = $isObjectFuncCall->getArgs()[0]
->value;

/** @var FuncCall|ClassConstFetch $getClassFuncCallOrClassConstFetchClass */
$getClassFuncCallOrClassConstFetchClass = $ternary->if;
Expand All @@ -152,7 +154,8 @@ private function areValuesIdentical(Ternary $ternary): bool
}

$secondExpr = $getClassFuncCallOrClassConstFetchClass instanceof FuncCall
? $getClassFuncCallOrClassConstFetchClass->args[0]->value
? $getClassFuncCallOrClassConstFetchClass->getArgs()[0]
->value
: $getClassFuncCallOrClassConstFetchClass->class;

/** @var FuncCall $gettypeFuncCall */
Expand Down
5 changes: 3 additions & 2 deletions rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ public function refactor(Node $node): ?New_
return null;
}

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

$flags = $node->args[1]->value;
$flags = $node->getArgs()[1]
->value;
if ($this->isSkipDotsPresent($flags)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ private function refactorResetFuncCall(Ternary $ternary, FuncCall $resetFuncCall
{
$ternary->if = $ternary->cond;

$firstArgValue = $resetFuncCall->args[0]->value;
if ($resetFuncCall->isFirstClassCallable()) {
return;
}

$firstArgValue = $resetFuncCall->getArgs()[0]
->value;
$firstArgType = $scope->getType($firstArgValue);

$falsyCompareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare(
Expand Down

0 comments on commit 5a32ed9

Please sign in to comment.