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 valid function names in the packages/block-library/src/*/*.php files #53438

Merged
merged 49 commits into from
Aug 16, 2023

Conversation

anton-vlasenko
Copy link
Contributor

@anton-vlasenko anton-vlasenko commented Aug 8, 2023

What?

This PR is focused on implementing a new PHPCS sniff, namely Gutenberg.NamingConventions.ValidBlockLibraryFunctionName.
Fixes #52769.

Why?

All the PHP function names within the ./packages/block-library/src/*/*.php files should start with one of the following prefixes:

  • block_core_<folder_name>
  • render_block_core_<folder_name>
  • register_block_core_<folder_name>

Here, <folder_name> represents the name of the folder where the associated file is situated. The folder name is converted to lowercase, and any characters other than letters and digits are substituted with underscores.

The introduction of this new PHPCS sniff is needed to ensure strict adherence to this naming pattern.

How?

The proposed ValidBlockLibraryFunctionNameSniff is configured to examine PHP function names contained within the ./packages/block-library/src/*/*.php files.

Whenever a function name deviates from the naming convention listed above, the PHPCS sniff will add an error to the PHPCS report.

Testing Instructions

  1. Make sure that CI jobs pass.
  2. Open the packages/block-library/src/calendar/index.php file in your preferred code editor.
  3. Add a new PHP function to the file:
/**
 * A test function.
 */
function block_core_calendar_() {

}
  1. Run the linter (npm run lint:php or composer run lint, based on your environment setup).
  2. Notice the linter error displayed in the console:
The function name "block_core_calendar_()" is invalid because PHP function names in this file should start with one of the following prefixes: "block_core_calendar", "render_block_core_calendar", "register_block_core_calendar".
  1. Rename the php function to block_core_clndr.
  2. Rerun the linter.
  3. Notice the linter error displayed in the console:
The function name "block_core_clndr()" is invalid because PHP function names in this file should start with one of the following prefixes: "block_core_calendar", "render_block_core_calendar", "register_block_core_calendar".
  1. Rename the php function to block_core_calendar.
  2. Rerun the linter.
  3. Confirm the absence of linter errors in the console.
  4. [Optional] Perform testing with other allowed prefixes, like render_block_core_calendar and register_block_core_calendar.

@anton-vlasenko anton-vlasenko added the [Type] Code Quality Issues or PRs that relate to code quality label Aug 8, 2023
@gziolo
Copy link
Member

gziolo commented Aug 9, 2023

See my comment in the parent issue: #52769 (comment). I think it's fine to add code comments that ignore existing violations and enforce good practices moving on.

phpcs.xml.dist Outdated Show resolved Hide resolved
@gziolo
Copy link
Member

gziolo commented Aug 10, 2023

It seems to be in a good shape, let me know when I can give a final review and approve.

@anton-vlasenko anton-vlasenko force-pushed the try/implement-sniff-for-block-library branch from 72d0e3f to b7583d3 Compare August 10, 2023 15:09
@anton-vlasenko anton-vlasenko changed the title Enforce valid function names in packages/block-library/*/src/index.php files Enforce valid function names in packages/block-library/*/src/index.php files Aug 10, 2023
@anton-vlasenko anton-vlasenko changed the title Enforce valid function names in packages/block-library/*/src/index.php files Enforce valid function names in packages/block-library/*/src/index.php files Aug 10, 2023
@anton-vlasenko anton-vlasenko marked this pull request as ready for review August 10, 2023 15:33
@anton-vlasenko
Copy link
Contributor Author

anton-vlasenko commented Aug 10, 2023

@gziolo

It seems to be in a good shape, let me know when I can give a final review and approve.

I had to address an edge case and write proper testing instructions.
Yes, please give it a final review. 🙏 Thank you!

