-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Use static closures whenever $this is not used to avoid memory leaks
#4457
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
Conversation
composer.json
Outdated
| "require-dev": { | ||
| "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", | ||
| "squizlabs/php_codesniffer": "3.6.0", | ||
| "squizlabs/php_codesniffer": "^3.7.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious why PHPCS was pinned back on 3.6.0. The current version is 3.7.2. @jrfnl I'm sure you know!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd definitely recommend an update, but until WPCS 3.0.0 is released the point is moot.
PHPCS 3.6.1 - 3.7.2 do contain some useful bugfixes, but the majority of updates are related to supporting new PHP syntaxes, which WP Core cannot use anyway since the minimum PHP version is 5.6, so for WP the benefit of updating would be minimal.
Also see the changelogs: https://github.com/squizlabs/PHP_CodeSniffer/releases
PHPCS does need to be pinned to a specific version as there is no committed composer.lock file and having PHPCS be a moving target which can fail builds at random whenever a new version is tagged is not desirable, so using ^ in the version constraint is out of the question.
Aside from that, I'd recommend an update to PHPCS to be done in a separate PR as it may throw new warnings/errors/allow for ignore annotations to be removed, which should not be mixed in with this PR.
composer.json
Outdated
| "phpcompatibility/phpcompatibility-wp": "~2.1.3", | ||
| "yoast/phpunit-polyfills": "^1.0.1" | ||
| "yoast/phpunit-polyfills": "^1.0.1", | ||
| "slevomat/coding-standard": "^8.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this sniff requires PHP 7.2+. Per GHA log:
Error: Your requirements could not be resolved to an installable set of packages.
Problem 1
- slevomat/coding-standard[8.0.0, ..., 8.12.0] require php ^7.2 || ^8.0 -> your php version (5.6.40) does not satisfy that requirement.
- Root composer.json requires slevomat/coding-standard ^8.0 -> satisfiable by slevomat/coding-standard[8.0.0, ..., 8.12.0].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... which is why it is not a dependency of WPCS (aside from a wide variety of bugs and other PHPCS non-standard behaviour, including adding additional external dependencies, which would need to be fixed first anyway).
jrfnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my inline comments.
While it is fine to use external sniffs to improve the WP codebase, adding those to the ruleset is a big no-no.
composer.json
Outdated
| "phpcompatibility/phpcompatibility-wp": "~2.1.3", | ||
| "yoast/phpunit-polyfills": "^1.0.1" | ||
| "yoast/phpunit-polyfills": "^1.0.1", | ||
| "slevomat/coding-standard": "^8.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... which is why it is not a dependency of WPCS (aside from a wide variety of bugs and other PHPCS non-standard behaviour, including adding additional external dependencies, which would need to be fixed first anyway).
composer.json
Outdated
| "require-dev": { | ||
| "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", | ||
| "squizlabs/php_codesniffer": "3.6.0", | ||
| "squizlabs/php_codesniffer": "^3.7.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd definitely recommend an update, but until WPCS 3.0.0 is released the point is moot.
PHPCS 3.6.1 - 3.7.2 do contain some useful bugfixes, but the majority of updates are related to supporting new PHP syntaxes, which WP Core cannot use anyway since the minimum PHP version is 5.6, so for WP the benefit of updating would be minimal.
Also see the changelogs: https://github.com/squizlabs/PHP_CodeSniffer/releases
PHPCS does need to be pinned to a specific version as there is no committed composer.lock file and having PHPCS be a moving target which can fail builds at random whenever a new version is tagged is not desirable, so using ^ in the version constraint is out of the question.
Aside from that, I'd recommend an update to PHPCS to be done in a separate PR as it may throw new warnings/errors/allow for ignore annotations to be removed, which should not be mixed in with this PR.
phpcs.xml.dist
Outdated
| <exclude-pattern>/src/wp-content/themes/twentyfourteen/inc/featured-content\.php</exclude-pattern> | ||
| </rule> | ||
|
|
||
| <rule ref="SlevomatCodingStandard.Functions.StaticClosure" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be removed.
It is not a rule in the official coding standards, so, while I agree, it is a good "best practice", we cannot demand for it to be enforced.
|
|
||
| // The likes of block element styles from theme.json do not have $metadata['name'] set. | ||
| if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { | ||
| $result = array_values( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a file controlled by gutenberg. This change will need to made in the gutenberg repo and ported across.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self, this is the Gutenberg file: https://github.com/WordPress/gutenberg/blob/trunk/lib/global-styles-and-settings.php
| * Check for allowed pseudo classes (e.g. ":hover") from the $selector ("a:hover"). | ||
| * This also resets the array keys. | ||
| */ | ||
| $pseudo_matches = array_values( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a file controlled by gutenberg. This change will need to made in the gutenberg repo and ported across.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that should be indicated in the file DocBlock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self, Gutenberg file located at https://github.com/WordPress/gutenberg/blob/trunk/lib/class-wp-theme-json-data-gutenberg.php
spacedmonkey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to remove all the coding standard changes. Let's just keep this today code changes. Also, please remove all gutenberg related changes.
@jrfnl Just curious why adding these to the ruleset is not OK. I thought the ruleset here was exclusively for wordpress-develop, as it's not located in the WPCS repo. |
…into trac-58323 * 'trunk' of https://github.com/WordPress/wordpress-develop: Media: Increase default for `wp_omit_loading_attr_threshold` to 3. Media: Prevent CSRF setting attachment thumbnails. Embeds: Add protocol validation for WordPress Embed code. Editor: Ensure block comments are of a valid form. Editor: Remove shortcode support from block templates. I18N: Introduce sanitization function for locale. Taxonomy: Do not prime term meta in `wp_get_object_terms`. Docs: Clarify `@param` types on `get_sample_permalink_html` filter.
True, but if we want to enforce something for WP Core, I keep being told that it has to go through a) a Make post, b) a change to the formal coding standards docs, c) only after that the standard can be changed. I don't see why it would be any different for you. 🤷🏻♀️ Aside from that, adding the Slevomat standard would be prohibitive for running a |
jrfnl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current state of this PR is all okay.
While looking through the PR, I do see some other things which should probably be addressed (long closures which should be named functions, unnecessary use of references and closure use), but that's definitely outside the scope of this PR and for another time ;-).
| * @return string Returns the block content. | ||
| */ | ||
| $settings['render_callback'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
| $settings['render_callback'] = static function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outside of the scope of this PR, but we may as well remove the // phpcs:ignore comment as the VariableAnalysis standard is not being used by Core, nor WPCS.
Isn't this also a Gutenberg file though ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this also a Gutenberg file though ?
Based on the filename it would seem so, but it seems to fundamentally be a different file from https://github.com/WordPress/gutenberg/blob/trunk/lib/blocks.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we should look into // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, but not in this ticket.
spacedmonkey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good.
|
Committed to |
statickeyword to eligible closures viacomposer run-script format -- --sniffs=SlevomatCodingStandard.Functions.StaticClosureTrac ticket: https://core.trac.wordpress.org/ticket/58323
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.