Closed Miraeld closed 4 days ago
Thanks @Miraeld ! To avoid breaking theme CI on the repo, could we apply this rule on the diff only for now? I think PHPCS offers this possibility. We would benefit from the rule without having to update the whole codebase for now.
@MathieuLamiot Phpstan doesn't offer this possibility, per se the documentation.
@Miraeld what about this? https://phpstan.org/user-guide/baseline
Coverage variation | Diff coverage |
---|---|
Report missing for eda20949b04ea5756cff630dadd67c64ab81bfd9[^1] | :white_check_mark: ∅ (target: 50.00%) |
Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more [^1]: Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
A baseline
system has been introduced in this PR. A notion page will be created to explain more about it.
Does this work with you locally? It doesn't work with me at all, I tested it like that: Add
apply_filters( 'rocket_test', false )
to the code insideinc/Engine
and run the command:composer run run-stan-reset-baseline
but it shows that everything is OK.
Am I doing anything wrong here?
Also, do we need to handle the function
apply_filters_ref_array
also here?
Yea it's normal you are using the wrong command.
Okay, let me explain quick here, and I'll explain deeper on Slack I think.
Introducing this new PHPStan rule across our codebase results in 557 errors because of how extensively we use apply_filters
. However, the goal of this PR is not to refactor all these instances but to implement the rule itself.
To manage these existing issues without addressing them right now, we're leveraging PHPStan's baseline system.
The baseline acts as a "to-do list" for PHPStan. It records known errors and tells PHPStan to "ignore these for now." This allows us to focus on new issues while keeping the existing ones acknowledged but not flagged during analysis.
The Composer command I added (run-stan-reset-baseline
) lets you update the baseline file with the latest list of known errors. This ensures PHPStan won't report the existing 557 issues during its checks.
This approach allows us to adopt the new rule without overwhelming ourselves with fixing historical issues immediately. Let me know if you need further elaboration!
About apply_filters_ref_array
, I'm not sure, what @CrochetFeve0251 & @remyperona think about it ?
Description
Fixes #7120 Nothing impacts users.
Type of change
Detailed scenario
N/a
Technical description
Documentation
This pull request includes several changes to enhance the PHPStan configuration and introduce a custom rule to discourage the use of
apply_filters
in favor of a typed alternative. The key changes include adding a new script to generate a PHPStan baseline, updating the PHPStan configuration file to include the baseline and new paths, and implementing a custom PHPStan rule.Enhancements to PHPStan configuration:
composer.json
: Added a new scriptrun-stan-reset-baseline
to generate a PHPStan baseline.phpstan.neon.dist
: Included thephpstan-baseline.neon
file and added new paths to thepaths
parameter. Also, setreportUnmatchedIgnoredErrors
to false. [1] [2]Introduction of a custom PHPStan rule:
phpstan.neon.dist
: Added a new custom ruleWP_Rocket\Tests\phpstan\Rules\DiscourageApplyFilters
to therules
parameter.tests/phpstan/Rules/DiscourageApplyFilters.php
: Implemented theDiscourageApplyFilters
class to discourage the use ofapply_filters
and suggest usingwpm_apply_filters_typed
instead.This will trigger an error with PHPStan if we are using
apply_filters
and recommend to usewpm_apply_filters_typed()
. Example:New dependencies
n/a
Risks
n/a
Mandatory Checklist
Code validation
Code style