Skip to content

Conversation

@anton-vlasenko
Copy link
Contributor

@anton-vlasenko anton-vlasenko commented Apr 24, 2023

What?

This pull request introduces checks that prevent redeclaration of Gutenberg classes and functions. More information about this can be found here: https://github.com/WordPress/gutenberg/blob/trunk/lib/README.md#wrap-functions-and-classes-with--function_exists-and--class_exists
Fixes #44151

Why?

Currently, Gutenberg allows to define PHP functions (classes) with any prefix.
This repeatedly resulted in fatal errors due to naming conflicts between Core and Gutenberg.

How?

To avoid these conflicts, this pull request adds a new Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames sniff to phpcs.xml.dist.
This sniff verifies that the functions and classes are properly guarded.

Testing Instructions

  1. Review https://github.com/anton-vlasenko/Gutenberg-Coding-Standards/. This is a new repository that will be containing Gutenberg-specific sniffs.
  2. Make sure that CI jobs pass.

Please don't merge this PR for now. Why?

  1. https://github.com/anton-vlasenko/Gutenberg-Coding-Standards/ needs to be moved under https://github.com/WordPress account (if this PR gets approved).
    https://github.com/anton-vlasenko/Gutenberg-Coding-Standards/ is for reviewing purposes only.

@anton-vlasenko anton-vlasenko changed the title Check if classes and functions are guarded against redaclarion Enforce checks against redeclaration for functions and classes Apr 24, 2023
@anton-vlasenko anton-vlasenko force-pushed the add/guard-functions-and-classes-sniff branch 3 times, most recently from da087cf to 118e136 Compare April 28, 2023 21:53
@anton-vlasenko anton-vlasenko marked this pull request as ready for review April 28, 2023 21:57
@anton-vlasenko anton-vlasenko added [Type] Code Quality Issues or PRs that relate to code quality [Type] Feature New feature to highlight in changelogs. and removed [Type] Feature New feature to highlight in changelogs. labels Apr 28, 2023
@anton-vlasenko anton-vlasenko self-assigned this Apr 28, 2023
Guard WP_Html_Span.

Guard classes against redeclaration.

Master is the main branch now.

Fix CS errors.
</property>
<property name="classesWhiteList" type="array">
<element value="/^Gutenberg.+/"/>
<element value="/^WP_.+_Gutenberg$/"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is: should classes like WP_Some_Class_Name_Gutenberg be allowed? I suppose it's quite unlikely that the _Gutenberg suffix won't be removed during backporting to Core, so I would say it's fine to whitelist such classes.

@anton-vlasenko
Copy link
Contributor Author

Closed in favour of #52696.

@johnbillion johnbillion deleted the add/guard-functions-and-classes-sniff branch February 10, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement a lint rule that ensures that functions/classes with no "gutenberg_" prefix are properly guarded

2 participants