Skip to content

Conversation

arshidkv12
Copy link
Contributor

@arshidkv12 arshidkv12 commented Aug 28, 2025

PHP 8.5+ deprecates passing null as the key to array_key_exists().
This Rector rule automatically replaces:

    array_key_exists(null, $array);

with:

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

https://wiki.php.net/rfc/deprecations_php_8_5

if (! $node instanceof FuncCall) {
return null;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add skip early $node->isFirstClassCallable() early here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

return null;
}

if ($this->isName($keyExpr, 'null')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use valueResolver->isNull instead, ensure exactly null variable, not variable named null

return $node;
}

return $node;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return null ?

CODE_SAMPLE
,
<<<'CODE_SAMPLE'
array_key_exists($key ?? '', $array);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer check type, if not string, then use (string) cast instead, see other rule

final class NullToStrictStringFuncCallArgRector extends AbstractRector implements MinPhpVersionInterface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the logic seems similar, I think you can create re-usable service to be used in both NullToStrictStringFuncCallArgRector and this rule so it share similar behaviour, just different php version and function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need Rector\Php85\Enum\NameNullToStrictNullFunctionMap, and should only array_key_exists be added?

Or without Rector\Php85\Enum\NameNullToStrictNullFunctionMap?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for just single function, no need to add to enum imo

* @param Arg[] $args
* @param int|string $position
*/
private function processNullToStrictStringOnNodePosition(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method looks duplicated with NullToStrictStringFuncCallArgRector, can be moved to shared service imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I should create a class like this, right?

final class NullToStrictStringConverter
{
    public function convertIfNull(Node $expr): Node
    {}
}

Where should I put it?”

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants