Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

namespace Rector\Tests\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector\Fixture;

array_key_exists(null, $array);

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector\Fixture;

array_key_exists('', $array);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector\Fixture;

array_key_exists(array: $array, key: null);

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector\Fixture;

array_key_exists(array: $array, key: '');

?>
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

namespace Rector\Tests\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector\Fixture;

array_key_exists($k, $array);

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector\Fixture;

array_key_exists((string) $k, $array);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -87,7 +93,7 @@ public function refactor(Node $node): ?Node
$result = $this->nullToStrictStringConverter->convertIfNull(
$node,
$args,
0,
$this->resolvePosition($args),
$isTrait,
$scope,
$parametersAcceptor
Expand All @@ -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;
Expand Down
Loading