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

WPCS Improvement #1123

Merged
merged 6 commits into from Apr 11, 2024
Merged

WPCS Improvement #1123

merged 6 commits into from Apr 11, 2024

Conversation

Chintesh
Copy link
Contributor

@Chintesh Chintesh commented Apr 11, 2024

Summary

Fixes #

Relevant technical choices

I've refactored the code to address PHPCS (PHP CodeSniffer) warnings and improve code readability and standards compliance.

  • Used isset() to check if a variable is set before accessing it to avoid illegal string offset warnings.
  • Replaced strpos() with str_contains() for more readable string containment checks.
  • Utilized filter_input() with appropriate sanitization for better handling of input data from superglobals.
  • Ensured that all global variables like $_SERVER are properly validated before use to prevent potential security vulnerabilities.

These changes aim to enhance the overall quality and maintainability of the codebase by adhering to best practices recommended by PHPCS.

Copy link

github-actions bot commented Apr 11, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Chintesh <chinteshprajapati@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

I'm curious about how you identified the WPCS issues because we're already running PHPCS checks with WPCS on this repo?

Comment on lines 77 to 78
// Retrieve the sanitized value of the 'REQUEST_METHOD' server variable.
$method = filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING );
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should use filter_input() for two reasons:

  1. It's not used in core.
  2. It's not able to be unit tested (e.g. by setting $_SERVER['REQUEST_METHOD']).

You can see there is a unit test failing for the second reason: https://github.com/WordPress/performance/actions/runs/8651242612/job/23721516648?pr=1123#step:9:84

You can see the test setup is overriding $_SERVER['POST_METHOD']:

'post_request' => array(
'set_up' => function () {
$this->go_to( home_url( '/' ) );
$_SERVER['REQUEST_METHOD'] = 'POST';
},
'expected' => false,
),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, I have Updated this

Thanks @westonruter

@@ -30,7 +30,7 @@ function auto_sizes_update_image_attributes( $attr ) {
}

// Don't add 'auto' to the sizes attribute if it already exists.
if ( false !== strpos( $attr['sizes'], 'auto,' ) ) {
if ( str_contains( $attr['sizes'], 'auto,' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

str_contains() is only available as of PHP8. However, since WP 5.9 and later implement a polyfill for this, then this is fine.

$able = ! (
// Since there is no predictability in whether posts in the loop will have featured images assigned or not. If a
// theme template for search results doesn't even show featured images, then this wouldn't be an issue.
is_search() ||
// Since injection of inline-editing controls interfere with breadcrumbs, while also just not necessary in this context.
is_customize_preview() ||
// Since the images detected in the response body of a POST request cannot, by definition, be cached.
'GET' !== $_SERVER['REQUEST_METHOD'] ||
( isset( $method ) && 'GET' !== $method ) ||
Copy link
Member

Choose a reason for hiding this comment

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

There isn't a need for isset() if the $method variable was set above, is there? Here it's just checking if it is null. There's no string offset going on.

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 have Updated this

Thanks @westonruter

@@ -93,7 +93,7 @@ function plsr_get_speculation_rules() {
),
);

// Allow adding a class on any links to prevent prerendering.
// Allow adding a class on any links to prevent pre-rendering.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this change should be made since "prerendering" is the term used. See https://developer.mozilla.org/en-US/docs/Glossary/Prerender

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 have Reverted it

Thanks @westonruter

@westonruter westonruter added the [Type] Enhancement A suggestion for improvement of an existing feature label Apr 11, 2024
@westonruter westonruter added no milestone PRs that do not have a defined milestone for release skip changelog PRs that should not be mentioned in changelogs [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Plugin] Auto Sizes Issues for the Auto Sizes plugin labels Apr 11, 2024
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thank you for the PR @Chintesh!

@felixarntz felixarntz merged commit c2bb4a7 into WordPress:trunk Apr 11, 2024
41 of 43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no milestone PRs that do not have a defined milestone for release [Plugin] Auto Sizes Issues for the Auto Sizes plugin [Plugin] Optimization Detective Issues for the Optimization Detective plugin skip changelog PRs that should not be mentioned in changelogs [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants