2017-07-13 11:00
浏览 145

自定义编码标准嗅探不起作用 - PHP_CodeSniffer_Exception

I am working on creating a custom ruleset for sniffing code in WordPress, and I'm using PHP_CodeSniffer for that.

The custom ruleset can be found here: https://github.com/infinum/coding-standards-wp.

Now when I try to use them I get this error

Fatal error: Uncaught exception 'PHP_CodeSniffer_Exception' with message 'Referenced sniff "WordPress" does not exist' in /wpcs/vendor/squizlabs/php_codesniffer/CodeSniffer.php:1167
Stack trace:
#0 /wpcs/vendor/squizlabs/php_codesniffer/CodeSniffer.php(780): PHP_CodeSniffer->_expandRulesetReference(Object(SimpleXMLElement), '/...', 0)
#1 /wpcs/vendor/squizlabs/php_codesniffer/CodeSniffer.php(578): PHP_CodeSniffer->processRuleset('/...')
#2 /wpcs/vendor/squizlabs/php_codesniffer/CodeSniffer/CLI.php(956): PHP_CodeSniffer->initStandard(Array, Array, Array)
#3 /wpcs/vendor/squizlabs/php_codesniffer/CodeSniffer/CLI.php(113): PHP_CodeSniffer_CLI->process()
#4 /wpcs/vendor/squizlabs/php_codesniffer/scripts/phpcs(25): PHP_CodeSniffer_CLI->runphpcs()
#5 {main}
  thrown in /wpcs/vendor/squizlabs/php_codesniffer/CodeSniffer.php on line 1167

I have no idea how to fix this. I have searched all around and modeled the ruleset like other people have, and I have no idea why this happens.


So when I install the standards using composer in my project and run

vendor/bin/phpcs --standard=vendor/infinum/coding-standards-wp wp-content/themes/twentyseventeen/functions.php

It works, it's just that it's not picking up my custom sniffs for some reason.

And when I put the coding standard in my globally installed wpcs folder, I get the above error in my Sublime console.


Ok, so I have wpcs installed in my home folder. In there I placed Infinum folder and inside there are my sniffs. This works only if I remove

 <config name="installed_paths" value="vendor/wp-coding-standards/wpcs"/>

from ruleset.xml

My Sublime will pick it up and the sniffs (now without namespaces) work as they should, all great.

When I install it in a project using composer, I get the above error again.

The difference when it's loaded globally (so that it's available to Sublime) the folder structure looks like this

  -- Infinum
    -- Sniffs
      -- Classes
      -- Shortcodes

And when I install it with composer in my project it is located inside vendor folder

  -- bin
  -- composer
  -- infinum
    -- coding-standards-wp
      -- Sniffs
        -- Classes
        -- Shortcodes
  -- squizlabs
  -- wp-coding-standards


Ok, so I made it work locally.

It picks up the custom sniffs and all.

But now if I place it in my global wpcs folder I cannot get it to work. Well I guess I'll just redo this globally so that it works with Sublime...

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyi4170 2017-07-13 22:51

    First, to your WordPress problem. Your ruleset includes this line:

    <rule ref="WordPress"/>

    That tells PHPCS to load in the entire WordPress coding standard. But PHPCS doesn't ship with a WordPress coding standard, so you need to tell PHPCS where it is.

    When you composer install your project, composer clones the WordPress coding standards repo, which has this section in its composer.json:

    "scripts"    : {
        "post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths ../../..",
        "post-update-cmd" : "\"vendor/bin/phpcs\" --config-set installed_paths ../../.."

    The --config-set installed_paths command tells PHPCS where else to look for coding standards besides its default directory. In this case, the WordPress standard is telling PHPCS to look in the directory where the WordPress standards have all been placed by composer. Now when you say WordPress in your ruleset.xml file, PHPCS knows where to look.

    You'll also find that running vendor/bin/phpcs -i will show that the WordPress coding standards are "installed".

    If you don't want to have to composer install your coding standard to make it work, you can clone the WordPress coding standards somewhere else and run phpcs --config-set installed_paths /path/to/WordPress-Coding-Standards manually.

    The reason why your custom sniffs are not being picked up is because they are not in a Sniffs directory at the same level as your ruleset.xml file. You need to place your ruleset.xml file directly into your Infinum directory and not on the very top level of your repo. Or, you can move the Sniffs directory to the top level.

    This change will also let you specify your coding standard using it's name. Instead of --standard=vendor/infinum/coding-standards-wp you would use --standard=vendor/infinum/coding-standards-wp/Infinum.

    Once you do this (or maybe before and after), try running phpcs with the -vv command line argument. For example, before the change run:

    vendor/bin/phpcs -vv --standard=vendor/infinum/coding-standards-wp wp-content/themes/twentyseventeen/functions.php

    And after the change run:

    vendor/bin/phpcs -vv --standard=vendor/infinum/coding-standards-wp/Infinum wp-content/themes/twentyseventeen/functions.php

    This will output a lot of content (pick a small file to run it over to reduce this) but the debug output at the very top is what you want to look at. It will show you how your ruleset has been parsed and where all the sniffs are coming from. After moving your ruleset.xml file, you should see PHPCS pick up your custom sniffs and load them in. The output will look something like this:

    Processing ruleset coding-standards-wp/Infinum/ruleset.xml
        Adding sniff files from coding-standards-wp/Infinum/Sniffs directory
            => coding-standards-wp/Infinum/Sniffs/Classes/OnlyClassInFileSniff.php
            => coding-standards-wp/Infinum/Sniffs/Shortcodes/DisallowDoShortcodeSniff.php

    You'll also see where it found the WordPress sniffs, what config options it detected etc.

    点赞 评论

相关推荐 更多相似问题