Part of #2571965: [meta] Fix PHP coding standards in core, stage 1.
Approach
We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. Both of these packages are not installed in Drupal core. We need to do a couple of steps in order to download and configure them so we can run a coding standards check.
Step 1: Add the coding standard to the whitelist
Every coding standard is identified by a "sniff". For example, an imaginary coding standard that would require all llamas to be placed inside a square bracket fence would be called the "Drupal.AnimalControlStructure.BracketedFence sniff". There are dozens of such coding standards, and to make the work easier we have started by only whitelisting the sniffs that pass. For the moment all coding standards that are not yet fixed are simply skipped during the test.
Open the file core/phpcs.xml.dist and add a line for the sniff of this ticket. The sniff name is in the issue title. Make sure your patch will include the addition of this line.
Step 2: Install PHP CodeSniffer and the ruleset from the Coder module
Both of these packages are not installed by default in Drupal core, so we need to download them. This can be done with Composer, from the root folder of your Drupal installation:
$ composer require drupal/coder squizlabs/php_codesniffer
$ ./vendor/bin/phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer
Once you have installed the phpcs package, you can list all the sniffs available to you like this:
$ ./vendor/bin/phpcs --standard=Drupal -e
This will give you a big list of sniffs, and the Drupal-based ones should be present.
Step 3: Prepare the phpcs.xml file
To speed up the testing you should make a copy of the file phpcs.xml.dist (in the core/ folder) and save it as phpcs.xml. This is the configuration file for PHP CodeSniffer.
We only want this phpcs.xml file to specify the sniff we're interested in. So we need to remove all the rule items, and add only our own sniff's rule. Rule items look like this:
<rule ref="Drupal.Classes.UnusedUseStatement"/>
Remove all of them, and add only the sniff from this issue title. This will make sure that our tests run quickly, and are not going to contain any output from unrelated sniffs.
Step 4: Run the test
Now you are ready to run the test! From within the core/ folder, run the following command to launch the test:
$ cd core/
$ ../vendor/bin/phpcs -p
This takes a couple of minutes. The -p flag shows the progress, so you have a bunch of nice dots to look at while it is running.
Step 5: Fix the failures
When the test is complete it will present you a list of all the files that contain violations of your sniff, and the line numbers where the violations occur. You could fix all of these manually, but thankfully phpcbf can fix many of them. You can call phpcbf like this:
$ ../vendor/bin/phpcbf
This will fix the errors in place. You can then make a diff of the changes using git. You can also re-run the test with phpcs and determine if that fixed all of them.
| Comment | File | Size | Author |
|---|---|---|---|
| #45 | drupal-coding-standards-2572699-45.patch | 9.13 KB | mfernea |
Comments
Comment #2
duaelfrAs agreed between the mentors at Drupalcon, according to issues to avoid for novices, I am untagging this issue as "Beginner". This issue contains changes across a very wide range of files and might create too many other patches to need to be rerolled at this particular time. This patch has an automated way to be rerolled later so better to implement it after Drupalcon.
Comment #3
tstoecklerThis does not comply with Drupal's coding standard. The closing brace should be on it's own line and there should be a newline after the opening brace.
Comment #4
marvin_b8 commentedComment #5
pfrenssenComment #6
tstoecklerThe elseif should be fixed as well. I didn't run
phpcslocally, so not sure if that would have been caught or not.In any case we should fix the
phpcs.xml.distfile to remove this sniff from the ignore section.Comment #7
andriyun commentedpatch outdated
Comment #8
andriyun commentedNew patch with removed Drupal.ControlStructures.InlineControlStructure sniff from phpcs.xml.dist file.
Comment #9
andypostlooks that needs follow-up to fix wtf
Comment #10
alexpottWe've got an new one :)
Comment #11
andriyun commentedComment #12
andriyun commentedNew patch.
Thank you Alexpott
RTBC b/c interdiff really small and clear
Comment #13
andriyun commentedComment #14
alexpottI'm not convinced that these changes are an improvement - is there a defined coding standard for this?
Perhaps this is better as...
Comment #15
alexpottSo for point 1...
From our coding standards... so...
The closing
}needs to be on a new line.Comment #17
pfrenssenComment #18
pfrenssenComment #19
andypostthe rule needs tuning or using other sniffers to prevent
PS: reroll
Comment #20
pfrenssenComment #21
alexpottI'm not sure this is a good change. Maybe we should fix the rule and check what the real standards are.
Let's put the close on a new line.
This looks super awkward and is fixed in #842620: Update manager can't install modules using FTP due broken FileTransferAuthorizeForm
Let's format these properly.
As above - I think this change is not a good one.
Let's format these properly.
Comment #22
rajeshwari10 commentedDone changes as per #21.
Thanks!!
Comment #23
alexpottShouldn't be removing a rule here.
Comment #24
rajeshwari10 commentedadding rule.
Comment #26
rajeshwari10 commentedComment #27
pashupathi nath gajawada commentedPlease find the attached patch.
Comment #29
pashupathi nath gajawada commentedPlease find the updated patch #29.
Comment #30
rajeshwari10 commented@pashupathi nath gajawada
Please provide the interdiff.
I have done the changes as per said in #26.
Thanks!!
Comment #31
pashupathi nath gajawada commentedHI @Rajeshwari Variar,
I have done the changes as suggested by #23 suggested by Alex Pott.
Thanks,
Comment #32
mile23Right here:
Comment #33
rajeshwari10 commentedRemoved whitespace errors.
Thanks!!
Comment #34
dawehnerThis patch does NOT fixes all instances of the rule:
Nitpick: Unneeded change
Wow, I did not even know that this was valid syntax :)
Comment #38
mfernea commentedRe-roll & updates.
Comment #39
andriyun commentedThere are new phpcs fails was commited
Please fix them too
Comment #40
andriyun commentedComment #41
mile23We're fixing inline control structures here, not array brackets.
I reviewed the patch in #38 this way:
This results in 0 errors,
up until the process hangs at 96% which I believe is because of my ancient crappy machine. :-)Update: I just didn't let it run long enough. I wonder what file is doing that.However, we also see that the testbot tells us that no errors occurred, and since a changed phpcs.xml.dist file will result in a testbot sniff of the whole codebase, we're safe. (Search the console output for 'PHPCS config file modified, sniffing entire project.')
The patch adds:
So if any errors remained, this would result in error reports within the scope of this issue.
These are kind of weird, and are left over from #21.1 and #21.5. We shouldn't rely on side effects to control the loop. I'll say this patch fixes the CS errors within scope, but we might unwrap those loops for readability either here or in a follow-up.
Comment #42
andriyun commentedAh... yes
Rechecked again
Confirm that no errors after patch applying
Comment #43
alexpott@Mile23 yeah the long wait at 96% bugged me too - see #2911280: RectangleTest.php takes a very long time to scan for coding standards
Comment #44
catchAgreed we need a follow-up for #21.1 and #21.5 - I think we should both open that and add a @todo here so marking CNW for that. It's possibly not in scope for the coding standards change to add the @todo, but it's exposed some very unreadable code.
Comment #45
mfernea commentedI added #2911497: Make \Drupal\Component\Utility\Unicode::truncateBytes more readable and #2911498: Make TestServiceProvider more readable (cleanup).
Here is the updated patch and the interdiff.
Comment #47
mfernea commentedI think the test error relates to this: #2857843: Random fail in Drupal\KernelTests\Core\Entity\ContentEntityChangedTest::testChanged.
Comment #48
andriyun commentedno phpcs fails after patch applying
patch contains only sniff related changes
and include @todo comments for #21.1 and #21.5
with links to proper issues.
Comment #49
catchCommitted/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!
Comment #51
andypostLooks it was not pushed to 8.4.x
Comment #52
andypost@catch please cherry pick to 8.4.x
Comment #53
mfernea commentedI can see 20a43f60d9 commit on 8.4.x, so looks ok, although it doesn't appear here in the issue. But that may be another problem.