[PHP 8.4] Fixes for implicit nullability deprecation#865
[PHP 8.4] Fixes for implicit nullability deprecation#865schlessera merged 2 commits intoWordPress:developfrom Ayesh:php84/nullability
Conversation
|
Will this require to drop support for PHP 5.6 and 7.0? |
|
Ah, I thought this library also requires the same PHP version as WordPress. |
|
@Ayesh Thanks for the PR. @Ayesh @Art4 WordPress still has a minimum of PHP 7.0, this package of PHP 5.6, so this change can not go in. I'd already been looking at this one and have another patch prepared, but hadn't pulled it yet as I wanted to discuss it with @schlessera first. We will basically need to make a choice between:
I'm leaning towards option 3 (which is the patch I prepared locally). |
|
Thank you @jrfnl - you are absolutely right making the parameter type nullable was not going for to work on PHP < 7.1. I tried to recreate error handling, it may not be perfect, but it tries to mimic PHP 5.6 behavior as well. |
Fixes all issues that emit deprecation notices on PHP 8.4 for implicit nullable parameter type declarations. See: - [RFC](https://wiki.php.net/rfc/deprecate-implicitly-nullable-types) - [PHP 8.4: Implicitly nullable parameter declarations deprecated](https://php.watch/versions/8.4/implicitly-marking-parameter-type-nullable-deprecated) The "fix" for this deprecation is a bit unorthodox, in that the type delcaration is dropped. This makes it effectively a `mixed` type. Inside the function, it mimics the type check and throws an exception (PHP 7.0+) or triggers an error.
src/Cookie.php
Outdated
| // Mimic a \WpOrg\Requests\Iri|null type-check for $origin. | ||
| // Change to a type declaration when PHP >= 7.1 is the base requirement. | ||
| if ($origin !== null && !$origin instanceof \WpOrg\Requests\Iri) { | ||
| if (\PHP_VERSION_ID >= 70000) { | ||
| throw new \TypeError( | ||
| 'WpOrg\Requests\Cookie::parse_from_headers(): Argument #2 ($origin) must be of type WpOrg\Requests\Iri, ' . gettype($origin) . ' given' | ||
| ); | ||
| } | ||
| trigger_error( | ||
| 'Argument 2 passed to WpOrg\Requests\Cookie::parse_from_headers() must be an instance of WpOrg\Requests\Iri, ' . gettype($origin) . ' given', | ||
| E_USER_ERROR | ||
| ); | ||
| exit(); | ||
| } |
There was a problem hiding this comment.
I think this can be simplified by using the WpOrg\Requests\Exception\InvalidArgument exception, which we're also using everywhere else we're doing input validation in-function in Requests:
if (!empty($origin) && !($origin instanceof Iri)) {
throw InvalidArgument::create(2, '$origin', Iri::class, gettype($origin));
}Also take note of the parentheses around the instanceof. These are necessary as otherwise the precedence order will cause this code to be read as (!$origin) instanceof Iri, which is effectively true|false instanceof Iri, which would always be false.
The function will now also need to get a @throws tag.
As a last point, I wonder if this check should be moved to below the $cookie_headers check ? as that can function just fine without the $origin being validated.
There was a problem hiding this comment.
Oh and before I forget: this change will also need a test to cover it. Basically a test passing a non-object and one passing a non-Iri object to the method to make sure those get rejected correctly. The WpOrg\Requests\Tests\TypeProviderHelper can be used to set up the data provider for this test to make it comprehensive.
|
@Ayesh FYI: Alain and me have discussed this ticket and I've updated the PR to bring back the |
|
Looking great, thank you! |
Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
|
Thanks @Ayesh for working on this. The patch is included in today's 2.0.11 release. |
Pull Request Type
This is a:
Context
Fixes all issues that emit deprecation notices on PHP 8.4 for implicit nullable parameter type declarations.
See:
Quality assurance
Documentation
For new features:
examplesdirectory.docsdirectory.If the documentation is in a new markdown file, I have added a link to this new file to the Docs folder
README.mdfile.