Skip to content
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

PHP 8.2 | Gutenberg is introducing deprecated callables into WP Core #44536

Closed
jrfnl opened this issue Sep 28, 2022 · 9 comments · Fixed by #44561
Closed

PHP 8.2 | Gutenberg is introducing deprecated callables into WP Core #44536

jrfnl opened this issue Sep 28, 2022 · 9 comments · Fixed by #44561
Assignees
Labels
[Package] Style Engine /packages/style-engine [Priority] High Used to indicate top priority items that need quick attention

Comments

@jrfnl
Copy link
Member

jrfnl commented Sep 28, 2022

Copied over from https://core.trac.wordpress.org/ticket/56467#comment:242 for visibility

While looking at the test runs of WP Core, I've found that WP Core PR 3199 as committed in WP Core 54156 introduces four new incompatibilities with PHP 8.2.

The tests committed with that same PR are insufficient and did not flag the problem. The tests committed in WP Core 54211 luckily did flag the problem.

The problem is with the following code:

class WP_Style_Engine {
	const BLOCK_STYLE_DEFINITIONS_METADATA = array(
		/* <snip> */
		'border'     => array(
			/* <snip> */
			'top'    => array(
				'value_func' => 'static::get_individual_property_css_declarations',
				'path'       => array( 'border', 'top' ),
				'css_vars'   => array(
					'color' => '--wp--preset--color--$slug',
				),
			),
			'right'  => array(
				'value_func' => 'static::get_individual_property_css_declarations',
				'path'       => array( 'border', 'right' ),
				'css_vars'   => array(
					'color' => '--wp--preset--color--$slug',
				),
			),
			'bottom' => array(
				'value_func' => 'static::get_individual_property_css_declarations',
				'path'       => array( 'border', 'bottom' ),
				'css_vars'   => array(
					'color' => '--wp--preset--color--$slug',
				),
			),
			'left'   => array(
				'value_func' => 'static::get_individual_property_css_declarations',
				'path'       => array( 'border', 'left' ),
				'css_vars'   => array(
					'color' => '--wp--preset--color--$slug',
				),
			),
		),
		/* <snip> */
	);

	/* <snip> */
}

Each of these definitions use 'value_func' => 'static::get_individual_property_css_declarations'.

That syntax for declaring a callable is deprecated as of PHP 8.2 and will result in a fatal error as of PHP 9.0.

Changing this may require an architectural change to the class.

To explain:

  1. The typical PHP-cross-version replacement for 'static::get_individual_property_css_declarations' would be array( static::class, 'get_individual_property_css_declarations' ).
  2. However, static::class cannot be used in a constant declaration as constant values have to be ''constant'' and how static::class resolves can change at runtime depending on the class hierarchy.

I'd like to understand why the choice was made to use static:: in these declarations.
I currently do not see any class within WP Core which extends the WP_Style_Engine class, so why is static:: being used instead of self:: ?

As for solution directions:

  • If static:: was not used intentional and can be replaced with self::, the callables need to be rewritten to array( self::class, 'get_individual_property_css_declarations' ).
  • If static:: was used intentionally, an architectural change is needed as the callable cannot be declared like that in the constant.
    • Changing the constant to a private property without the callables and adding them to the array in the class constructor is not an option as the class only contains static methods and does not seem to be intended to be instantiated.
    • Changing the constant to a private static property is not an option either, as same as constants and non-static properties, the initial value for a property needs to be ''constant''.

In other words: If static:: was used intentionally, the class will probably need a rethink...

As this is a new class and any architectural change in it in the future would be considered a breaking change, this MUST be fixed before WP 6.1 is released.

And for the love of code, please, please add better tests to safeguard against these kind of problems and check the PHP 8.1 and 8.2 test logs to prevent introducing new issues like this.

Also see: https://wiki.php.net/rfc/deprecate_partially_supported_callables

@jrfnl jrfnl added the [Priority] High Used to indicate top priority items that need quick attention label Sep 28, 2022
@talldan
Copy link
Contributor

talldan commented Sep 29, 2022

check the PHP 8.1 and 8.2 test logs to prevent introducing new issues like this.

@jrfnl Is there any way the tests can be made to fail if there are such critical issues? It'd be great if continuous integration could help prevent these kind of errors.

edit: I'm also wondering how a developer can check the logs. I don't see anything being logged in the github check - https://github.com/WordPress/wordpress-develop/actions/runs/3084932834/jobs/4987654678.

@ramonjd
Copy link
Member

ramonjd commented Sep 29, 2022

Thanks for creating this issue and the great explanation.

I'd like to understand why the choice was made to use static:: in these declarations.

I believe the use of static:: was in imitation of the WP Theme JSON class, which, at least in the Gutenberg plugin relies on extensibility to overwrite class functionality. Example

