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

Update phpcs and related sniffs #8415

Merged
merged 31 commits into from
Mar 22, 2024

Conversation

reykjalin
Copy link
Contributor

Changes proposed in this Pull Request

  • Update the WooCommerce and Slevomat PHPCS sniffs so we can update PHPCS itself.
  • Update PHPCS so it can be run via PHP 8.1.

Testing instructions

Note

There are currently way more issues reported after the update. Not sure which ones are false positives, which can be silenced, and which ones we should fix.

  1. Run npm run lint:php in develop and make a note of the issues reported (there should be none).
    • If you're already running PHP v8.1 you may have to run this using something like /opt/homebrew/opt/[email protected]/bin/php ./vendor/bin/phpcs --standard=phpcs.xml.dist $(git ls-files | grep .php$)
  2. Checkout this branch and make sure you run composer install.
  3. Run npm run lint:php and make sure you see the same issues reported as in develop.

  • Run npm run changelog to add a changelog file, choose patch 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.
  • Covered with tests (or have a good reason not to test in description ☝️)
  • Tested on mobile (or does not apply)

Post merge

@reykjalin reykjalin requested review from a team and ricardo and removed request for a team March 18, 2024 19:22
@botwoo
Copy link
Collaborator

botwoo commented Mar 18, 2024

Test the build

Option 1. Jetpack Beta

  • Install and activate Jetpack Beta.
  • Use this build by searching for PR number 8415 or branch name tweak/update-phpcs-composer-dependencies in your-test.site/wp-admin/admin.php?page=jetpack-beta&plugin=woocommerce-payments

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:

  • Latest commit: 13857bd
  • Build time: 2024-03-22 18:27:22 UTC

Note: the build is updated when a new commit is pushed to this PR.

Copy link
Contributor

github-actions bot commented Mar 18, 2024

Size Change: +6 B (0%)

Total Size: 1.2 MB

Filename Size Change
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/i18n-loader.js 2.44 kB +6 B (0%)
ℹ️ View Unchanged
Filename Size
release/woocommerce-payments/assets/css/admin.css 1.06 kB
release/woocommerce-payments/assets/css/admin.rtl.css 1.06 kB
release/woocommerce-payments/assets/css/success.css 158 B
release/woocommerce-payments/assets/css/success.rtl.css 158 B
release/woocommerce-payments/dist/blocks-checkout-rtl.css 1.92 kB
release/woocommerce-payments/dist/blocks-checkout.css 1.91 kB
release/woocommerce-payments/dist/blocks-checkout.js 54.1 kB
release/woocommerce-payments/dist/cart.js 4.45 kB
release/woocommerce-payments/dist/checkout-rtl.css 405 B
release/woocommerce-payments/dist/checkout.css 406 B
release/woocommerce-payments/dist/checkout.js 37 kB
release/woocommerce-payments/dist/index-rtl.css 40.1 kB
release/woocommerce-payments/dist/index.css 40 kB
release/woocommerce-payments/dist/index.js 290 kB
release/woocommerce-payments/dist/multi-currency-analytics.js 1.05 kB
release/woocommerce-payments/dist/multi-currency-rtl.css 3.28 kB
release/woocommerce-payments/dist/multi-currency-switcher-block.js 59.4 kB
release/woocommerce-payments/dist/multi-currency.css 3.29 kB
release/woocommerce-payments/dist/multi-currency.js 54.5 kB
release/woocommerce-payments/dist/order-rtl.css 733 B
release/woocommerce-payments/dist/order.css 735 B
release/woocommerce-payments/dist/order.js 41.7 kB
release/woocommerce-payments/dist/payment-gateways-rtl.css 1.21 kB
release/woocommerce-payments/dist/payment-gateways.css 1.21 kB
release/woocommerce-payments/dist/payment-gateways.js 38.5 kB
release/woocommerce-payments/dist/payment-request-rtl.css 155 B
release/woocommerce-payments/dist/payment-request.css 155 B
release/woocommerce-payments/dist/payment-request.js 12.4 kB
release/woocommerce-payments/dist/product-details-rtl.css 155 B
release/woocommerce-payments/dist/product-details.css 155 B
release/woocommerce-payments/dist/product-details.js 16.6 kB
release/woocommerce-payments/dist/settings-rtl.css 11 kB
release/woocommerce-payments/dist/settings.css 10.8 kB
release/woocommerce-payments/dist/settings.js 199 kB
release/woocommerce-payments/dist/subscription-edit-page.js 669 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal-rtl.css 527 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.css 527 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.js 19.4 kB
release/woocommerce-payments/dist/subscription-product-onboarding-toast.js 710 B
release/woocommerce-payments/dist/subscriptions-empty-state-rtl.css 120 B
release/woocommerce-payments/dist/subscriptions-empty-state.css 120 B
release/woocommerce-payments/dist/subscriptions-empty-state.js 18.5 kB
release/woocommerce-payments/dist/tos-rtl.css 235 B
release/woocommerce-payments/dist/tos.css 236 B
release/woocommerce-payments/dist/tos.js 21 kB
release/woocommerce-payments/dist/woopay-direct-checkout.js 4.58 kB
release/woocommerce-payments/dist/woopay-express-button-rtl.css 155 B
release/woocommerce-payments/dist/woopay-express-button.css 155 B
release/woocommerce-payments/dist/woopay-express-button.js 21.1 kB
release/woocommerce-payments/dist/woopay-rtl.css 4.44 kB
release/woocommerce-payments/dist/woopay.css 4.41 kB
release/woocommerce-payments/dist/woopay.js 71 kB
release/woocommerce-payments/includes/subscriptions/assets/css/plugin-page.css 622 B
release/woocommerce-payments/includes/subscriptions/assets/js/plugin-page.js 812 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/i18n-loader.js 1.01 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-ajax.js 522 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-callables.js 581 B
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/babel.config.js 160 B
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.css 2.37 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.js 13.5 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.rtl.css 2.37 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/about.css 1.03 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-empty-state.css 291 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-order-statuses.css 403 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin.css 3.6 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/checkout.css 299 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/modal.css 742 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/view-subscription.css 572 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/wcs-upgrade.css 411 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin-pointers.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin.js 9.4 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.js 6.8 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.min.js 3.83 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-coupon.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-subscription.js 2.52 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.js 22.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.min.js 11.6 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/payment-method-restrictions.js 1.29 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/wcs-meta-boxes-order.js 502 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/payment-methods.js 355 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/single-product.js 429 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/view-subscription.js 1.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/wcs-cart.js 781 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/modal.js 1.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/wcs-upgrade.js 1.27 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.css 392 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.js 3.05 kB

