Problem/Motivation
PHP 8.4 released after Drupal 11.1, meaning we won't be able to use new features until we drop support for PHP 8.3. Fortunately Symfony provides polyfills for many of these functions.
Steps to reproduce
https://github.com/symfony/polyfill-php84/blob/1.x/README.md
Proposed resolution
As symfony/polyfill-php84 is added. Modernize some areas of core to use array_find(), array_all(), array_any() and other related functions. \Drupal\Component\Assertion\Inspector is a good place to start because it has many related functions and has good unit test coverage
Remaining tasks
- review areas where to replace
- build a list of functions to improve
- patch/commit
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3458945-nr-bot.txt | 90 bytes | needs-review-queue-bot |
Issue fork drupal-3458945
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 11.x
compare
- 3458945-polyfill-php84
changes, plain diff MR !8655
Comments
Comment #3
mstrelan commentedNeeds work for the remaining functions in the Inspector class.
Comment #4
mstrelan commentedThis will need a change record for the new dependency, but I'm not creating one right now until this is reviewed.
Comment #5
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #6
mstrelan commentedComment #7
smustgrave commentedSo not sure the process to evaluate this but looking at https://github.com/symfony/polyfill-php84
These are functions it claims to introduce
I don't see any open issues
Last release was June 19th, 2024 so still seems to be active
I personally don't see any downside,
Is there a way to identify other places this could be used? Looking at the file in the MR
core/lib/Drupal/Component/Assertion/Inspector.phpdefinitely seems cleanerI'm a +1
Comment #8
mstrelan commentedIf it helps for evaluation we already have a number of symfony polyfills in 11.0.x, while 10.3.x has symfony/polyfill-php83 and 9.5.x has symfony/polyfill-php80.
I didn't find an existing rector rule for this, the best I could come up with is so grep for
foreachorforloops that have areturnstatement. If they are returning TRUE or FALSE then they might be candidates forarray_allorarray_any. If they return something else then possibly could be a candidate forarray_find. I don't think it's worth trying to include too much in the initial scope.Comment #9
smustgrave commentedNo sure who can make the call though.
Comment #10
xjmAdding a backend dependency requires release and framework manager signoff.
Since this is a symfony package and we've used their polyfills before, it does not require a full dependency evaluation.
I'm personally in favor of the addition.
Comment #11
catchYeah makes sense to me too - we can drop the polyfill once we require PHP 8.4, but we get the benefit of the new functions faster.
Comment #12
mstrelan commentedAdded a change record, assuming it's too late for 11.0.
Comment #13
smustgrave commentedThink this can be RTBC? @mstrelan mind rebasing and I don't mind marking
Comment #14
mstrelan commentedRebased / reran the composer commands
Comment #15
smustgrave commentedThanks. Sorry if I’m jumping the gun but have only gotten +1s for this
Comment #16
xjmI think @catch and I together counts as RM signoff, so untagging.
In terms of which branch we target, 11.1 makes sense because that will likely be the first branch to support PHP 8.4. It's also a potential maintenance minor target (i.e., 10.4 backport) since it has to do with enabling PHP compatibility.
Normally, adding a dependency is not allowed in a patch release. I think we do not need to concern ourselves with backporting it to 10.3/11.0 unless those branches also end up being PHP-8.4-compatible (i.e. there are no disruptive fixes required to get to PHP 8.4 compatibility). We could discuss it at that point if that turns out to be possible.
Comment #17
larowlan+1 from me as FM to the concept
But needs work because the composer.json in lib/Drupal/Component/Assertion also needs to declare this dependency
Comment #18
mstrelan commentedAdded the dependency as per #17, but unsure if I've done it correctly as the composer.json says it's partially generated automatically and I couldn't understand the link it points to.
I guess we'd have to take care to add the same dependency to any other component that uses these functions.
Comment #19
smustgrave commentedNot 100% sure why if a core committer would know?
Comment #20
xjmI broke this when I committed the pre-11.0.0 dep updates, so it will need to be re-generated.
Regarding the "why do we need to add the dependency" question: The IS of the issue the above change record was for (#3272110: Drupal 9 and 10's Drupal\Component composer.json files are totally out of date) explains the problem a bit better. Basically, before that issue, every component's
composer.jsonwas maintained by hand and so we got things like them being left as version 8.8 when the core version was 9.4, declaring requirements of very old PHP versions that did not match core syntax anymore, etc. So the issue added ComponentGenerator, which does things like make the core versions match and reconcile the PHP requirement.However,
ComponentGeneratoris not psychic; it can't tell that we're using PHP 8.4 syntax in this component now when core's own PHP requirement (and therefore all the components' PHP requirements) are 8.3. So to use the PHP 8.4 syntax, the component also needs to declare a direct dependency on the polyfill, in case it's installed by itself without core. Hope that helps. :)I do agree that the CR could maybe use some work to be more specific about what
ComponentGeneratordoes (and doesn't do). Also, it would actually be better for the generated message to link permanent documentation (rather than a change record). Let's file a followup to address that.Comment #21
xjmLooking at it, there's also a second problem that
ComponentGeneratorhardcodes PHP 7.3.0, rather than using Drupal's own minimum requirement. So that's a second followup issue needed. (It still would not address the issue of needing the dependency in the component, though.)Comment #22
mstrelan commentedOpened follow ups #3465352: Update package metadata readme to a docs page instead of a change record and #3465353: ComponentGenerator hardcodes PHP 7.3.0
Rebased MR. Setting back to RTBC.
Comment #23
mstrelan commentedThe validatable config job failed but that's because it's broken. Opened #3465354: Validatable Config job is broken when MR changes composer.json/composer.lock file.
Comment #24
mstrelan commentedI've just realised these array functions won't work on other traversables, only arrays, and that is a regression for the Inspector class.
Comment #25
andypostComment #26
mstrelan commentedIn light of #24 I think we should revert the changes to the Inspector class and simply add the polyfill without any usage. Alternatively we could find somewhere else to use it.
Comment #27
andypostFixed CI as config validation job does not calling composer with new lock file after reverting commit
so now job has
Comment #28
andypostComment #29
smustgrave commentedChanges still look good. Can the changes to the gitlab file though be reverted as seems out of scope for this issue.
Comment #30
andypostAdded polyfill without usage in #3523284: Update remaining Composer dependencies for 11.2.0 but not sure it will be accepted
Comment #31
andypostbtw there's 1.32 https://github.com/symfony/polyfill-php85
Comment #32
andypostas new array functions already added
https://wiki.php.net/rfc/array_first_last
Comment #33
andypostpolyfill added via #3523284: Update remaining Composer dependencies for 11.2.0
renamed and updated IS
Comment #34
andypostComment #35
mstrelan commentedNot sure if we need to do anything here or should just close it. The idea was to find a use case so we could add the dependency, but now it's been added anyway so no need to do this. Unless there is a rector for this maybe?
Comment #36
andypostyep, rector rule + php85 sounds a good fit here
Comment #37
mstrelan commentedI had a look at the available rector rules for PHP 8.4 and none of them seem useful here.
I don't think we need to refactor existing loops to use the new functions, I was only trying to do that to justify getting the polyfill in, but we have that now. I think we should just close this.
Opened #3526515: Use get_error_handler() in php 8.5 and deprecate Error::currentErrorHandler for 8.5
Comment #38
larowlanComment #39
larowlanAgree that #3523284: Update remaining Composer dependencies for 11.2.0 got the polyfill in and feels like there's little point in searching for usages where we can use e.g. \array_find in old code.
Comment #40
xjmUpdating saved credits.