I currently do not see any class within WP Core which extends the WP_Style_Engine class, so why is static:: being used instead of self:: ?

Switching the callables to array( self::class, 'get_individual_property_css_declarations' ) as you suggest appears to be the best option. I'm pretty sure the WP_Style_Engine class will not (and should not) be extended, in fact, I might add a comment to that effect in the class.

this MUST be fixed before WP 6.1 is released.

Agree. I'm happy to throw up 2 x patches (Core and a Gutenberg sync). Will do it ASAP

@jrfnl
Copy link
Member Author

jrfnl commented Sep 29, 2022

check the PHP 8.1 and 8.2 test logs to prevent introducing new issues like this.

@jrfnl Is there any way the tests can be made to fail if there are such critical issues? It'd be great if continuous integration could help prevent these kind of errors.

@talldan Well, to start, it would help if the tests for the PHP code in GB would actually be run on PHP 5.6 up to PHP 8.2, instead of only being run on PHP 7.4.... 😢

See: #43333 (comment)

edit: I'm also wondering how a developer can check the logs. I don't see anything being logged in the github check - https://github.com/WordPress/wordpress-develop/actions/runs/3084932834/jobs/4987654678.

That's correct as you are looking in the wrong place, though I have to admit, it's on my pet peeve list that the PHPCompatibility workflow hasn't been set up to also show the issues in the logs. It's on my long term to-do list to get that fixed.

For now, issues thrown by PHPCompatibility will only show as comments in the PR, but PHPCompatibility wouldn't pick up on the issues I reported here yet, as there is no release (yet) containing PHP 8.2 related sniffs.

What I meant was to check the actual unit test run logs. The test runs for PHP 8.1 and PHP 8.2 (for WP Core) are still "allowed to fail" due to the sheer amount of issues which had to be fixed, but the logs will show the issues.

So for this issue, you can see the errors in the actual unit test runs on PHP 8.2 here: https://github.com/WordPress/wordpress-develop/actions/runs/3084932812/jobs/4987657992#step:19:306

Hope that helps ?

@jrfnl
Copy link
Member Author

jrfnl commented Sep 29, 2022

I believe the use of static:: was in imitation of the WP Theme JSON class, which, at least in the Gutenberg plugin relies on extensibility to overwrite class functionality. Example

@ramonjd thank you for providing this context. Highlights again that we all need to be careful when copying over code patterns as they may not be appropriate for another class.

Switching the callables to array( self::class, 'get_individual_property_css_declarations' ) as you suggest appears to be the best option.

👍🏻

I'm pretty sure the WP_Style_Engine class will not (and should not) be extended, in fact, I might add a comment to that effect in the class.

A comment is a good first step, but what about making the class final ? That way we can prevent the class from ever being extended.

(After all, we all know that plugins will try to do things if they can, no matter what we say in comments/docblocks - if they can't extend the class, we don't have that problem).

this MUST be fixed before WP 6.1 is released.

Agree. I'm happy to throw up 2 x patches (Core and a Gutenberg sync). Will do it ASAP

Much appreciated! Feel free to ping me if you want a review/approval.

@ramonjd
Copy link
Member

ramonjd commented Sep 29, 2022

A comment is a good first step, but what about making the class final ?

I don't see any issues here. Given that it'd be great to avoid the compat woes that exist for WP Theme JSON, I'm for it.

cc @aristath and @andrewserong

Patches incoming... !

In fact it was already raised by @costdev in the original backport PR

@andrewserong
Copy link
Contributor

I don't see any issues here. Given that it'd be great to avoid the compat woes with WP Theme JSON I'm for it.

Same here — so far I think the intention has been for the style engine classes to be mostly used internally in WordPress, and for public functions to be used as the entry point for other use cases, e.g. plugins and themes. But I believe Ari has had more of the plugin/theme use case in mind so might have some insights to add there, too.

@talldan
Copy link
Contributor

talldan commented Sep 29, 2022

The test runs for PHP 8.1 and PHP 8.2 (for WP Core) are still "allowed to fail" due to the sheer amount of issues which had to be fixed

I suppose an option could be to run the tests on trunk and then on the PR and check whether the number of errors reported increased. If so, fail the job. I don't know whether that's easy to do though. It wouldn't be foolproof if the PR fixed some errors but introduced others.

@jrfnl
Copy link
Member Author

jrfnl commented Sep 29, 2022

The test runs for PHP 8.1 and PHP 8.2 (for WP Core) are still "allowed to fail" due to the sheer amount of issues which had to be fixed

I suppose an option could be to run the tests on trunk and then on the PR and check whether the number of errors reported increased. If so, fail the job. I don't know whether that's easy to do though. It wouldn't be foolproof if the PR fixed some errors but introduced others.

I am actively working, together with some other people, to get it down to zero and remove the continue-on-error for both PHP 8.1 and PHP 8.2. We are nearly there, only a few more issues to go (which is why anything new being introduced now really stands out like a sore thumb...)

