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

Enforce checks against redeclaration for functions and classes #50042

Closed
wants to merge 1 commit into from

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.

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
1 participant