WordPress.org

Make WordPress Core

Opened 4 weeks ago

Last modified 2 weeks ago

#47751 new defect (bug)

PHP 7.4 compatibility fix / accessing arrays/string using curly brace syntax

Reported by: jrf Owned by:
Milestone: 5.3 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch has-unit-tests php74
Focuses: coding-standards Cc:

Description

PHP used to allow both square brackets and curly braces to be used interchangeably for accessing array elements and string offsets.

<?php
$array = [1, 2];
echo $array[1]; // prints 2
echo $array{1}; // also prints 2
 
$string = "foo";
echo $string[0]; // prints "f"
echo $string{0}; // also prints "f"

The curly bracket syntax is only allowed in a limited set of cases and can be confusing for people not used to it.

PHP 7.4 will now deprecate the curly brace syntax for accessing array elements and string offsets and it is expected that support will be completely removed in PHP 8.0.

Ref: https://wiki.php.net/rfc/deprecate_curly_braces_array_access

The patches

A recent Travis build against the current trunk already shows unit tests failing because of the deprecation errors being thrown on PHP 7.4: https://travis-ci.com/WordPress/wordpress-develop/jobs/218044529

The WordPress code-base containes 182 instances of curly brace array access.

These 182 can be divided into issues in WP native files and in files from four external dependencies:

# Files # Issues
WP native files 4 11
PemFTP 1 2
POP3 1 1
Services_JSON 1 40
getID3 7 128

Similar to #47746, while the issues in external dependencies should be solved there, this may not be a feasible road.

PemFTP

I have not been able to find references to the canonical location where this code is publicly maintained, nor has a search of typical source repositories yielded anything.

The canonical URL mentioned in the file docblock points to PHPClasses where the code hasn't been updated for over 10 years.

With that in mind, I very much doubt there will be a new release any time soon.

POP3

I have not been able to find references to the canonical location where this code is publicly maintained, nor has a search of typical source repositories yielded anything.

Based on the file docblock, this external dependency hasn't been updated since 2011.

If anyone can point me to where this code is maintained, please do.

Services_JSON

I have not been able to find references to the canonical location where this code is publicly maintained, nor has a search of typical source repositories yielded anything.

The canonical URL mentioned in the file docblock points to a proposal on PEAR where the code hasn't been updated for over 14 years.

As this essentially has been added to PHP core anyway, I doubt there will be a publicly maintained canonical version of this code.

Related #47699

getID3

Now, as for the worst offender... there is a public and actively maintained GitHub repo available.

My pull request containing the fixes for getID3 has already been merged.

But...

  1. It is unclear when the next version will be released.
  2. WordPress currently includes version 1.9.14-201706111222 (June 2017), while the latest release is version 1.9.17, released February this year and version 2.0 is already in alpha.

So a decision would need to be taken about updating the dependency once the next version is released.

Conclusion

Based on the above, I will be uploading patches for all instances of the issue, though I will split the patches based on the code origin.

Like I've said in https://core.trac.wordpress.org/ticket/47746#comment:3 :

So, yes, while I agree that the fixes should primarily be made upstream, I don't think WP should be throwing deprecation notices when run on PHP 7.4 while we are waiting for
1) new releases from upstream and
2) the existing WP Core versions of those libraries to be updated.

A passing build on a branch containing all the patches can be found here: https://travis-ci.org/jrfnl/wordpress-develop/builds/561922102

These patches should cover all instances of this issue in the current codebase.
Based on the current trunk in combination with these patches, there are no remaining issues.

Other notes

Should there be a guideline in the WordPress Coding Standards Handbook that curly braces for array access should not be used ?

And if so, maybe now is the time to have another look at #46152, as the next version of PHPCompatibility will contain a sniff to detect these issues.

/cc @pento

Attachments (5)

47751-WP-native-files.patch (3.4 KB) - added by jrf 4 weeks ago.
[WP native files] PHP 7.4 compatibility: fix deprecated curly brace array access
47751-External-PemFTP.patch (1.0 KB) - added by jrf 4 weeks ago.
[External PemFTP] PHP 7.4 compatibility: fix deprecated curly brace array access
47751-External-Services_JSON.patch (12.5 KB) - added by jrf 4 weeks ago.
[External Services_JSON] PHP 7.4 compatibility: fix deprecated curly brace array access
47751-External-POP3.patch (1.0 KB) - added by jrf 4 weeks ago.
[External POP3] PHP 7.4 compatibility: fix deprecated curly brace array access
47751-External-getID3.patch (28.0 KB) - added by jrf 4 weeks ago.
[External getID3] PHP 7.4 compatibility: fix deprecated curly brace array access

Download all attachments as: .zip

Change History (9)

@jrf
4 weeks ago

[WP native files] PHP 7.4 compatibility: fix deprecated curly brace array access

@jrf
4 weeks ago

[External PemFTP] PHP 7.4 compatibility: fix deprecated curly brace array access

@jrf
4 weeks ago

[External Services_JSON] PHP 7.4 compatibility: fix deprecated curly brace array access

@jrf
4 weeks ago

[External POP3] PHP 7.4 compatibility: fix deprecated curly brace array access

@jrf
4 weeks ago

[External getID3] PHP 7.4 compatibility: fix deprecated curly brace array access

#1 @dd32
4 weeks ago

PemFTP

PHPClasses was the original source, and I don't know of any Canonical source for it anymore other than WordPress. I believe we've fixed other things in it since, so I would consider WordPress the most up to date source of that file.

POP3

The original sources of that class have long been abandoned and replaced in other projects with more modern alternatives, including in it's canonical location of SquirrelMail. WordPress is now the most recent variant of that class.

#2 @jorbin
2 weeks ago

In 45730:

PHP 7.4 compatibility fix / accessing arrays/string using curly brace syntax

PHP used to allow both square brackets and curly braces to be used interchangeably for accessing array elements and string offsets. The curly bracket syntax is only allowed in a limited set of cases and can be confusing for people not used to it.
PHP 7.4 will deprecate the curly brace syntax for accessing array elements and string offsets and it is expected that support will be completely removed in PHP 8.0.
Ref: https://wiki.php.net/rfc/deprecate_curly_braces_array_access

See #47751.
Props jrf.

#3 follow-up: @jorbin
2 weeks ago

  • Keywords php74 added

I included all of the patches other than getID3 as that is still an actively maintained project. Keeping this open as we should update that upstream library as they have already implemented this fix.

#4 in reply to: ↑ 3 @jrf
2 weeks ago

  • Keywords 2nd-opinion removed

Replying to jorbin:

I included all of the patches other than getID3 as that is still an actively maintained project. Keeping this open as we should update that upstream library as they have already implemented this fix.

Thanks for getting this in @jorbin <3

FYI: Both the 1.9 (master) branch as well as the 2.0 branch of GetID3 have been patched, though there is no new release yet containing the patches.

Note: See TracTickets for help on using tickets.