jrfnl added a commit to jrfnl/wordpress-develop that referenced this issue Sep 29, 2022
As it was, results of PHPCS runs would only ever show inline in a commit code view/PR code view.

This is confusing as people will often try to find an output log of the run in the GH Actions build log, not realizing the scan results are not available in the GH Actions log.

This commit changes the steps in the GH Actions workflows for both the PHPCS run against the WordPress Coding Standards as well as for the run against PHPCompatibility to show the full scan results report in the GH Actions logs **_as well as_** show the results in commit/PR code views and still fail the build correctly when new issues are detected.

I've elected to store the (temporary) `phpcs-report.xml` file in the `artifacts` subdirectory as that directory is (git/svn)-ignored by default already to prevent this new, temporary file from interfering with the `git diff` check at the end of the workflows.

Refs:
* https://github.com/staabm/annotate-pull-request-from-checkstyle#using-php_codesniffer
* WordPress/gutenberg#44536 (comment)
jrfnl added a commit to jrfnl/wordpress-develop that referenced this issue Sep 29, 2022
As it was, results of PHPCS runs would only ever show inline in a commit code view/PR code view.

This is confusing as people will often try to find an output log of the run in the GH Actions build log, not realizing the scan results are not available in the GH Actions log.

This commit changes the steps in the GH Actions workflows for both the PHPCS run against the WordPress Coding Standards as well as for the run against PHPCompatibility to show the full scan results report in the GH Actions logs **_as well as_** show the results in commit/PR code views and still fail the build correctly when new issues are detected.

I've elected to store the (temporary) `phpcs-report.xml` file in the `.cache` subdirectory as that directory is (git/svn)-ignored by default already to prevent this new, temporary file from interfering with the `git diff` check at the end of the workflows.

Refs:
* https://github.com/staabm/annotate-pull-request-from-checkstyle#using-php_codesniffer
* WordPress/gutenberg#44536 (comment)
jrfnl added a commit to jrfnl/wordpress-develop that referenced this issue Sep 29, 2022
As it was, results of PHPCS runs would only ever show inline in a commit code view/PR code view.

This is confusing as people will often try to find an output log of the run in the GH Actions build log, not realizing the scan results are not available in the GH Actions log.

This commit changes the steps in the GH Actions workflows for both the PHPCS run against the WordPress Coding Standards as well as for the run against PHPCompatibility to show the full scan results report in the GH Actions logs **_as well as_** show the results in commit/PR code views and still fail the build correctly when new issues are detected.

I've elected to store the (temporary) `phpcs-report.xml` file in the `.cache` subdirectory as that directory is (git/svn)-ignored by default already to prevent this new, temporary file from interfering with the `git diff` check at the end of the workflows.

Note: I'm also removing the `-q` (quiet) flag from the PHPCS runs as it could give the misleading impression that nothing happened in the step for a successfull run. The progress report being shown will take away that impression.

Refs:
* https://github.com/staabm/annotate-pull-request-from-checkstyle#using-php_codesniffer
* WordPress/gutenberg#44536 (comment)
@jrfnl
Copy link
Member Author

jrfnl commented Sep 29, 2022

That's correct as you are looking in the wrong place, though I have to admit, it's on my pet peeve list that the PHPCompatibility workflow hasn't been set up to also show the issues in the logs. It's on my long term to-do list to get that fixed.

@talldan FYI: WordPress/wordpress-develop#3368

jrfnl added a commit to jrfnl/wordpress-develop that referenced this issue Sep 29, 2022
As it was, results of PHPCS runs would only ever show inline in a commit code view/PR code view.

This is confusing as people will often try to find an output log of the run in the GH Actions build log, not realizing the scan results are not available in the GH Actions log.

This commit changes the steps in the GH Actions workflows for both the PHPCS run against the WordPress Coding Standards as well as for the run against PHPCompatibility to show the full scan results report in the GH Actions logs **_as well as_** show the results in commit/PR code views and still fail the build correctly when new issues are detected.

I've elected to store the (temporary) `phpcs-report.xml` file in the `.cache` subdirectory as that directory is (git/svn)-ignored by default already to prevent this new, temporary file from interfering with the `git diff` check at the end of the workflows.

Note: I'm also removing the `-q` (quiet) flag from the PHPCS runs as it could give the misleading impression that nothing happened in the step for a successfull run. The progress report being shown will take away that impression.

Refs:
* https://github.com/staabm/annotate-pull-request-from-checkstyle#using-php_codesniffer
* WordPress/gutenberg#44536 (comment)
Repository owner moved this from Needs Review to Done in WordPress 6.1 Editor Tasks Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Style Engine /packages/style-engine [Priority] High Used to indicate top priority items that need quick attention
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

4 participants