Part of meta-issue #2571965: [meta] Fix PHP coding standards in core
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.Strings.UnnecessaryStringConcat"/>
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 |
---|---|---|---|
#13 | 2902412-13.patch | 12.25 KB | andypost |
#13 | interdiff.txt | 6.26 KB | andypost |
#5 | 2902412-5.patch | 10.54 KB | jofitz |
Comments
Comment #2
mfernea CreditAttribution: mfernea at AmeXio commentedHere is a partial patch. I'm not sure how to approach the errors in DbDumpCommand.php and HtmlToTextTest.php.
Comment #3
andriyun CreditAttribution: andriyun at Skilld, Drupal Ukraine Community commentedNeed small reroll for phpcs.xml.dist file
To cover the errors in DbDumpCommand.php and HtmlToTextTest.php files let's use following construction
Comment #4
jofitz CreditAttribution: jofitz at ComputerMinds commentedSimple re-roll before addressing @andriyun's suggestion in #3.
Comment #5
jofitz CreditAttribution: jofitz at ComputerMinds commentedResolve coding standards errors in DbDumpCommand.php and HtmlToTextTest.php.
Comment #6
andriyun CreditAttribution: andriyun at Skilld, Drupal Ukraine Community commentedLGTM
Comment #7
andriyun CreditAttribution: andriyun at Skilld, Drupal Ukraine Community commentedComment #8
mfernea CreditAttribution: mfernea at AmeXio commentedGood idea @andriyun! Looks good to me too. Thanks Jo! :)
Comment #9
xjmHmm, I'm not sure about using
implode('',...)
with a static list of strings. It adds a function call to get around a coding standard. We don't do that anywhere else in core; all the others are with a variable:It looks like it's just two places that this is being done for readability, and one is a fixture for a test, whereas the other one is generating a command for a fixture for a test.
I think
HtmlToTextTest
can probably just inline them. Less sure about the best change for the DB dump command one since it also includes actual linebreaks; maybe it could use EOF format or whatnot. (Do we have any coding standards that prefer not using EOF format/multi-line strings)?Setting NR for further discussion.
Comment #11
borisson_I think the EOF format is a good idea here, I don't know of any coding standard preventing that.
Comment #13
andypostRe-roll & address #9 with http://php.net/manual/en/language.types.string.php#language.types.string...
2 places in
\Drupal\Tests\system\Functional\Mail\HtmlToTextTest
requirestr_replace(["\r", "\n"], '', $input)
because the case ensures that new lines actually added by text processingComment #14
borisson_This looks very good now, great work y'all!
Comment #16
catchCommitted e6a0d58 and pushed to 8.7.x. Thanks!
Comment #19
quietone CreditAttribution: quietone at Acro Commerce commentedIt has taken a while but finally tracked all the unexpected changes to the database dumps to this patch. If an existing dump (currently using one in Commerce Migrate) is imported and then immediately exported the files no longer match and they should.
It probably isn't worth modifying the code that this patch change, unless someone else thinks so, but the migrate dumps need to be rerolled because of this. And I will be doing the same for all the dumps in Commerce Migrate as well.