Skip to content

Commit

Permalink
[Scoped] Fixing downgrade part 2 (#1597)
Browse files Browse the repository at this point in the history
* [Scoped] Fixing downgrade part 2

* test
  • Loading branch information
samsonasik committed Dec 30, 2021
1 parent b25e351 commit 8648410
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeArrayFilterNullableCallbackRector\Fixture;

class SkipCallbackSecondArgWithClosure
{
public function run(array $data)
{
array_filter($data, function ($v, $k) { return ! empty ($v); }, ARRAY_FILTER_USE_BOTH);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\FuncCall;
Expand All @@ -17,7 +19,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PHPStan\Type\MixedType;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -103,8 +105,7 @@ public function refactor(Node $node): FuncCall|Ternary|null
return $node;
}

$type = $this->nodeTypeResolver->getType($args[1]->value);
if (! $type instanceof MixedType) {
if ($this->shouldSkipSecondArg($args[1]->value)) {
return null;
}

Expand All @@ -115,6 +116,11 @@ public function refactor(Node $node): FuncCall|Ternary|null
return $node;
}

private function shouldSkipSecondArg(Expr $expr): bool
{
return in_array($expr::class, [String_::class, Closure::class, ArrowFunction::class], true);
}

/**
* @param Arg[] $args
*/
Expand Down

0 comments on commit 8648410

Please sign in to comment.