如何在我的 Yii2 项目中安装和运行 yii2-code-sniffer 或 Php Code Sniffer?

How to install and working yii2-code-sniffer Or Php Code Sniffer in my Yii2 Project?

我已经尝试过 Yii2 Code Sniffer Git RichWeber 的 Hook 和 squizlabs/PHP_CodeSniffer。 我无法安装和管理任何东西 properly.There 我添加了一些全局作曲家文件和一些 vendor/bin/。 我完全不知道如何配置它以及如何 运行 它在我的项目中有用。

我已经使用了 phpcs 和 yii2 代码标准,将其添加到 composer.json

"yiisoft/yii2-coding-standards": "2.*", "squizlabs/php_codesniffer": "^3.0@dev"

并在根目录添加phpcs.xml.dist并添加此标准

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Yii2">
    <description>Yii 2 Web Application Framework Coding Standard</description>

    <rule ref="PSR2">
        <!-- Property names MUST start with an initial underscore if they are private. -->
        <exclude name="PSR2.Classes.PropertyDeclaration.Underscore"/>

        <!-- Opening parenthesis of a multi-line function call must be the last content on the line. -->
        <!-- Closing parenthesis of a multi-line function call must be on a line by itself. -->
        <!-- Only one argument is allowed per line in a multi-line function call. -->
        <exclude name="PEAR.Functions.FunctionCallSignature"/>

        <!-- We don't have line length (line width) limits. -->
        <!--<exclude name="Generic.Files.LineLength"/>-->

        <!-- CASE keyword must be indented 4 spaces from SWITCH keyword. -->
        <exclude name="PSR2.ControlStructures.SwitchDeclaration"/>
    </rule>

    <!-- Property declaration rules. -->
    <!-- TODO: -->

    <!-- Function call signature. -->
    <!-- TODO: -->

    <!-- Switch declaration. -->
    <!-- TODO: -->

    <!-- ... other Yii2 specific rules. -->
    <rule ref="Generic.Arrays.DisallowLongArraySyntax"/>

    <!-- If string doesn't contain variables or single quotes, use single quotes. -->
    <rule ref="Squiz.Strings.DoubleQuoteUsage.NotRequired"/>

    <!-- Replaces Yii2_Sniffs_Files_SpacesAroundConcatSniff -->
    <rule ref="Squiz.Strings.ConcatenationSpacing">
        <properties>
            <property name="spacing" value="1" />
            <property name="ignoreNewlines" value="true" />
        </properties>
    </rule>

    <!-- Replaces Yii2_Sniffs_Properties_PrivatePropertiesUnderscoreSniff -->
    <rule ref="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>

    <!-- https://github.com/squizlabs/PHP_CodeSniffer/issues/1008 -->
    <rule ref="Squiz.ControlStructures.ControlSignature">
        <properties>
            <property name="requiredSpacesBeforeColon" value="0" />
        </properties>
    </rule>
    <!-- line length -->
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120" />
            <property name="absoluteLineLimit" value="120" />
        </properties>
     </rule>
     <rule ref="Generic.PHP.Syntax"/>
    <!-- Ignore for migrations. -->
    <!-- Ignore missing namespace for migrations -->
    <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
        <exclude-pattern>m\d{6}_\d{6}_.+\.php$</exclude-pattern>
    </rule>
    <!-- Ignore camel caps format for class name of migrations -->
    <rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
        <exclude-pattern>m\d{6}_\d{6}_.+\.php$</exclude-pattern>
    </rule>

    <!-- Ignore for tests. -->
    <!-- Ignore method name prefixed with underscore to indicate visibility -->
    <rule ref="PSR2.Methods.MethodDeclaration.Underscore">
        <exclude-pattern>tests?/*(Cest|Test).php$</exclude-pattern>
    </rule>
    <!-- Ignore declare new symbols and execute logic with side effects same file -->
    <rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
        <exclude-pattern>/tests?*/_bootstrap.php$</exclude-pattern>
        <exclude-pattern>/web/(index|index-test).php$</exclude-pattern>
    </rule>
    <exclude-pattern>*/i18n/data/*</exclude-pattern>
    <exclude-pattern>*/views/errorHandler/*</exclude-pattern>
    <exclude-pattern>*/requirements/*</exclude-pattern>

 <exclude-pattern>ProfileTarget.php</exclude-pattern>
</ruleset>

现在使用这些命令来测试每个文件夹标准,就像我正在测试后端一样 vendor/bin/phpcs --encoding=utf-8 --extensions=php backend

并修复这些错误 运行 这个命令 vendor/bin/phpcbf --encoding=utf-8 --extensions=php backend

Note: You have to remove parse and syntax error yourself.