如何部分禁用 PHPCS 的 PSR-12 声明(strict_types=1) 检查?

The*_*ith 3 php phpcs psr-12

我目前正在运行php vendor/bin/phpcs --standard=PSR12 src我的几个项目的 CI。

\n\n

他们已经失败了 6 个多月,因为我的代码组织如下:

\n\n
<?php declare(strict_types=1);\n\n/**\n * This file is part of SimpleDTO, a PHP Experts, Inc., Project.\n *\n * Copyright \xc2\xa9 2019 PHP Experts, Inc.\n * Author: Theodore R. Smith <theodore@phpexperts.pro>\n *  GPG Fingerprint: 4BF8 2613 1C34 87AC D28F  2AD8 EB24 A91D D612 5690\n *  https://www.phpexperts.pro/\n *  https://github.com/phpexpertsinc/SimpleDTO\n *\n * This file is licensed under the MIT License.\n */\n\nnamespace PHPExperts\\SimpleDTO;\n
Run Code Online (Sandbox Code Playgroud)\n\n

它当前生成几个 PHPCS 警告:

\n\n
--------------------------------------------------------------------------------------------\nFOUND 3 ERRORS AFFECTING 2 LINES\n--------------------------------------------------------------------------------------------\n 1 | ERROR | [x] Header blocks must be separated by a single blank line\n 1 | ERROR | [x] Opening PHP tag must be on a line by itself\n 3 | ERROR | [ ] The file-level docblock must follow the opening PHP tag in the file header\n--------------------------------------------------------------------------------------------\n
Run Code Online (Sandbox Code Playgroud)\n\n

有什么方法可以保留 PSR-12 检查的其余部分而不是那些吗?

\n

Mas*_*din 6

您有两个选择:

  1. 使用--exclude命令行参数,它允许您指定您不想担心的嗅探。使用你的例子,这将是php vendor/bin/phpcs --standard=PSR12 --exclude=PSR12.Files.FileHeader,PSR12.Files.OpenTag src

  2. phpcs.xml使用以下设置创建一个文件:

<?xml version="1.0"?>
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">
  <rule ref="PSR12">
    <exclude name="PSR12.Files.FileHeader" />
    <exclude name="PSR12.Files.OpenTag" />
  </rule>
</ruleset>
Run Code Online (Sandbox Code Playgroud)

如果您使用该名称 [ 1 ],则会自动选取phpcs.xml(或)文件,否则使用参数来指向其文件位置。使用该文件时,您还可以更准确地指定使用命令行选项无法执行的嗅探(例如) 。phpcs.xml.dist--standardPSR12.Files.OpenTag.NotAlone--exclude