yii2 - 如何在 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 作为编码标准
解决方案
首先,您正在尝试安装扩展程序,您应该知道应该使用它的目的以及何时使用它。
以下代码风格用于 Yii 2.x 核心和官方扩展开发。如果您想将请求代码拉入核心,请考虑使用它。
我们不会强迫您在应用程序中使用这种代码风格。随意选择更适合您的。
何时使用
它仅在您开发Yii2
不使用 Yii2时使用,这意味着仅当您要为框架的核心功能做出贡献时才使用此扩展
何时忽略
如果你正在做一个使用Yii2 框架的项目,你不需要安装这个扩展,因为如果你安装了 PHPCS 可以直接工作,所有已经使用的标准都在 PHPCS 中并不多不同的,
你不需要这个扩展,留下它,PHPCS 可以直接完成工作。
推荐阅读
- ecmascript-6 - ECMAScript 符号语法的含义?
- linux - 连续扫描目录并执行操作
- macos - Python 安装 / Mac / Homebrew Python VSCode 版本
- oauth - 在 Microsoft BotFramework-Composer 中使用 oauth 时出现“对象引用未设置为对象的实例”错误
- sql - Presto 默认日期显示不正确
- python - python +升级密码学pkg后没有名为cryptography.fernet的模块
- python - StaleElementReferenceException:消息:元素引用已过时
- mysql - MySQL 查询 - 将结果集转换为 JSON
- javascript - 为嵌套模式 graphql/apollo/react 添加突变
- php - 当我在 CodeIgniter 中为数据表源选择多个数据时,我在 ORDER BY 中遇到问题,但出现 500 错误