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..84d8c80d9da 100644 --- a/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php +++ b/rules/Php85/Rector/FuncCall/ArrayKeyExistsNullToEmptyStringRector.php @@ -5,7 +5,9 @@ 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; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\FunctionReflection; @@ -74,6 +76,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 +93,7 @@ public function refactor(Node $node): ?Node $result = $this->nullToStrictStringConverter->convertIfNull( $node, $args, - 0, + $this->resolvePosition($args), $isTrait, $scope, $parametersAcceptor @@ -99,6 +105,20 @@ public function refactor(Node $node): ?Node return null; } + /** + * @param Arg[] $args + */ + private function resolvePosition(array $args): int + { + 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;