Fix PHP 8.1+ deprecations: null passed to string functions#2231
Open
mumnik wants to merge 1 commit intofuel:1.9/developfrom
Open
Fix PHP 8.1+ deprecations: null passed to string functions#2231mumnik wants to merge 1 commit intofuel:1.9/developfrom
mumnik wants to merge 1 commit intofuel:1.9/developfrom
Conversation
PHP 8.1 deprecated passing null to built-in string functions like strlen(), strpos(), strtolower(), preg_match(), explode(), etc. This will become a TypeError in PHP 9.0. Changes: - html.php: Cast $href and $email to (string) in anchor() and mail_to_safe() - input.php: Cast $index to (string) in server(), headers(), is_ajax() - str.php: Fix parenthesis bug in truncate() — strlen($string !== mb_strlen($string)) was comparing a boolean, now correctly: strlen($string) !== mb_strlen($string) Also cast $string in is_html() - security.php: Cast Input::method() return to (string) in CSRF check - format.php: Cast $string and $json to (string) in XML/JSON processing - validation.php: Cast $rules to (string) in add_field() - controller/rest.php: Cast HTTP_AUTHORIZATION header to (string) - request/driver.php: Cast $method and $mime to (string) Tested on PHP 8.4.8 with a production FuelPHP 1.9 application.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
PHP 8.1 deprecated passing
nullto built-in string functions (strlen(),strpos(),strtolower(),preg_match(),explode(), etc.). These deprecations becomeTypeErrorin PHP 9.0.This PR adds
(string)casts where nullable values can reach string functions across 8 core files:anchor()andmail_to_safe()can receive null$href/$emailserver(),headers(),is_ajax()pass potentially null$indextostrtoupper()/strtolower()truncate():strlen($string !== mb_strlen($string))had wrong parentheses, passing a boolean tostrlen()instead of comparing lengths. Also fixesis_html()Input::method()return cast in CSRF check$stringand$jsonparameters in XML/JSON processing$rulesparameter inadd_field()HTTP_AUTHORIZATIONheader value fromInput::server()$methodand$mimeparametersBug fix in
Str::truncate()This bug meant the multibyte offset correction in
truncate()never executed.Test plan
php -lsyntax check(string)cast on a string is a no-op