-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add variable analysis phpcs sniffs #8556
Conversation
Test the buildOption 1. Jetpack Beta
Option 2. Jurassic Ninja - available for logged-in A12s🚀 Launch a JN site with this branch 🚀 ℹ️ Install this Tampermonkey script to get more options. Build info:
Note: the build is updated when a new commit is pushed to this PR. |
Size Change: 0 B Total Size: 1.23 MB ℹ️ View Unchanged
|
This is because templates have the variables defined when they're loaded.
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.
PHPCS was not scanning any files in my local environment until I commented out the following exclude pattern:
<exclude-pattern>./dev/*</exclude-pattern>
After removing this line, I was able to follow the testing instructions provided in the PR. However, even after prefixing the variable name with unused_
, I still encountered the following error:
FILE: /Users/rafael-a8c/dev/a8c/woopay-dev/woocommerce-payments/includes/admin/class-wc-payments-admin-sections-overwrite.php
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
39 | ERROR | Doc comment for parameter "$unused_default_sections" missing (Squiz.Commenting.FunctionComment.MissingParamTag)
42 | ERROR | Doc comment for parameter $default_sections does not match actual variable name $unused_default_sections (Squiz.Commenting.FunctionComment.ParamNameNoMatch)
46 | WARNING | The method parameter $unused_default_sections is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
As an additional point, the VariableAnalysis plugin is being loaded upon executing composer install
:
PHP CodeSniffer Config installed_paths set to ../../phpcompatibility/php-compatibility,../../phpcompatibility/phpcompatibility-paragonie,../../phpcompatibility/phpcompatibility-wp,../../phpcsstandards/phpcsextra,../../phpcsstandards/phpcsutils,../../sirbrillig/phpcs-variable-analysis,../../slevomat/coding-standard,../../woocommerce/woocommerce-sniffs/src,../../wp-coding-standards/wpcs
For reference, here are my Composer and PHP versions:
Composer version 2.7.2, 2024-03-11 17:12:18
PHP version 8.1.27 (/usr/local/Cellar/php@8.1/8.1.27_1/bin/php)
Do you think the issue lies in the PR or in my environment? Do you have any advice?
🤯 I see in the phpcs report that you have your project stored in I'll be very surprised if this is the problem. I wouldn't expect phpcs to look so far up the folder structure 🤔
Hmm, the doc comment errors are expected, but I guess we need to add
I'm honestly not at all sure what this even means. I guess maybe something the |
I've updated the testing instructions to match the most recent changes 👍 |
That was the reason it wasn't working. I can't believe how much time I wasted because of it. 🤦 |
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.
The code looks good, and I was able to follow the instructions.
I can confirm that adding the prefix _unused_
to the variable with the VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
error makes the error disappear.
Co-authored-by: Shendy <73803630+shendy-a8c@users.noreply.github.com>
Part of #8436
Changes proposed in this Pull Request
VariableAnalysis.CodeAnalysis.VariableAnalysis
sniff.VariableAnalysis.CodeAnalysis.VariableAnalysis
sniff that ignores unused variables prefixed with_unused_
.Testing instructions
VariableAnalysis.CodeAnalysis.VariableAnalysis
excludes fromphpcs.xml.dist
.npm run lint:php
and locate a file with an unused variable._unused_
to all the unused variables in that file../vendor/bin/phpcs --standard=phpcs.xml.dist <path/to/file>
.npm run changelog
to add a changelog file, choosepatch
to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.Post merge