-
Notifications
You must be signed in to change notification settings - Fork 95
Make implementations of built-in interfaces PHP 8.1 friendly #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
i really like this pr to add support for PHP 8.1 although it's still a RC.
|
Hi @marcelthole, I intentionally left out the {
"minimum-stability": "dev",
"prefer-stable": true
} In my (not exhausting) test, running the Composer with the above config installs the dev version of Symfony packages. So that's why I instead just documented how to install it on 8.1. If this constrain is OK with you, I am happy to push a commit for that. |
Since the support of PHP is at least 7.1. It means return types already exists. why not directly add those instead of the attribute ? |
@Neirda24 for the So yeah, some points here could use the correct return type but not all of them. |
Indeed. @marcelthole . But that's already a good leap forward. Keep in ,ind that those warnings only come from native PHP objects (like |
As now PHP 8.1 released, I removed all unnecessary dependency manipulations.
It turned out PHPUnit 9.5.x is compatible with PHP 8.1. No need for change.
On older PHP, the built-in classes do not define return types. So if a user-land code defines it, it will fail on 7.x - 8.0. If not, on 8.1+. The Edited to add: Because Liskov, we can define return types, the failure comes from parameter types. |
Hi @cebe, I ran GH Actions against this pull in our fork. It looks like newer PHP versions are failing with older Symfony: https://github.com/OM4/php-openapi/runs/4672017289?check_suite_focus=true I address those errors in #134 |
Reason: To maintain PHP >=7.1.0 and make implementation PHP 8.1 compatible
@cebe Hi - please can you let me know if there is anything I can do to help get the required changes for PHP 8.1 compatibility merged? This is the remaining blocker for my application's upgrade. I'm happy to pitch in if necessary :) |
Hi, another volunteer here :) If i can help, I will. |
@cebe I sent you an email with a sponsorship offer to get this moving. |
I've opened a PR against this PR that will fix the build failure: https://github.com/OM4/php-openapi/pull/3 |
Thanks @shadowhand , I merged that fix in #162. |
Problem
Unfortunately, PHP 8.1 throws Fatal Error if the built-in interface implementation is incompatible with the new, type enhanced interfaces.
Example error:
Example differences:
Solution
As the above error messages also refer to it, the solution is relatively straightforward. The implementation needs to be updated with type information, or the
#[ReturnTypeWillChange]
attribute must be added to the affected methods.This pull implements a the later, to maintain the current PHP compatibility:
PHP >= 7.1.0
.Fortunately, attributes syntax just comments on PHP 7.x, the added lines may look weird but ignored by PHP.
Note: Attributes are "references" to classes, so namespace rules apply. That's why the attributes incorporate the backslash as well, i.e.:
#[\ReturnTypeWillChange]
.Test
To successfully run PHPUnit tests under PHP 8.1, I used the following commands:
I tested these changes on 7.1.33, 7.2.34, 7.3.30, 7.4.23, 8.0.10 and 8.1.0beta3
References
Affected built-in interfaces:
ReturnTypeWillChange
attribute introduced in this rfc: https://wiki.php.net/rfc/internal_method_return_typesAttributes in general: https://www.php.net/manual/en/language.attributes.php