如何在 Yii2 项目中安装 Yii2 代码标准?

How to Install Yii2 Code standard in Yii2 Project?

$ ./vendor/bin/phpcs backend/controllers/AdvertisementController.php --colors

ERROR: the "Yii" coding standard is not installed. The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend

 "require-dev": {
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*",
        "yiisoft/yii2-coding-standards": "2.*"
    }

Next, create a phpcs.xml.dist file on the project root directory, see PHP_CodeSniffer Documentation: Using a Default Configuration File

<?xml version="1.0"?>
<ruleset name="Yii2 App Basic Standard">
    <description>Yii2 App Basic coding standard</description>

    <exclude-pattern>/runtime/*</exclude-pattern>
    <exclude-pattern>/web/assets/*</exclude-pattern>
    <exclude-pattern>/vendor/*</exclude-pattern>
    <exclude-pattern>/requirements.php</exclude-pattern>

    <rule ref="vendor/yiisoft/yii2-coding-standards/Yii2"/>

    <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
        <exclude-pattern>/migrations/*</exclude-pattern>
    </rule>
    <rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
        <exclude-pattern>/migrations/*</exclude-pattern>
    </rule>
</ruleset>



$ ./vendor/bin/phpcs backend/controllers/AdvertisementController.php --colors

ERROR: the "Yii" coding standard is not installed. The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend

 "require-dev": {
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*",
        "yiisoft/yii2-coding-standards": "2.*"
    }

接下来,在项目根目录创建一个phpcs.xml.dist文件,参见PHP_CodeSniffer文档:使用默认配置文件

<?xml version="1.0"?>
<ruleset name="Yii2 App Basic Standard">
    <description>Yii2 App Basic coding standard</description>

    <exclude-pattern>/runtime/*</exclude-pattern>
    <exclude-pattern>/web/assets/*</exclude-pattern>
    <exclude-pattern>/vendor/*</exclude-pattern>
    <exclude-pattern>/requirements.php</exclude-pattern>

    <rule ref="vendor/yiisoft/yii2-coding-standards/Yii2"/>

    <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
        <exclude-pattern>/migrations/*</exclude-pattern>
    </rule>
    <rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
        <exclude-pattern>/migrations/*</exclude-pattern>
    </rule>
</ruleset>

Last, use command:

php composer.phar install --dev
vendor/bin/phpcs --encoding=utf-8 --extensions=php .

This thrown me error vendor/bin/phpcs --encoding=utf-8 --extensions=php .

 coding standard is not installed. The installed coding standards are
 MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend

如何安装 Yii2 作为编码标准

首先是扩展,您正在尝试安装您应该知道为什么应该使用它的目的以及何时

来自Yii 2 Core Framework Code Style

The following code style is used for Yii 2.x core and official extensions development. If you want to pull-request code into the core, consider using it.

We aren't forcing you to use this code style for your application. Feel free to choose what suits you better.

何时使用

它仅在您开发时使用 Yii2 不工作 WITH Yii2 意味着仅当您要为框架的核心功能做出贡献时才使用它扩展

何时忽略

如果您正在开发一个使用 Yii2 框架的项目,您不需要安装此扩展,因为如果您有 PHPCS 可以直接在这种情况下工作安装好了,所有已经使用的标准都在PHPCS中,差别不大,

你不需要这个扩展,留下它,PHPCS 可以直接完成工作。