假设我有两三个项目,它们都使用不同的 PHP CodeSniffer 标准(定制)。
我希望能够将它们作为 require-dev
依赖项包含在 Composer 中,这样当启动项目存储库时,它们将与 PHPUnit 和我的其他开发需求一起安装。
目前我的机器的全局 phpcs
标准文件夹中有所有标准,我通过 phpcs.xml
配置文件告诉每个项目使用哪个标准项目根。
我可以指定标准的路径,以便可以通过 composer 安装和使用它吗?
最佳答案
您可以为 phpcs
指定一个额外的路径来查找使用 installed_paths
setting 的标准.下面是一个示例 phpcs.xml
配置:
<?xml verison="1.0"?>
<ruleset name="MyProject">
<description>My cool project, using a custom PHPCS standard</description>
<rule ref="MyCoolStandard" />
<config name="installed_paths" value="vendor/myvendorhere" />
</ruleset>
然后假设您的自定义 PHPCS 标准具有如下文件结构:
+ MyCoolStandard - Sniffs - Classes - MyClassSniff.php - ruleset.xml
You can add a basic composer.json
file to it and give it a name. An important part here is that you will need to tell it to install into a path that matches the standard name - or more simply just name the package as your standard (note: needs to match ruleset.xml
in the standard, case included):
{
"name": "myvendorhere/MyCoolStandard",
"description": "My cool PHPCS standard",
"require": {}
}
现在您可以将它作为 VCS 存储库包含在您的项目中:
# Project: composer.json
...
"repositories": [
{
"type": "vcs",
"url": "git@github.com:myvendorhere/myreponame.git"
}
],
"require-dev": {
"myvendorhere/MyCoolStandard": "dev-master"
}
...
长话短说:
- 使用
installed_paths
CodeSniffer 配置设置指定 Composer 的供应商路径(直到以您的标准命名的文件夹)作为标准路径 - 在您的标准存储库中,以标准命名作曲包(区分大小写)。它必须与您在
ruleset.xml
中使用的名称相同,以便phpcs
可以找到它,否则您会收到错误消息说找不到它。 - 将您的自定义标准包括为 a VCS repo在项目的 Composer 配置中(或更好的方式...!)
关于php - 我可以将 CodeSniffer 标准指定为开发 Composer 依赖项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36755484/