Slide 1

Slide 1 text

PHP 8 and Legacy Code A case-study Juliette Reinders Folmer Tweet about it: @jrf_nl #OnlinePHPConference © Sebastian Bergmann

Slide 2

Slide 2 text

PHP 8 26 November 2020 © FlyClipArt

Slide 3

Slide 3 text

New in PHP 8.0 Match control structure Attributes Nullsafe object operator Trailing comma in closure use Constructor property promotion Non capturing catch Union types Mixed type Static return type Throw expressions Stringable Magic method signature check Stable sorting ::class on objects Syntax tweaks Named arguments Reclassify engine warnings Saner numeric strings Saner string to number comparisons Locale independent float to string cast Stricter type check for arithmetic/ bitwise operators Consistent type errors for internal functions "bug fixes"

Slide 4

Slide 4 text

An Example

Slide 5

Slide 5 text

“ As a rare exception, this BC break will probably hit php-src harder than actual users of PHP "Backward Incompatible Changes" as per the Consistent type errors for internal functions RFC

Slide 6

Slide 6 text

Another example

Slide 7

Slide 7 text

test Test TEST

Slide 8

Slide 8 text

Test Requirements Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 9

Slide 9 text

PHP 8 Compatibility Checklist ❑ Wait for PHP 8 compatible tooling ❑ Get the test suite running on PHP 8 ❑ Run static analysis ❑ Fix every issue ❑ Update external dependencies ❑ Review public parameter names

Slide 10

Slide 10 text

WordPress A case-study

Slide 11

Slide 11 text

WordPress Test Suite Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 12

Slide 12 text

Testing WordPress on PHP 8 July August September October November December First PHP 8 tickets opened

Slide 13

Slide 13 text

PHPUnit vs PHP PHPUnit / PHP 5.6 7.0 7.1 7.2 7.3 7.4 8.0 5.x 6.x 7.x 8.x 9.x 9.3+ void

Slide 14

Slide 14 text

Testing WordPress on PHP 8 July August September October November December First PHP 8 tickets opened Drop PHP < 7.1 ?

Slide 15

Slide 15 text

Let's Look at the Stats

Slide 16

Slide 16 text

Let's Look at the Stats

Slide 17

Slide 17 text

Let's Look at the Stats

Slide 18

Slide 18 text

Let's Look at the Stats

Slide 19

Slide 19 text

Let's Look at the Stats

Slide 20

Slide 20 text

Testing WordPress on PHP 8 July August September October November December First PHP 8 tickets opened Drop PHP < 7.1 ? Fixed rolling schedule proposal

Slide 21

Slide 21 text

Constraints and Roadblocks ▪ Supporting PHP 5.6 + 7.0 still required ▪ Plugin tests often extend WP integration test suite ▪ PHPUnit < 9.3 not compatible with PHP 8.0 - Mocking - @runInSeparateProcess ▪ PHPUnit 7 Phar will not run on PHP 8.0 ▪ Committed composer.lock file

Slide 22

Slide 22 text

Solution "autoload-dev": { "files": [ "tests/includes/MockObject/Builder/NamespaceMatch.php", "tests/includes/MockObject/Builder/ParametersMatch.php", "tests/includes/MockObject/InvocationMocker.php", "tests/includes/MockObject/MockMethod.php" ], "exclude-from-classmap": [ "vendor/phpunit/…/MockObject/Builder/NamespaceMatch.php", "vendor/phpunit/…/MockObject/Builder/ParametersMatch.php", "vendor/phpunit/…/MockObject/InvocationMocker.php", "vendor/phpunit/…/MockObject/MockMethod.php" ] }, ▪ Composer, not phar ▪ Lock at PHPUnit 7.5 ▪ Downgrade to PHPUnit 5 in CI for PHP 5.6, 7.0 ▪ Use MockObject classes from PHPUnit 9.3 ▪ Separate process => separate group

Slide 23

Slide 23 text

Testing WordPress on PHP 8 July August September October November December First PHP 8 tickets opened Drop PHP < 7.1 ? Fixed rolling schedule proposal Tests running on PHP 8 Tests passing on PHP 8

Slide 24

Slide 24 text

WordPress Test Suite Have tests Strict assertions High code coverage, strictly measured Happy & unhappy path

Slide 25

Slide 25 text

Testing WordPress on PHP 8 July August September October November December First PHP 8 tickets opened Drop PHP < 7.1 ? Fixed rolling schedule proposal Tests running on PHP 8 Tests passing on PHP 8 WP/ PHP 8 compat report External dependency updates WP.5.6 released "beta" compatible with PHP 8

Slide 26

Slide 26 text

"beta-compatible" Fix all KNOWN issues Undiscovered issues ? Parameter name review Plugins & themes not being compatible Plugins & themes doing it wrong™

Slide 27

Slide 27 text

WordPress Plugins & Themes

Slide 28

Slide 28 text

Test Requirements Happy & unhappy path High code coverage, strictly measured Strict assertions Have tests

Slide 29

Slide 29 text

Constraints Support multiple WP versions Support multiple PHP versions No control over run environment

Slide 30

Slide 30 text

Mock-based Tests PHPUnit 5.x composer.lock Platform - php 5.6 PHPUnit 5.x – 9.x CI: composer update … --ignore-platform-reqs Needs PHPUnit cross-version compatibility layer

Slide 31

Slide 31 text

WP Integration Tests PHPUnit 5.x composer.lock Platform - php 5.6 PHPUnit 5.x – 7.x CI: composer update … --ignore-platform-reqs Need PHPUnit cross-version compatibility layer Need custom autoload for MockObject

Slide 32

Slide 32 text

New Tooling PHPUnit Polyfills WP Test Utils Sponsored by: Alternative: Symfony Bridge * Supports PHPUnit 4.8 – 9.x, PHP 5.5 – 8.0

Slide 33

Slide 33 text

“ The barrier for updating legacy code to use a more modern PHP version has gone up exponentially and for some, may even have become insurmountable. Juliette Reinders Folmer "A Perfect Storm"

Slide 34

Slide 34 text

Further Reading ▪ The 2020 WordPress and PHP 8 Compatibility Report https://developer.yoast.com/blog/the-2020-wordpress-and-php-8- compatibility-report/ ▪ A Perfect Storm https://24daysindecember.net/2020/12/21/a-perfect-storm/ ▪ PHPUnit Polyfills https://packagist.org/packages/yoast/phpunit-polyfills ▪ WP Test Utils https://packagist.org/packages/yoast/wp-test-utils

Slide 35

Slide 35 text

Thanks! Any questions ? Slides: https://speakerdeck.com/jrf Feedback: https://joind.in/talk/dafa1 @jrf_nl @jrfnl