Prepare for PHP 8.5 and PHP 9.0: NullToStrictStringFuncCallArgRector#5575
Prepare for PHP 8.5 and PHP 9.0: NullToStrictStringFuncCallArgRector#5575dcsjulio wants to merge 2 commits intoe107inc:masterfrom
Conversation
Rule: NullToStrictStringFuncCallArgRector (https://getrector.com/rule-detail/null-to-strict-string-func-call-arg-rector) In versions of PHP prior to 8.1, passing null to native functions that expected a string (such as strlen(), explode(), trim(), or str_replace()) would trigger an internal "coercion." PHP would silently convert the null value into an empty string (""), allowing the execution to continue without warnings. As of PHP 8.1, this behavior was deprecated, and in PHP 8.4 and 8.5, the engine has moved towards strict type enforcement. This Rector rule explicitly adds a (string) cast to arguments passed to these native functions whenever the variable could potentially be null.
|
Hi, I am working on preparing the codebase to fully support PHP 8.5 and future releases. To achieve this, I am cherry-picking specific Rector rules. The first one is NullToStrictStringFuncCallArgRector, which is critical for modern PHP versions. Rector analyzes whether variables might contain null values. In cases of type ambiguity (common with database-stored data or legacy code), it adds an explicit (string) cast when passing those variables to core functions. This prevents TypeErrors and silences deprecation warnings that are becoming stricter in PHP 8.5 and will be fatal in PHP 9.0. It may be a good idea to accept these automated corrections now to ensure stability. As the codebase evolves and we implement more strict type hinting across the project, we can review these casts on a case-by-case basis and remove those that are no longer necessary. Regarding the CI, the Acceptance tests failed with an SFTP deployment error. I am unsure if this is related to my changes or a pre-existing infrastructure issue. Could you please take a look? Thank you! |
Rule: NullToStrictStringFuncCallArgRector (https://getrector.com/rule-detail/null-to-strict-string-func-call-arg-rector)
In versions of PHP prior to 8.1, passing null to native functions that expected a string (such as strlen(), explode(), trim(), or str_replace()) would trigger an internal "coercion." PHP would silently convert the null value into an empty string (""), allowing the execution to continue without warnings.
As of PHP 8.1, this behavior was deprecated, and in PHP 8.4 and 8.5, the engine has moved towards strict type enforcement. This Rector rule explicitly adds a (string) cast to arguments passed to these native functions whenever the variable could potentially be null.
Motivation and Context
Being able to use e107 with php8.5 and future releases. Avoid potential flaws.
Description
Cast to string (null ==> '') for arguments where non null values are expected.
Used php rector for the changes, just the rule: NullToStrictStringFuncCallArgRector
Also: Some identation is changed because of the tool: empty lines will remove trailing spaces.
How Has This Been Tested?
Running the tests
Types of Changes
Checklist