Skip to content

Commit

Permalink
[TypeDeclaration] Skip re-assign with call and use as arg on StrictAr…
Browse files Browse the repository at this point in the history
…rayParamDimFetchRector (#5434)

* [TypeDeclaration] Skip re-assign with call on StrictArrayParamDimFetchRector

* fix

* [ci-review] Rector Rectify

* fix phpstan

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Jan 5, 2024
1 parent d4f6b91 commit 3d71ae3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parameters:
- src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
- src/PhpParser/Printer/BetterStandardPrinter.php #41
- src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php
- rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php

-
message: '#Cognitive complexity for "(.*?)" is (.*?), keep it under 11#'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;

final class SkipReassignWithCall
{
public function addSuccessMessage($maybe_not_array)
{
$maybe_not_array = $this->convert($maybe_not_array);
echo $maybe_not_array['test'];
}

private function convert($maybe_not_array)
{
return ['test' => 'test'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp\Coalesce as AssignOpCoalesce;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Empty_;
Expand Down Expand Up @@ -215,7 +217,42 @@ private function shouldStop(Node $node, string $paramName): bool
return true;
}

return $this->isEmptyOrEchoedOrCasted($node, $paramName);
if ($this->isEmptyOrEchoedOrCasted($node, $paramName)) {
return true;
}

return $this->isReassignAndUseAsArg($node, $paramName);
}

private function isReassignAndUseAsArg(Node $node, string $paramName): bool
{
if (! $node instanceof Assign) {
return false;
}

if (! $node->var instanceof Variable) {
return false;
}

if (! $this->isName($node->var, $paramName)) {
return false;
}

if (! $node->expr instanceof CallLike) {
return false;
}

if ($node->expr->isFirstClassCallable()) {
return false;
}

foreach ($node->expr->getArgs() as $arg) {
if ($arg->value instanceof Variable && $this->isName($arg->value, $paramName)) {
return true;
}
}

return false;
}

private function isEmptyOrEchoedOrCasted(Node $node, string $paramName): bool
Expand All @@ -228,7 +265,7 @@ private function isEmptyOrEchoedOrCasted(Node $node, string $paramName): bool
return true;
}

return $node instanceof Array_ && $node->expr instanceof Variable && $node->expr->name === $paramName;
return $node instanceof Array_ && $node->expr instanceof Variable && $this->isName($node->expr, $paramName);
}

private function isMethodCallOrArrayDimFetch(string $paramName, ?Node $node): bool
Expand Down

0 comments on commit 3d71ae3

Please sign in to comment.