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 phpcs to see what's broken:
    $ cd path/to/drupal/root/
    $ ./vendor/bin/phpcs -ps --standard=modules/examples/phpcs.xml.dist modules/examples/
    
  • If necessary, use phpcfb to do some automated fixing.
  • Review the work that phpcfb did, 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.

Comments

mile23’s picture

StatusFileSize
new13.07 KB
mile23’s picture

Status: Active » Needs review

Ugh. Early submit button pressing.

block_example, cache_example, some phpunit_example and tour_example coverage.

mile23’s picture

Issue summary: View changes
mile23’s picture

Status: Needs review » Needs work
mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new17.1 KB

cron_example and dbtng_example

mile23’s picture

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new20.88 KB

And still going...

Status: Needs review » Needs work

The last submitted patch, 7: 2176147_7.patch, failed testing.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new20.88 KB

The Tyrrany Of The Hanging Paren.

Status: Needs review » Needs work

The last submitted patch, 9: 2176147_9.patch, failed testing.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new21.24 KB

...

mile23’s picture

Status: Needs review » Needs work
mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new32.06 KB

Moar!

mile23’s picture

mile23’s picture

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new994 bytes

Found 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

Status: Needs review » Needs work

The last submitted patch, 16: 2176147-16.patch, failed testing.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new2.19 KB

That'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. :-)

Status: Needs review » Needs work

The last submitted patch, 18: 2176147_18.patch, failed testing.

mile23’s picture

Status: Needs work » Fixed

And fixed. Now to move on to re-fixing field_example. Again. :-)

mile23’s picture

Status: Fixed » Active

Er, actually, set to Active since this is ongoing.

  • Mile23 committed d92b6f7 on 8.x-1.x authored by martin107
    Issue #2176147 by Mile23, martin107: Coding standards review for D8...
martin107’s picture

I blinked and its committed thanks :)

Mile23++

mile23’s picture

Assigned: mile23 » Unassigned
mile23’s picture

Title: Coding standards review for D8 Examples » [meta] Coding standards review for D8 Examples

Rescoping to be an umbrella for ongoing per-module coding standards issues.

mile23’s picture

Issue summary: View changes
jlbellido’s picture

Issue summary: View changes

Added current issues related with this META.

Thanks for your efforts here!

jlbellido’s picture

Issue summary: View changes
mile23’s picture

navneet0693’s picture

Tried 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

mile23’s picture

Issue summary: View changes

Rescoping 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.

  • Mile23 committed 72c6d36 on 8.x-1.x
    Issue #2176147 by Mile23: [meta] Coding standards review for D8 Examples
    
mile23’s picture

Added 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. :-)

  • Mile23 committed a6bc7f9 on 8.x-1.x
    Issue #2176147: Some coding standards regressions.
    
mile23’s picture

Issue summary: View changes

Updated some regressions that had crept in.

mile23’s picture

Issue summary: View changes
mile23’s picture

Issue summary: View changes
mile23’s picture

Issue summary: View changes

Updating instructions after changes to core.

jungle’s picture

Version: 8.x-1.x-dev » 3.x-dev

Changing to the default branch

andypost’s picture

When using core-dev it's useful to run sniffers this way

core$ composer run phpcs -- --standard=core/phpcs.xml.dist -ps modules/examples/