compressed-size-action

@reykjalin
Copy link
Contributor Author

reykjalin commented Mar 18, 2024

These are the rules that have issues reported after updating the sniffs:

Rule Fixed Ignored Commit Comment
"Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected" x 3671304 Should've been caught before
"Generic.CodeAnalysis.UnusedFunctionParameter.Found" #8436
"Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed" #8436
"Generic.WhiteSpace.IncrementDecrementSpacing.SpaceAfterIncrement" x 3c95b05 Should've been caught before
"Generic.WhiteSpace.SpreadOperatorSpacingAfter.TooMuchSpace" x 5d16a92 Should've been caught before
"Modernize.FunctionCalls.Dirname.FileConstant" x ec86e08 Should've been caught before
"PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon" x 5b65096 and b580e91 Should've been caught before
"PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeReturnType" x 5b65096 Should've been caught before
"PSR12.Traits.UseDeclaration.MultipleImport" x 47890f3 Should've been caught before
"PSR2.Classes.ClassDeclaration.CloseBraceAfterBody" x ea4e19f Should've been caught before
"PSR2.Files.EndFileNewline.TooMany" x 0c288f5 and 985d059 Should've been caught before
"PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose" x 5d520f3 Should've been caught before
"Squiz.Functions.MultiLineFunctionDeclaration.ContentAfterBrace" x b9170fe Should've been caught before
"Squiz.Functions.MultiLineFunctionDeclaration.Indent" x 6343265 Should've been caught before
"Squiz.Functions.MultiLineFunctionDeclaration.OneParamPerLine" x 8ac694e Should've been caught before
"Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction" x da7ed25 Consistent with current code style
"Universal.Arrays.DisallowShortArraySyntax.Found" x a4a2d40 We were already ignoring this rule, but it was changed from Generic.Arrays.… to Universal.Arrays.…
"Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound" x 1e7850a Constructors can't return values
"Universal.CodeAnalysis.NoEchoSprintf.Found" x 393ba5e Should've been caught before
"Universal.ControlStructures.DisallowLonelyIf.Found" x 62ba856 Should've been caught before
"Universal.Files.SeparateFunctionsFromOO.Mixed" x 7f9455e The issue was only in test files, which I think is warranted in the cases reported. Ignored this rule for test files.
"Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.boolFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.classFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.elseFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.functionFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.matchFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.returnFound" #8438
"Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound" #8438
"Universal.Operators.DisallowStandalonePostIncrementDecrement.PostIncrementFound" x b079925 Seems logical to follow common styles here
"Universal.UseStatements.NoLeadingBackslash.LeadingBackslashFound" x 151c73a Should've been caught before
"WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle" #8437
"WooCommerce.Commenting.CommentHooks.MissingHookComment" #8437
"WooCommerce.Commenting.CommentHooks.MissingSinceComment" #8437
"WordPress.Security.EscapeOutput.ExceptionNotEscaped" #8434
"WordPress.Security.EscapeOutput.OutputNotEscaped" #8434
"WordPress.Security.ValidatedSanitizedInput.InputNotValidated" #8434
"WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents" #8435
"WordPress.WP.AlternativeFunctions.file_system_operations_touch" #8435
"WordPress.WP.AlternativeFunctions.unlink_unlink" #8435
"WordPress.WP.Capabilities.Unknown" x 60140b0 Added custom capabilities to phpcs.xml.dist

