Part of meta-issue #2571965: [meta] Fix PHP coding standards in core, stage 1
This issue is to add the sniffs Squiz.Arrays.ArrayDeclaration.NoKeySpecified and Squiz.Arrays.ArrayDeclaration.KeySpecified
The relevant coding standard is Arrays which does not specify the changes being made in the sniffs. In other words, these sniffs are not part of the Drupal coding standards.
Step 1: Preparation
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 & configure PHPCS
Install PHP CodeSniffer and the ruleset from the Coder module:
$ composer install
$ ./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 |
|---|---|---|---|
| #29 | interdiff_21_29.txt | 1.01 KB | sanjayk |
| #29 | 2908266-29.patch | 9.91 KB | sanjayk |
Comments
Comment #2
mfernea commentedWe should wait until #2901739: Fix 'Squiz.Arrays.ArrayDeclaration' coding standard gets committed.
Comment #3
mfernea commentedComment #4
mfernea commentedComment #6
idebr commented#2901739: Fix 'Squiz.Arrays.ArrayDeclaration' coding standard was committed, so this is no longer postponed.
Comment #7
mfernea commentedComment #8
mfernea commentedHere is the patch. I'm not sure what to do about files in /lib/Drupal/Component/Transliteration/data.
Comment #11
andypostReroll for 8.7.x + fixes
-
core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php-
core/tests/Drupal/KernelTests/Core/Menu/LocalActionManagerTest.phpinterdiff & "no-translit" patch is workaround for
/lib/Drupal/Component/Transliteration/dataI think it needs follow-up to discus & fix this tablesComment #13
andypostproper no-translit patch
Comment #14
andypostFiled follow-up #3007415: Fix transliteration data to conform Squiz.Arrays.ArrayDeclaration standard
Now it looks ready
Comment #16
andyposttestbot flux
Comment #20
daffie commentedPatch still applies to 9.1.
The patch fixes a lot of errors, but not all of them.
The remaining error:
Comment #21
munish.kumar commentedComment #22
andypostFixed
Comment #23
andypost@munish.kumar sorry, please help testing the patch
Comment #24
munish.kumar commented@andypost thats alright, you have uploaded it so quickly. Yeah sure will help with the testing.
Comment #25
munish.kumar commentedTested the latest patch, Applied successfully to 9.1.x branch also it fixed the #20 issue. Here is the result.
Comment #26
daffie commented@munish.kumar: Thank you for your review. What you did was great, only for a full review you should also change the status of the issue. There is a easy to follow review process, that explains everything you should do when doing a review. For a guide: https://www.drupal.org/patch/review.
We all like to have our patches reviewed by somebody else. Doing a review of somebody else's is far less popular thing to do. Doing reviews of other people's patches build community credits and also gets rewarded with commit credits! Its usually less work to do a review then to write a patch, so more commit credits for the same amount of time invested. How to get commit credits: https://www.drupal.org/core/maintainers/issue-credit.
By the way: the patch look great and is for me RTBC, but I leave it to @munish.kumar to change to status.
Comment #27
munish.kumar commented@daffie, Thanks for your valuable feedback. Yeah, I have reviewed the patch and it works as expected. So moving to RTBC.
Comment #28
alexpottSo are we sure that we want to adopt this as a standard? It's not part of https://www.drupal.org/docs/develop/standards/coding-standards#array and it's not very pretty. I'm not convinced this is a good coding standard. Yes it is part of Coder's ruleset but that does not 100% implement https://www.drupal.org/docs/develop/standards/coding-standards - it has more opinions.
There are the only runtime fixes apart from the ones excluded in transliteration data - and I'm really really not sure that doing #3007415: Fix transliteration data to conform Squiz.Arrays.ArrayDeclaration standard is worth it. It's not like you look to core/lib/Drupal/Component/Transliteration/data/x87.php for how to write core code.
The change to
template_preprocess_language_negotiation_configure_form()doesn't really look correct. This is a render array. So we can give the thing a meaning full key. Like:Or as $form[$type]['title'][$id] is a render array we can even do:
Or we could even do
And then use CSS to style this as the choice of bold is a visual thing to make the titles stand out from the description and this feels like something that should be controlled by the theme.
Comment #29
sanjayk commentedFixed changes as suggested #28.
Comment #30
sanjayk commentedForgot to unassigned, kindly review #29 patch.
Comment #32
quietone commentedBefore work continues here the question in #28 needs to be addressed.
Comment #33
longwaveI think I agree with #28, I am not sure I see the point of this standard in core. To me it makes code less readable in the places touched by this patch, rather than more readable which is what coding standards should aim for.
Comment #35
quietone commentedI can confirm that the changes made by these sniffs are not part of the coding standards. I searched and could not find an issue in the Coding Standards project about these either.
What is the next step here? I suppose a) create an issue in Coding Standards and postpone this on that or b) close this as won't fix.
My two cents is that I agree with #28 and #33 that this does not add value.
Comment #36
daffie commentedWe have a core committer and 3 long term contributers (including myself) who think this is something we should not do. Therefor marking this issue as "closed won't fix".