Skip to content

PHP 8.1 compatibility #10250

@emteknetnz

Description

@emteknetnz

Split off from #10219

Upgrade all the support modules to support php 8.1

Agreed way forward to resolve common errors #10237 (comment)

Reference to some breaking changes in php 8.1 - https://stitcher.io/blog/new-in-php-81#breaking-changes

Note

Steve created some science experiment to add a _a method that can note docblock/parameter type mismatched and a _c method that can recast variable to their expected type. He also has a script to apply it to the entire code base.

Update: this approach won't work for php 8.1. Reason being that the _a() method profiling will certainly detect the say all the string and null arguments being passed in for a param, we cannot determine whether or not null is valid in this instance or not, so we don't know if _c('string', $myvar, 1) should be included or not

Acceptance criteria

  • PHP8.1 build are green.
  • We don't break semver.
  • The magical process for auto updating our code base is documented somewhere. (it's in this issue)
  • CI fails on php deprecation warnings
  • graphql4 is also updated
  • Ensure an installable version of php 8.1 compatible tractorcow deps is available - see ENH PHP 8.1 compatibility tractorcow/classproxy#5 (comment)

Explanation of the conversion process in the PRs below

  • Automatic code modification using https://github.com/emteknetnz/php81-fixer
  • Static code analysis that ensures any arguments passed to native php functions are not null
    - If the native param only accepts a single type of (string|bool|int|float) then it is cast i.e (string)
    - If the native param only accepts an array then it uses the ternary operator i.e. ?: []
    - If the native param accepts multiple types then it uses the ternary operator with a bias to using string i.e. ?: ''
  • Use the null coalescing operator for variables passed to native functions that do not accept null values
  • There are some conditions where arguments are not converted such as the argument is a native constant, is an inner native function call with a single return type, is using the class static property e.g. SiteTree::class

Because this is an automated process this certainly goes on the side of over converting code. If we move to full-type hinting param and return type in the future, we could run another code sweep to remove a bunch of the code conversions. Currently we cannot rely on the docblock types as they are very unreliable.

There are also #[\ReturnTypeWIllChange] method attributes added to class methods where relevant, as well as some other methods that are updated, such as adding __serialized()/__unserialized()

Current PHP 8.1 unit test status

Follow up PRs

PRs

GraphQL 3 PRs

"Manual fix" PRs

solr-php-client fork

Installer (all prs use null coalesce)

No changes

  • silverstripe/silverstripe-errorpage
  • silverstripe/silverstripe-siteconfig

Kitchen-sink (all prs use null coalesce)

No changes

  • content-widget
  • crontask
  • elemental-fileblock
  • security-extensions
  • taxonomy
  • dnadesign/silverstripe-elemental-userforms
  • tractorcow/silverstripe-proxy-db

PRs for dependencies

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions