Part of #2571965: [meta] Fix PHP coding standards in core.
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: Run the test
Now you are ready to run the test! From within the core/
folder, run the following command to launch the test:
$ composer run 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 3: 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:
$ composer run phpcbf
This will fix the errors in place or composer will tell you that Script phpcbf --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer -- handling the phpcbf event returned with error code 1
meaning there was no files fixed and you need to fix them manually. 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 |
---|
Issue fork drupal-2937882
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 2937882-fix-drupal.classes.propertydeclaration-coding-ignore-is-the-way-to-go-for-realz-now changes, plain diff MR !576
- 2937882-fix-drupal.classes.propertydeclaration-coding-ignore-is-the-way-to-go changes, plain diff MR !527
- 2937882-fix-drupal.classes.propertydeclaration-coding-for-realz changes, plain diff MR !522
- 2937882-fix-drupal.classes.propertydeclaration-coding changes, plain diff MR !510
Comments
Comment #2
RoSk0Fixed.
Comment #4
Ivan Berezhnov CreditAttribution: Ivan Berezhnov as a volunteer and at Drupal Ukraine Community for Levi9 commentedComment #10
SpokjeComment #12
SpokjeAlthough the above TestBot errors are easily fixed, I think we should not turn this rule on.
Most important reason:
Both
\Drupal\views\ResultRow->_entity
and\Drupal\views\ResultRow->_relationship_entities
are widely used in Contrib (See here and here).Changing the name of them would lead to massive breakage.
Other reasons:
This sniff is also covered by sniff
Drupal.NamingConventions.ValidVariableName.LowerCamelName
.Comment #13
quietone CreditAttribution: quietone as a volunteer commentedThere are two sniff that will do the same task? I am new to this but that seems odd.
Comment #14
SpokjeAFAIK:
Drupal.Classes.PropertyDeclaration
only checks properties andDrupal.NamingConventions.ValidVariableName.LowerCamelName
checks all variables for lower CamelCasing.In the case above both properties start with an underscore, which makes them fail both sniffs.
Comment #15
longwaveWould it be possible to enable this sniff but skip it for the files we know we can't change for BC reasons (assuming it only triggers on the declaration, and not each time the property is used)? That way we at least can be sure we won't be introducing any new ones to core, even if we can't fix the existing ones right now.
Comment #16
Spokje@longwave in #15
Currently the sniff comes back with this:
So, as far as I can see, it only triggers on the declaration.
Gonna run with the suggestion from @longwave:
Comment #17
longwaveIf we do go down this route, unsure whether we should skip with the config file or start using
phpcs:disable
comments in the relevant files. We don't use the comment method for PHPCS yet, but we do have some precedent in that we control cspell with code comments in a few places.Comment #18
Spokje#@longwave:
Pro
phpcs:disable
:- It's clear in the code that it won't be checked.
Con
phpcs:disable
:- No precedence, but as @longwave said: It has already be done by cspell (which may or may not have a config file in which to exclude files)
- This would mean another change in sniff
Drupal.Commenting.InlineComment.InvalidEndChar
to ignore//phpcs:disable
. (See similiar issue for cspell: #3207576: Problem with Drupal.Commenting.InlineComment.InvalidEndChar: //cspel)In #3105950: Fix 'Drupal.Commenting.ClassComment.Missing' coding standard I'm about to introduce excluding files using
core/phpcs.xml.dist
. (Like this)Since we already have used
core/phpcs.xml.dist
to exclude files.I think this is the time to make a decision which way yo go.
Personally: I'm a fan of the config-file excluding style.
Comment #19
longwaveI forgot that we do use
@codingStandardsIgnoreFile
already in a number of places so we do have precedence (and opened #3207968: Replace @codingStandards comments with phpcs: comments to modernise that)Comment #20
Spokje@longwave
Excellent, also we don't have to exclude the full file but some specific lines (https://stackoverflow.com/a/59073074). Let's run with that.
Comment #23
SpokjeComment #24
SpokjeComment #25
longwaveI am not sure we can safely change any of these property names here.
ConfigEntityBase $_core is used for the
_core
property in exported config entities so I don't think we can easily change that.$_serviceIds and $_entityStorages are special properties relating to serialization and would need a BC layer for existing serialized objects, we also need to ensure they don't accidentally clash with any existing property name so the underscore helps prevent that.
Comment #26
SpokjeLooking at @longwave's comment in #25 I think it's better to ignore all three of them.
That way this sniff can go in, and no more of these underscored properties go in.
We could open a follow-up for a BC layer for existing serialized objects for
$_serviceIds
and$_entityStorages
, but that might be too much effort for little result?Comment #29
longwavePersonal opinion is it's not worth changing the serialization properties, they are a really special case and so I think they are OK to stay with their rule-breaking names.
Comment #30
SpokjeI'm with #TeamLongwave on this one.
Comment #31
SpokjeComment #32
longwaveI think this is the best we can do here, and it will prevent us adding similar properties in the future without making a good case for doing so first.
Comment #33
quietone CreditAttribution: quietone as a volunteer commentedThis looks good. My only concern is how to prevent someone in the future wanting to remove the ignore lines. Kind of feels like some of the ignores need to have an @see to a doc page with some explanation and history. Not sure though. Is it worth adding an 'exceptions page' to Coding standards?
Comment #34
SpokjeI like the idea of an explanation/
@see
for these cases. Having them right beside the ignore bit makes sure people actually see them.However (untested at the moment) I fear that adding such a line will trigger some of the other sniffs and a Coder/sniff change is needed.
If I have time later today I will give that a test-test-run (See what I did there...), if anybody beats me to it: Fair play :)
Comment #35
SpokjeAdded explanatory comment to each phpcs:ignore
Let's see what TestBot thinks.
If it's green: Let's see what you think => Back to NR.
Comment #36
SpokjeAlso something to consider:
If we like these comments about _why_
phpcs:ignore
was needed in the first place, should we create a follow-up to make these comments on thephpcs:ignore
that are already in the code.Also, if we decide to do so, we need to make sure all the sniffs currently in backlog get them.
Comment #37
quietone CreditAttribution: quietone as a volunteer commentedI applied the MR and looked the changes in PhpSTorm for ConfigEntityBase and ResultRow and found the extra comments informative but it made the code ugly to read. I think we go back to the version that was RTBC in #32. Thanks for making the changes Spokje, I really needed to see it. So, shall we make a single issue follow up for deprecating these class properties. That leaves the code easier on the eye and the details of the properties are not lost.
Comment #38
SpokjeThanks @quietone.
I would like more opinions about the comments on
phpcs:ignore
, I think they're useful and they don't strike me as ugly, but that's in the eye of the beholder I guess.That's a lot of work to get a BC layer for those in place (See @longwave in #25).
(Again) personally: I don't see that follow-up being worked on in the foreseeable future, since I don't think it will get a lot of priority.
Comment #39
quietone CreditAttribution: quietone as a volunteer commentedCompletely agree with getting more eyes on this!
Regarding the followup, I have voiced my opinion, more than once, and accept the wisdom of the group. No problem there from me. Thanks for listening.
Comment #40
Spokje@quietone: More than happy to listen to you, most of the time it makes sense :)
Since my wisdom has long left, I'm also more than happy to listen to The Wiser Ones on this one.
Comment #41
longwaveI prefer the version in #32 as well.
I don't think we explicitly need to say why we are ignoring coding standards in the code here. If someone wants to know the reasoning, they can look at git history for the line in question which will link them back to this issue and they can read the discussion here. I find myself doing this for all sorts of core issues where I want to know why a specific line was added.
We add comments to help other developers; I think coding standards comments are a niche case where most developers won't care why the property is named what it is, they are much more interested in the docblock above which tells them what the property is actually used for.
Comment #42
longwaveAlso as for followups: there is no harm in opening them but as I said in #29 the first three are super-special cases that I think can continue to break the rules. ResultRow is inherited from Views and the naming isn't for any particular reason so we have more chance of fixing this one, but I'm still not sure it's worth the effort.
Comment #45
SpokjeThanks @longwave and @quiteone for the input.
Seeing that I'm outnumbered, I can only do what any self-respecting person would do: Surrender! ;)
New MR should contain only
phpcs":ignore
s and the actual adding of the sniff.Comment #46
longwaveBack to RTBC as per #32, I assume @quietone agrees.
Comment #47
quietone CreditAttribution: quietone as a volunteer commentedYes, I agree!
Comment #48
SpokjeAnd there was much rejoicing!
Comment #50
catchCommitted/pushed to 9.2.x, thanks!