Problem/Motivation
This is the first step that is needed for #2571965: [meta] Fix PHP coding standards in core, stage 1. If we are going to fix coding standards in core, we also need to ensure that these are tested so that the coding standards will not regress in the future.
Proposed resolution
Currently most coding standards are violated in core. We can add a phpcs.xml that will initially exclude all coding standard rules so that we can start testing them on DrupalCI without causing failures. Then, whenever we fix one violation, the patch fixing it should remove the rule from the blacklist in the the phpcs.xml so that the rule can be tested automatically from that point onwards.
Docs are at https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
Remaining tasks
Review and commit.
API changes
None, this is just a configuration file that does not affect the functionality of core.
| Comment | File | Size | Author |
|---|---|---|---|
| #35 | result-bad.txt | 48.54 KB | pfrenssen |
| #35 | result-good.txt | 2.25 KB | pfrenssen |
| #32 | 2573377-32.patch | 4.5 KB | pfrenssen |
| #32 | phpcs-generate.php_.txt | 8.18 KB | pfrenssen |
Comments
Comment #2
attiks commentedComment #3
attiks commentedPatch adding phpcs.xml, excluding all rules for now.
For the moment it assumes coder is installed globally
Comment #4
pfrenssenThis includes generic and Squiz sniffs, are these part of the sniffs used by Coder Sniffer?
Comment #5
pfrenssenRegarding #4: yes these are part of the official Drupal coding standard ruleset, I verified with @klausi.
Comment #6
pfrenssenComment #7
pfrenssenI would rename the file to
phpcs.xml.dist. This is a convention indicating to developers that it is OK to copy and rename this file tophpcs.xmlif they need to make any modifications for their project's needs.This pattern is also used for the
phpunit.xml.distfile.Add some documentation explaining the purpose of this file. It would be great if it mentions that this file is intended for automatic coding standards checks on the DrupalCI testbot. Also mention that this is a blacklist to temporarily disable checks while the work is still in progress.
Maybe we can put the file in the
core/folder? This is also where thephpunit.xml.distfile lives. We also don't need to scan anything outside the core folder, so it makes sense to put it there.Instead of referring to the home folder, just assume that the ruleset is installed globally (ie. <rule ref="Drupal">).
When this will be implemented on DrupalCI we can either symlink the Drupal ruleset inside the PHP CodeSniffer installation, or copy the XML file and replace the path.
The same for the "Squiz" rule, this can also be considered to be installed globally.
The list of sniffs is not complete, but I guess this list only includes what is being actively violated? I will run the full test now and report back
Comment #8
pfrenssenI did a full scan with the supplied ruleset, but had many violations. I didn't count them but there were tens of thousands :)
It seems we are missing a whole bunch of sniffs in the blacklist. For example
Squiz.Scope.MethodScope:FILE: ...e/tests/Drupal/Tests/Core/Asset/CssCollectionGrouperUnitTest.php ---------------------------------------------------------------------- FOUND 1 ERROR AFFECTING 1 LINE ---------------------------------------------------------------------- 37 | ERROR | Visibility must be declared on method "testGrouper" | | (Squiz.Scope.MethodScope.Missing) ----------------------------------------------------------------------Comment #9
pfrenssenGoing to work on a script to generate this file automatically.
Comment #10
pfrenssenHere's a script. Here's how to use it:
First, install the latest dev version of the coder module, and install its dependencies (this includes PHP CodeSniffer):
Secondly, do a full scan of the core folder with the Drupal standard and save the result in a JSON file. Note that this can take around 15 minutes.
Now, finally, run the script and feed it the JSON file:
I had a problem generating the JSON report, there is a bug in PHP CodeSniffer that causes the JSON to be invalid if any of the errors or warnings contain a newline or tab character. I've attached a patch that works around this in PHPCS, and created a pull request: Issue #716: Prevent generating invalid json reports when messages contain newlines or tabs.
Comment #11
attiks commented#10 @pfrenssen can you upload a patch for the new phpcs.xml.dist file?
Comment #12
pfrenssenHmm I'm still getting an invalid JSON file. The file is 6MB large which makes it a bit difficult to find where it goes wrong :-/
Comment #13
pfrenssenReworked the script to use XML instead. This works a lot better. I do have to edit the XML file to remove some invalid characters such as EOL characters which end up in the XML file.
If you install PHP CodeSniffer and the ruleset using the instructions from #10, you can then generate the coding standards report with:
You'll need to edit the XML to remove a couple of illegal characters, then finally generate the phpcs.xml file:
Comment #14
pfrenssenHere's the
phpcs.xmlfile as generated by the script against current HEAD. Will now run a full coding standards check using this ruleset. If it returns green I'll roll up a proper patch.Comment #15
pfrenssenRunning the full scan with the generated
phpcs.xmlfile I get the following result:For some reason the "Internal" sniffs are still being checked, even though they are excluded in the
phpcs.xmlfile. This is probably a bug in PHP CodeSniffer.All of these failures are very specific, they deal with minified files, or PHP files that actually do not contain PHP. We can work around them by excluding these files.
Comment #16
attiks commented#10 To link the standards I had to use
ln -s ../../../../drupal/coder/coder_sniffer/Drupal .Comment #17
attiks commented#14 The excludes are missing
Comment #18
attiks commented#13 Updated the script to exclude vendor folders
Comment #19
attiks commentedI created an upstream issue for the compressed files: https://github.com/squizlabs/PHP_CodeSniffer/issues/720
Comment #20
attiks commentedI got feedback upstream, the exclude should work for the
Internal.Tokenizer.Exceptionand it does, I created a small site with 1 file and excluded it without a problem, so something else might be wrong.Comment #21
attiks commentedThe internal is fixed, the order is very important, extensions has to come before the excludes
Comment #22
attiks commentedUpdated script
Comment #23
pfrenssenGood catch with the
comment_hacks.cssfile. Updated script to include some helpful comments, so it's clear why we are excluding these files.Comment #24
pfrenssenThis is how the generated file looks right now. The newlines I added manually, I didn't figure out how to get DOMDocument to generate newlines.
I will now run a full coding standards test against the latest HEAD, then regenerate the file, then do a full test with the generated file to see if we now catch all violations.
Comment #25
attiks commented#24 Tested as follows
And did not report any issues.
Comment #26
xjmI'm very +1 on this change and the approach taken to begin introducing coding standards checking. It will fit well with the current plan for coding standards fixes Drupal 8.
I think we probably should have a framework manager review too, so tagging for that.
Also bumping to major, because coding standards compliance is a huge pain point for core developers.
Comment #27
alexpottI think we should not do js - that is covered by eslint. And i ponder about css too
Comment #28
dawehneris there a reason to not mention yml files? Theoretically they could have code styles as well at some point?
MHH, so this file is relative to /core/... even the file is already in /core, this sounds wrongish
It feels like at this point we might want to opt in first and then at some point swap it around?
Comment #29
attiks commented#27 Agree on js, for css there are custom Drupal tests written, not sure if it can easily be replaced by csslint
#28
1. For the moment there are no tests for yml, we can always add it later
2. Yes, the phpcs.xml.dist file is part of core, but has t be run in the root folder, but we can move it, if needed
Comment #30
xjmRe: #28 point 3, no, we definitely want to blacklist first and then opt in one at a time as core is compliant with each rule so that we can add bot support to ensure it doesn't regress.
Comment #31
pfrenssenRe #28.2 I'll have a look if we can treat the core/ folder as the root.
I managed to overwrite my report.xml file, regenerating it and running a new test will take about 30 minutes.
Comment #32
pfrenssenUpdated script. Addressed all remarks, except adding support for YML files, this is not currently covered by PHP CodeSniffer. I also like the suggestion of @alexpott to limit it only to what it does best: PHP files. We can use other tools such as JSLint to scan the other files. I bet there are good CSS scanners too.
Changes:
I did a full scan using this ruleset on the DrupalCI test bot and it returned 0 failures!
I will rerun this test and capture the output. I'll also run a test with one of the rules removed to check if the bot correctly fails the test if any violations are detected.
Comment #33
pfrenssenComment #34
attiks commentedFYI: css will be handled by #1190252: [573] Use csslint as a weapon to beat the crappy CSS out of Drupal core
Comment #35
pfrenssenHere are the test results from the DrupalCI test bot with the patch from #1266444: DrupalCI should return failed for poor code style - php applied. The relevant part can be seen right at the end of the output.
result-good.txt: test run with thephpcs.xml.distfile from #32. The command does not return with an error status. No coding standards violations found.result-bad.txt: test run with thephpcs.xml.distfile from #32 with theDrupal.Files.EndFileNewlineline removed from the blacklist. The command lists the found violations and correctly returns an error status.Comment #36
dawehnerFor some reason I thought we already fail with this issue, but yeah the idea is to track the overall status. In this case blacklisting makes sense.
Comment #37
attiks commentedAll points are addressed for now, so back to RTBC
FYI: I created issues to add css and js checks to the bot as well
- #2575497: DrupalCI should report code style issues - CSS
- #2575501: [PP-2] DrupalCI should return failed for poor code style - JS
Comment #39
attiks commentedSeems unrelated
Comment #40
attiks commentedBot seems happy again
Comment #41
attiks commentedComment #42
alexpottThis is a fantastic idea. Let's do it...
Committed 65c2f39 and pushed to 8.0.x. Thanks!
Comment #44
alexpott