-
Notifications
You must be signed in to change notification settings - Fork 921
Introduce PHP coding-standards thresholds #15911
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
Bump PHPCS to ~2.0.0 to use the latest and greatest. Introduces code that captures the PHPCS results output to have an opinion on the number of errors and warnings that are expected. Updates the PHPCS configuration by removing exclusions that are marked to be irrelevant for the bumped version of YoastCS. Fixes spaces vs tabs in PHPCS configuration.
Use thresholds on non-pull requests (push, tags) Use check-branch-cs for pull-requests, this will fail on any introduced problems.
This allows Travis to fail on errors.
In line with the phpcs configuration additions.
Mention * Thanks for the inspiration from https://github.com/OXID-eSales/coding_standards_wrapper as the source of inspiration for the code used. The package was not dynamic enough for us to be able to use it practically.
Herre and Irene revisited the issue about thresholds yesterday - squizlabs/PHP_CodeSniffer#2094 and noted that there was a solution being created to extract the numbers from the results of PHPCS. This PR implements a similar approach in a slightly different implementation. We think this should help avoid introducing new warnings and errors for the 2.0.0+ branch of YoastCS; which currently are just too easy to accidentally do. @jrfnl I'm sure this change warrants some more configuration for PHPCS. Could you take a look? |
@herregroen / @jrfnl could you think along with the stated problem in the description of the issue? |
We could do a threshold check on PRs, it takes around 16 seconds on my machine - so probably 60 seconds on Travis.. |
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.
Hi @moorscode, thanks for the ping.
I've left some comments and questions inline.
Would you like me to add some commits updating the ruleset to what it should be for YoastCS 2.x to this PR ? We would need to adjust the threshold numbers after the ruleset update.
I'd also like to suggest I do a clean-up run before this gets merged, so the threshold numbers are as low as can be at this time and don't include low-hanging fruit.
Problem to solve:
We only have branch-checks on Travis, which would mean that we will break trunk if new warnings are introduced.
Enabling trunk builds will require more resources to be used on Travis, but will make this process more reliable.
PHPCS 3.x has the caching feature, if we enable that and store the cache file between builds, running a full CS check on branches/trunk should be faster. Would that help ?
We could do a threshold check on PRs, it takes around 16 seconds on my machine - so probably 60 seconds on Travis..
AFAICS, the threshold check basically adds a second CS run to the build and a full one at that, that seems counter to the intentions of minimizing the time taken ?
If we enable caching and do a full run, including threshold check, is there still a need for a trunk
/branch
toggle ?
On a single run, multiple reports can be generated, so that is not a problem --report=full, summary,source
, however, I can imagine that the branch specific run makes it easier to find the specific issues introduced by a PR as only the issues in the changed file will be shown ?
Could the scripts be adjusted to allow for only outputting the "full" reports for the changed files ?
"cs": [ | ||
"Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards" | ||
], | ||
"check-cs-thresholds": [ | ||
"@putenv YOASTCS_THRESHOLD_ERRORS=625", | ||
"@putenv YOASTCS_THRESHOLD_WARNINGS=546", |
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.
How will these numbers be maintained ?
* | ||
* @return array ['error_count' = (int), 'warning_count' = (int)] Errors and warnings found by PHPCS. | ||
*/ | ||
private static function extract_cs_statistics( $output ) { |
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.
Would it be an idea to add the "Files" and "Sources" into the mix as well ?
PHPCS throws two violation types - errors and warnings, those are reported at the bottom of the summary
report and addressed here. That same line also contains a file count.
Additionally, as you know, those errors and warnings each have an "error code" which is, for instance, used inline to ignore issues.
Using the source
report, you get to see how many errors/warnings there are for each error code with at the bottom a total and a total for the number of error codes.
That last number should not go up as that means that even when the error/warning counts remain the same, one type of error has just been replaced by another.
To get both reports, use --report=summary,source"
(works in PHPCS 3.x, not 2.x)
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 think we should start keeping this as simple as possible. Just having the distinction between errors and warnings will give the developers a place to start. If we don't see the thresholds going down over time, we could try to enforce more fine-grained control over what is allowed, but we prefer to see that ownership come from the people instead of enforced logic.
I think it would also mean that we need to keep a long list of errors per error-code somewhere (composer.json as setup currently) which doesn't make that easier to work with or adjust thresholds when problems get fixed.
Co-authored-by: Juliette <[email protected]>
This has been included in YoastCS since 1.0.0
Added a general exclusion in PHPUnit for the composer config directory.
That would be much appreciated! |
My idea was to only check for thresholds on the Not sure if caching would help in that case, because the branch-changed-files is pretty specific and (mostly) not a big task to run. |
Working on it. As Free would now make a huge jump in one go, I'm cleaning up the commits (which were incremental) in my local branch before adding them here. I need a little time to finish cleaning it all up. Aside from the ruleset, I think we'll also need to fix a number of CS/ignores + add a commit removing the file docblocks from namespaced files, otherwise the threshold numbers will be artificially high when they don't need to be.
As most people don't look at the |
I've changed the Travis file to execute the thresholds for both trunk and PR builds. |
Reviewed & tested, this is awesome. I'm going to go ahead and merge. @jrfnl please do a pull to fix whatever you can already fix as soon as possible, but I think adding this now prevents us from introducing new stuff which is great. |
Context
Problem to solve:
We only have branch-checks on Travis, which would mean that we will break trunk if new warnings are introduced.
Enabling trunk builds will require more resources to be used on Travis, but will make this process more reliable.
Summary
This PR can be summarized in the following changelog entry:
Relevant technical choices:
config-yoastcs
as this is being done automatically by the newer YoastCS versionmaybe-restore-composer.lock
as this is no longer neededthresholds
check to thecomposer cs
menuTest instructions
This PR can be tested by following these steps:
composer install
to install the latest packagescomposer check-cs-thresholds
to verify the current code is at or below the thresholdscheck-branch-cs
for the Pull Request instead of doing a full thresholds checkUI changes
Documentation
Quality assurance
I have added unittests to verify the code works as intended