From 8648410c86d2775821b15ee20c3665dc1e34a806 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 30 Dec 2021 22:54:54 +0700 Subject: [PATCH] [Scoped] Fixing downgrade part 2 (#1597) * [Scoped] Fixing downgrade part 2 * test --- .../skip_callback_second_arg_with_closure.php.inc | 13 +++++++++++++ .../DowngradeArrayFilterNullableCallbackRector.php | 12 +++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 rules-tests/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector/Fixture/skip_callback_second_arg_with_closure.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector/Fixture/skip_callback_second_arg_with_closure.php.inc b/rules-tests/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector/Fixture/skip_callback_second_arg_with_closure.php.inc new file mode 100644 index 00000000000..598a9f37268 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector/Fixture/skip_callback_second_arg_with_closure.php.inc @@ -0,0 +1,13 @@ + diff --git a/rules/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector.php b/rules/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector.php index 72b9217f079..7b9fb6b055f 100644 --- a/rules/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector.php +++ b/rules/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector.php @@ -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; @@ -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; @@ -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; } @@ -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 */