And some leftovers fixed in b6301d6.

So those are all rules we have to make a decision on.

I found these by writing the phpcs report to json and using jq to find the source of the issues reported:

./vendor/bin/phpcs --standard=phpcs.xml.dist (git ls-files | grep '.php$') --report=json > issues.json
vi issues.json # And remove the empty lines and PHPCS report
cat issues.json | jq '.. |."source"? | select(. != null )' | sort | uniq

@reykjalin
Copy link
Contributor Author

reykjalin commented Mar 21, 2024

I've been working through the list but it's taking a bit of time. I've settled on trying to use phpcbf as much as possible in a process that looks like this:

  1. Pick a rule, e.g. PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon.
  2. Run PHPCS for that class of rules:
./vendor/bin/phpcs --standard=phpcs.xml.dist $(git ls-files | grep '.php$') --sniffs='PSR12.Functions.ReturnTypeDeclaration'
  1. If it makes sense to fix them, and auto-fix is available, run phpcbf to auto-fix:
./vendor/bin/phpcbf --standard=phpcs.xml.dist $(git ls-files | grep '.php$') --sniffs='PSR12.Functions.ReturnTypeDeclaration'
  1. Commit with a commit message that explains the change and update my previous comment.

@bborman22 this is taking longer than it's expected, but I think it's important that we get this done because:

  1. PHPCS crashes under PHP 8.1 without these updates, and general PHP 8.1 usage is imminent due to the WPCOM migration.
  2. There are some legitimate style inconsistencies caught here.
  3. There are some (potentially) legitimate issues caught here, such as (maybe?) unescaped output, confusing function parameter names ($object, $array, etc.), and more.

The longer this drags the harder it will be to merge due to the amount of files this touches. The things we can't auto-fix via phpcbf we could potentially save for a different PR or add ignores for since they won't be reported unless someone is changing the files containing those issues.

Note that all of these issues appeared after I updated the sniffs and PHPCS, they were not present before.


@ricardo I've already seen some things that will probably need to discuss:

  1. I think the Universal.NamingConventions.NoReservedKeywordParameterNames.* reports point to a legitimate issue, but I suspect these are all part of filters. Maybe we can just ignore this?
  2. Same for Generic.CodeAnalysis.UnusedFunctionParameter.*. Maybe we can ignore this?

Probably some others similar to this, or maybe ones we have to fix, but this is as far as I've gotten today.

@reykjalin reykjalin marked this pull request as ready for review March 21, 2024 20:11
@asumaran asumaran self-requested a review March 22, 2024 15:30
Copy link
Contributor

@asumaran asumaran left a comment

Choose a reason for hiding this comment

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

PHP lint works well with PHP 8.1 now. While I see a lot of warnings and errors, I think it's fine to merge and address the rest separately.

@reykjalin
Copy link
Contributor Author

Awesome! I just merged from develop and fixed the conflict (which introduced no new linting issues that can be auto-fixed AFAICT after running ./vendor/bin/phpcs --standard=phpcs.xml.dist $(git ls-files | grep '.php$') | grep '\[x\]').

I'll wait for tests to pass and add a changelog entry before I merge.

@reykjalin reykjalin merged commit bc8ae14 into develop Mar 22, 2024
20 of 22 checks passed
@reykjalin reykjalin deleted the tweak/update-phpcs-composer-dependencies branch March 22, 2024 18:35
jessy-p added a commit that referenced this pull request Mar 25, 2024
@reykjalin reykjalin mentioned this pull request Mar 25, 2024
6 tasks
Jinksi pushed a commit that referenced this pull request Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants