Problem/Motivation
Examples for Developers needs a better process for fixing and enforcing coding standards rules.
Proposed resolution
We can follow the example of Drupal 8 core: #2571965: [meta] Fix PHP coding standards in core, stage 1
- Add a phpcs.xml.dist file to the root of examples.
- Fix coding standards violations per rule rather than piecemeal.
- Add rules to the phpcs.xml.dist file as we progress.
- Reviewers can then use phpcs during patch review.
How To:
Install the tools
You'll need tools. Specifically, drupal/coder, which in turn requires squizlabs/php_codesniffer. These packages are dev requirements of Drupal 8 core now:
$ cd path/to/drupal/
$ composer install
Now you have to configure phpcs to use coder:
$ ./vendor/bin/phpcs --config-set installed_paths vendor/drupal/coder/code_sniffer/
You can now verify that you've installed the Drupal rules properly like this:
$ ./vendor/bin/phpcs -i
The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, Drupal and DrupalPractice
Do The Work
Pick an issue. The issues are all child issues here. Specific issues might have instructions, but the general flow is this:
- Add a Drupal sniff to the ruleset in phpcs.xml.dist.
- Run
phpcsto see what's broken:
$ cd path/to/drupal/root/ $ ./vendor/bin/phpcs -ps --standard=modules/examples/phpcs.xml.dist modules/examples/ - If necessary, use
phpcfbto do some automated fixing. - Review the work that
phpcfbdid, and amend as necessary.
Remember that the goal here is to make patches which are easy to write, and also easy to review. Over time, all standards will be met.
If you see a violation that doesn't have an issue here, add a child issue to this one.
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 2176147_18.patch | 2.19 KB | mile23 |
Comments
Comment #1
mile23Comment #2
mile23Ugh. Early submit button pressing.
block_example, cache_example, some phpunit_example and tour_example coverage.
Comment #3
mile23Comment #4
mile23Committed: http://drupalcode.org/project/examples.git/commitdiff/336d2b1c8f25b9645f...
Next? :-)
Comment #5
mile23cron_example and dbtng_example
Comment #6
mile23Committed: http://drupalcode.org/project/examples.git/commitdiff/8b26a2dcd0786e92bd...
Comment #7
mile23And still going...
Comment #9
mile23The Tyrrany Of The Hanging Paren.
Comment #11
mile23...
Comment #12
mile23Committed: http://drupalcode.org/project/examples.git/commitdiff/dde415fc4e2f64eca2...
Next!
Comment #13
mile23Moar!
Comment #14
mile23Committed: http://drupalcode.org/project/examples.git/commitdiff/cd144285ad53159f58...
Comment #15
mile23Comment #16
martin107 commentedFound by lint checker under "probably bug" not using http://pareview.sh/
The return value cannot be what the coder intended ....
/**
* Data provider for testing menu links.
*
* @return array
* Array of page -> link relationships to check for:
* - The key is the path to the page where our link should appear.
* - The value is the link that should appear on that page.
*/
protected function providerMenuLinks() {
return array(
'/' => 'examples/dbtng_example',
'examples/dbtng_example' => 'examples/dbtng_example/list',
'examples/dbtng_example' => 'examples/dbtng_example/add',
'examples/dbtng_example' => 'examples/dbtng_example/update',
'examples/dbtng_example' => 'examples/dbtng_example/advanced',
);
}
duplicate array keys mean that what is actually returned from this function is an array with 2 rows
array( return array(
'/' => 'examples/dbtng_example',
'examples/dbtng_example' => 'examples/dbtng_example/advanced',
);
I hope this is the correct forum for this bug...as it falls under nit-picky review
Comment #18
mile23That's way more of a bug than a nitpicky change. :-)
Let's try it this way, and try not to be upset that our field example seems to be broken again. :-)
Comment #20
mile23And fixed. Now to move on to re-fixing field_example. Again. :-)
Comment #21
mile23Er, actually, set to Active since this is ongoing.
Comment #23
martin107 commentedI blinked and its committed thanks :)
Mile23++
Comment #24
mile23Comment #25
mile23Rescoping to be an umbrella for ongoing per-module coding standards issues.
Comment #26
mile23Comment #27
jlbellidoAdded current issues related with this META.
Thanks for your efforts here!
Comment #28
jlbellidoAdded #2659866: Update DBTNG Example to meet coding standards to the issue description.
Comment #29
mile23Comment #30
navneet0693 commentedTried to add patch on cache_example: https://www.drupal.org/node/2659712, still pending since we have to decide on the best methods : https://www.drupal.org/node/2659712#comment-11149381
Comment #31
mile23Rescoping here to match the Drupal 8 core process: #2571965: [meta] Fix PHP coding standards in core, stage 1
Our coding standards woes are not nearly as severe as core's, but it's much easier to review a single rule over all of the project rather than lots of subjective changes at once.
Comment #33
mile23Added a phpcs.xml.dist file with Drupal.WhiteSpace.Comma rule already in place. This is a rule that Examples already passes.
Add more issues per rule as inspired. :-)
Comment #35
mile23Updated some regressions that had crept in.
Comment #36
mile23Comment #37
mile23Comment #38
mile23Updating instructions after changes to core.
Comment #39
jungleChanging to the default branch
Comment #40
andypostWhen using core-dev it's useful to run sniffers this way
core$ composer run phpcs -- --standard=core/phpcs.xml.dist -ps modules/examples/