@@ -87,11 +87,13 @@ function render_block_core_avatar( $attributes, $content, $block ) {
/**
* Generates class names and styles to apply the border support styles for
* the Avatar block.
* //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName
Copy link
Member

Choose a reason for hiding this comment

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

This and other files are going to be copied to WordPress core. Should this rule be present in both places and get prefixed with WordPress.? Maybe it's fine to have it only in Gutenberg, just flagging the fact that these files are tricky to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gziolo To use the WordPress. in the rule's name, it would need to become a part of the WordPress-Coding-Standards package (wpcs). However, getting this rule into the wpcs package might be challenging.

wpcs is currently undergoing a major overhaul. It's possible that you may need to wait for an undefined period of time until it's released.

Since this rule is currently essential for Gutenberg, my suggestion is to commit it to Gutenberg first. If the wpcs contributors agree that it should be included in wpcs, it can always be removed from the Gutenberg package.

To address the concern about maintainability, I will relocate the //phpcs:disable and //phpcs:enable statements from the doc blocks to prevent any interference.

Copy link
Member

Choose a reason for hiding this comment

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

We might have linting disabled on files copied from Gutenberg through WP packages, so you can double check that first before applying any changes. I guess we can live with the Gutenberg. specific rule if it doesn't throw errors on CI in WP core.

Copy link
Contributor Author

@anton-vlasenko anton-vlasenko Aug 11, 2023

Choose a reason for hiding this comment

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

@gziolo
The //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName statements shouldn't generate any linter errors in Core. However, personally, I'm not fond of the idea of having these comments in Core code.
I think I will add the possibility to ignore specific functions via the linter rule's configuration in phpcs.xml.dist. This way, we can avoid cluttering Core code with these phpcs: statements. I'll make the necessary updates to the PR and let you know.

Copy link
Member

@gziolo gziolo Aug 11, 2023

Choose a reason for hiding this comment

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

Yes, we could list these function names as exceptions in the config file. Not ideal, but it should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the PR.
So it can be reviewed now.
Thanks!

@@ -144,12 +144,14 @@ function block_core_navigation_add_directives_to_submenu( $w, $block_attributes

/**
* Replaces view script for the Navigation block with version using Interactivity API.
* //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName
*
* @param array $metadata Block metadata as read in via block.json.
*
* @return array Filtered block type metadata.
*/
function gutenberg_block_core_navigation_update_interactive_view_script( $metadata ) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm a little bit confused here with the gutenberg_ prefix and the usage of gutenberg_should_block_use_interactivity_api. The latter will trigger a fatal error in WordPress core when the files gets copied. This code should leave outside of this file, so in that regard the lining rule confirms that it's very useful.

Aside, we should also enforce that you can't use functions prefixed with gutenberg_ with core blocks in the same set of files as they don't exist in WP core.

As for this PR, it's fine to proceed with the ignore comment, but the usage of gutenberg_should_block_use_interactivity_api needs to be replaced with code present in WP core before we start backporting changes to WP core. @luisherranz, are you aware of this issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aside, we should also enforce that you can't use functions prefixed with gutenberg_ with core blocks in the same set of files as they don't exist in WP core.

Sure. This can enforced. I will update the PR, @gziolo.

Copy link
Member

@luisherranz luisherranz Aug 11, 2023

Choose a reason for hiding this comment

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

@luisherranz, are you aware of this issue?

Not at all. I don't have any experience backporting stuff to WP core yet, nor with how backporting requirements affect the code of Gutenberg. But happy to fix things if you give me instructions on what needs to be done, tho.

cc: @westonruter (pinging because Weston introduced the gutenberg_should_block_use_interactivity_api function, just in case)

Copy link
Member

@gziolo gziolo Aug 11, 2023

Choose a reason for hiding this comment

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

Related to that, I think it could be valuable to include a mandatory banner in every PHP file we know gets backported to WordPress core with the link to the document explaining all the expected requirements enforced by the complex process. It will also give a hint to folks whenever they try to update those files directly in the WordPress core which is going to be replaced the next time someone runs npm run build ...

Obviously not in this PR.

Copy link
Contributor Author

@anton-vlasenko anton-vlasenko Aug 11, 2023

Choose a reason for hiding this comment

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

Sure. This can enforced. I will update the PR, @gziolo.

I didn't realize that the linting rule doesn't allow any other functions to be defined outside of the ones specified in the prefixes config property.
Therefore, there's no need to modify the PR to restrict the gutenberg_ prefixed functions. 🤦‍♂️

The linting rule will only permit functions that start with the block_core_, render_block_core_, and register_block_core_ prefixes (+ the directory name).

The two functions with the gutenberg_ prefix (gutenberg_block_core_file_update_interactive_view_script and gutenberg_block_core_navigation_update_interactive_view_script) were allowed because of the phpcs::disable statements before them.

Given the discussion above, I'm uncertain about the course of action for these two gutenberg_ prefixed functions. Should they be allowed for now?

cc @gziolo @luisherranz

<property name="prefixes" type="array">
<element value="block_core_"/>
<element value="render_block_core_"/>
<element value="register_block_core_"/>
Copy link
Member

Choose a reason for hiding this comment

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

I see functions that start build_template_part_, so maybe we could open also build_block_core_template_part in case folks want to use that. I don't have strong opinions on that, as they could also do block_core_tempalte_part_build.

Copy link
Contributor Author

@anton-vlasenko anton-vlasenko Aug 11, 2023

Choose a reason for hiding this comment

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

I don't have a preference either. However, please note that the linting rule not only checks the prefix name but also the directory name, as requested here.

Copy link
Member

Choose a reason for hiding this comment

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

It's fine as is for now, but we should keep monitoring and extending the list if that is useful.

@@ -85,12 +85,14 @@ function register_block_core_footnotes() {

/**
* Saves the footnotes meta value to the revision.
* //phpcs:disable Gutenberg.NamingConventions.ValidBlockLibraryFunctionName
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR. I'm slightly surprised that the Footnotes block works correctly with WordPress 6.3. We don't guard calling these WP hooks when they are present in the WP core, so essential we register them twice with the Gutenberg plugin. We should double-check it and if necessary move all the code that handles WP hooks to another file in lib/compat/ with some sort of check if it was already applied and override them only if necessary. At the same time, we would have to move the same code to a different place in WordPress core. @ellatrix, what do you think?

@github-actions
Copy link

Flaky tests detected in 62d4d53.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5833436112
📝 Reported issues:

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

I like how the new rule works after a few refinements. In a follow-up we should include guidelines for contributing in @wordpress/block-library that explain how functions should be named. There is already a section for that here. We could use that later to reference in the message that the linting rule prints when an error gets detected.

I'm probably not the best person to review the code implementing the rule, so feel free to wait for another review from someone better suited for that task.

@anton-vlasenko
Copy link
Contributor Author

anton-vlasenko commented Aug 16, 2023

i'm probably not the best person to review the code implementing the rule, so feel free to wait for another review from someone better suited for that task.

Thank you for the code review, @gziolo.
I understand your concerns. I will ask around for help with code reviews.
Otherwise, I will rebase this PR onto trunk and merge it today evening (UTC+2).
Honestly, I don't see how anything bad could happen if this gets merged.
I've created 2 follow-up issues:
#53732
#53731

@gziolo
Copy link
Member

gziolo commented Aug 16, 2023

I understand your concerns. I will ask around for help with code reviews.
Otherwise, I will merge this PR today evening (UTC+2).
Honestly, I don't see how anything bad could happen if this gets merged.

Yes, it should be all good, but I wanted to set proper expectations regarding my review. The plan sounds perfect 😄

@anton-vlasenko anton-vlasenko force-pushed the try/implement-sniff-for-block-library branch from f5be78b to 1c25491 Compare August 16, 2023 14:20
@anton-vlasenko anton-vlasenko merged commit c991bfc into trunk Aug 16, 2023
50 checks passed
@anton-vlasenko anton-vlasenko deleted the try/implement-sniff-for-block-library branch August 16, 2023 17:15
@github-actions github-actions bot added this to the Gutenberg 16.6 milestone Aug 16, 2023
@anton-vlasenko anton-vlasenko changed the title Enforce valid function names in packages/block-library/*/src/index.php files Enforce valid function names in the packages/block-library/src/*/index.php files Aug 16, 2023
@anton-vlasenko anton-vlasenko changed the title Enforce valid function names in the packages/block-library/src/*/index.php files Enforce valid function names in the packages/block-library/src/*/*.php files Aug 16, 2023
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.

Enforce valid function names in packages/block-library/*/src/index.php files
3 participants