Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and document running `phpcs` locally #986

Merged
merged 5 commits into from Jun 5, 2017

Conversation

Projects
None yet
3 participants
@nylen
Copy link
Member

commented Jun 1, 2017

This PR makes it easier to run phpcs locally.

To test

  • Make sure the Travis build, especially the phpcs step, still passes.
  • Install phpcs following the instructions in the comments of bin/run-phpcs.sh. Run the script and verify that it works (no output, exit code 0).
-not \( -path './node_modules' \) \
-not \( -path './vendor' \) \
-name '*.php' \
| xargs -d'\n' phpcs --standard=phpcs.ruleset.xml -s

This comment has been minimized.

Copy link
@westonruter

westonruter Jun 2, 2017

Member

FYI: If phpcs.ruleset.xml is renamed to phpcs.xml.dist or phpcs.xml then you don't have to supply --standard at all. I suggest that this could eliminate the need for bin/run-phpcs.sh entirely. See https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#using-a-custom-ruleset

Additionally, the ruleset file can be configured to exclude the node_modules and vendor directories, eliminating the need for using find and xargs.

This comment has been minimized.

Copy link
@westonruter

westonruter Jun 2, 2017

Member

The ruleset just needs to include these lines:

<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

This comment has been minimized.

Copy link
@nylen

nylen Jun 2, 2017

Author Member

Nice! I'll try this out, and then move the setup documentation to a better place.

One more question: is it possible to enable the -s option via the config as well? I find it much easier to parse the output this way.

This comment has been minimized.

Copy link
@nylen

nylen Jun 2, 2017

Author Member

It turns out we need to whitelist directories and files using the <file> tag instead.

With the suggested exclude entries in place, running phpcs -s . is extremely, extremely slow (2 minutes and counting before I killed it). strace reveals that phpcs still recurses into all directories, ignoring the exclude entries. This is a known issue, fixed in the 3.x releases, which we can't use yet: squizlabs/PHP_CodeSniffer#1113

is it possible to enable the -s option via the config as well

From looking through the code, the answer to this is no.

This comment has been minimized.

Copy link
@westonruter

westonruter Jun 2, 2017

Member

From looking at https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml I just found that you can indeed do this! See 7f0fff5.

Now all you need to do to run PHPCS is just do phpcs without any args at all.

@ntwb

ntwb approved these changes Jun 2, 2017

Copy link
Member

left a comment

Looks good to me 👍

edit: Ha, I should have refreshed the page first, I would have seen the above ^^^ 😉

@nylen nylen changed the title Add a script to run `phpcs` locally Simplify and document running `phpcs` locally Jun 2, 2017


https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#standalone

You should now be able to run `phpcs -s` from the root directory of this

This comment has been minimized.

Copy link
@westonruter

westonruter Jun 3, 2017

Member

We can now remove -s from the instructions since it is defined as a default arg in phpcs.xml

.travis.yml Outdated
-not \( -path './vendor' \) \
-name '*.php' \
| xargs -d'\n' phpcs --standard=phpcs.ruleset.xml -s
phpcs -s

This comment has been minimized.

Copy link
@westonruter

westonruter Jun 3, 2017

Member

Per below, the -s can now be removed since it is the default.

not yet compatible with the WordPress coding standards. For more information see
[this issue](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/718).

The easiest way to get `phpcs` is to download the .phar archive from the latest

This comment has been minimized.

Copy link
@westonruter

westonruter Jun 3, 2017

Member

I wonder if it would be easier to make phpcs 2.9.x a Composer dependency instead? If we add a composer.json which has wp-coding-standards/wpcs among its dev-requirements then all a contributor (and Travis CI alike) would need to do is composer install and then do ./vendor/bin/phpcs.

This comment has been minimized.

Copy link
@nylen

nylen Jun 5, 2017

Author Member

I tried this out, and am getting the following error:

PHP Fatal error:  Uncaught exception 'PHP_CodeSniffer_Exception' with message 'Referenced sniff "WordPress-Core" does not exist'

Is it expected that we need to call vendor/bin/phpcs --config-set?

This comment has been minimized.

Copy link
@westonruter

westonruter Jun 5, 2017

Member

@nylen yes, that's right.

@nylen nylen force-pushed the add/phpcs-script branch from 7f0fff5 to 977dcc7 Jun 5, 2017

@nylen

This comment has been minimized.

Copy link
Member Author

commented Jun 5, 2017

I'll merge this and explore using composer in a separate PR.

@nylen nylen merged commit b8ed716 into master Jun 5, 2017

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@nylen nylen deleted the add/phpcs-script branch Jun 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.