From 8abecaa8fd771ad75052baf52df2b7747a416f56 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 28 Aug 2025 23:02:06 +0700 Subject: [PATCH 1/2] [Php84] Handle named key argument with flipped position on ArrayKeyExistsNullToEmptyStringRector --- .../Fixture/key_null.php.inc | 4 ++++ .../Fixture/key_null_named_flip.php.inc | 15 ++++++++++++++ .../Fixture/key_null_var.php.inc | 4 ++++ .../ArrayKeyExistsNullToEmptyStringRector.php | 20 ++++++++++++++++++- 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_named_flip.php.inc diff --git a/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null.php.inc b/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null.php.inc index 028c9c4e852..a67a072f816 100644 --- a/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null.php.inc +++ b/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null.php.inc @@ -1,11 +1,15 @@ ----- \ No newline at end of file diff --git a/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_named_flip.php.inc b/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_named_flip.php.inc new file mode 100644 index 00000000000..b8606d38af2 --- /dev/null +++ b/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_named_flip.php.inc @@ -0,0 +1,15 @@ + +----- + \ No newline at end of file diff --git a/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_var.php.inc b/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_var.php.inc index e692a56612f..dde06a0d90c 100644 --- a/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_var.php.inc +++ b/rules-tests/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector/Fixture/key_null_var.php.inc @@ -1,11 +1,15 @@ ----- \ No newline at end of file diff --git a/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php b/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php index 41db245f894..3686f83da96 100644 --- a/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php +++ b/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\FunctionReflection; @@ -74,6 +75,10 @@ public function refactor(Node $node): ?Node $args = $node->getArgs(); + if (count($args) !== 2) { + return null; + } + $classReflection = $scope->getClassReflection(); $isTrait = $classReflection instanceof ClassReflection && $classReflection->isTrait(); @@ -87,7 +92,7 @@ public function refactor(Node $node): ?Node $result = $this->nullToStrictStringConverter->convertIfNull( $node, $args, - 0, + $this->resolvePosition($node), $isTrait, $scope, $parametersAcceptor @@ -99,6 +104,19 @@ public function refactor(Node $node): ?Node return null; } + private function resolvePosition(FuncCall $funcCall): int + { + $args = $funcCall->getArgs(); + + foreach ($args as $position => $arg) { + if ($arg->name instanceof Identifier && $arg->name->toString() === 'key') { + return $position; + } + } + + return 0; + } + public function provideMinPhpVersion(): int { return PhpVersionFeature::DEPRECATE_NULL_ARG_IN_ARRAY_KEY_EXISTS_FUNCTION; From 640a781ef4ee547e826dc6827a7281001be9e54c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 28 Aug 2025 23:03:36 +0700 Subject: [PATCH 2/2] [Php84] Handle named key argument with flipped position on ArrayKeyExistsNullToEmptyStringRector --- .../FuncCall/ArrayKeyExistsNullToEmptyStringRector.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php b/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php index 3686f83da96..84d8c80d9da 100644 --- a/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php +++ b/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php @@ -5,6 +5,7 @@ namespace Rector\Php85\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Identifier; use PHPStan\Analyser\Scope; @@ -92,7 +93,7 @@ public function refactor(Node $node): ?Node $result = $this->nullToStrictStringConverter->convertIfNull( $node, $args, - $this->resolvePosition($node), + $this->resolvePosition($args), $isTrait, $scope, $parametersAcceptor @@ -104,10 +105,11 @@ public function refactor(Node $node): ?Node return null; } - private function resolvePosition(FuncCall $funcCall): int + /** + * @param Arg[] $args + */ + private function resolvePosition(array $args): int { - $args = $funcCall->getArgs(); - foreach ($args as $position => $arg) { if ($arg->name instanceof Identifier && $arg->name->toString() === 'key') { return $position;