首页 > 解决方案 > 如何在 Yii2 项目中安装 Yii2 代码标准?

问题描述

$ ./vendor/bin/phpcs 后端/控制器/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>



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

错误:未安装“Yii”编码标准。安装的编码标准是 MySource、PEAR、PSR1、PSR12、PSR2、Squiz 和 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>

最后,使用命令:

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

这给我带来了错误 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 作为编码标准

标签: yii2yii2-advanced-app

解决方案


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

来自Yii 2 核心框架代码风格

以下代码风格用于 Yii 2.x 核心和官方扩展开发。如果您想将请求代码拉入核心,请考虑使用它。

我们不会强迫您在应用程序中使用这种代码风格。随意选择更适合您的。

何时使用

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

何时忽略

如果你正在做一个使用Yii2 框架的项目,你不需要安装这个扩展,因为如果你安装了 PHPCS 可以直接工作,所有已经使用的标准都在 PHPCS 中并不多不同的,

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


推荐阅读