LoginSignup
1
3

More than 1 year has passed since last update.

PHP_CodeSnifferのコーディング規約をカスタマイズする方法

Last updated at Posted at 2022-05-23

最初に

この記事はPHP_CodeSnifferの導入方法と解説の続きになります。

コーディング規約をカスタマイズする

コーディング規約をカスタマイズをするには
下記の場所から各種規約ルールを書き換える必要がある。

ruleset.xml
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="CakePHP">
    <description>CakePHP coding standard</description>

    <config name="installed_paths" value="../../slevomat/coding-standard,../../../slevomat/coding-standard,../vendor/slevomat/coding-standard"/>

    <exclude-pattern>\.git</exclude-pattern>
    <exclude-pattern>/*/tmp/</exclude-pattern>
    <exclude-pattern>tests/*/templates/*</exclude-pattern>

    <!-- PSR12 Standard -->
    <rule ref="PSR12">
        <exclude name="PSR12.Files.FileHeader.SpacingAfterBlock"/>
        <exclude name="PSR12.Files.FileHeader.IncorrectOrder"/>
        <!--
        Property and method names with underscore prefix are allowed in CakePHP.
        Not using underscore prefix is a recommendation of PSR2, not a requirement.
        -->
        <exclude name="PSR2.Classes.PropertyDeclaration.Underscore"/>
        <exclude name="PSR2.Methods.MethodDeclaration.Underscore"/>
    </rule>

    <!-- Relax rules from PSR12 -->
    <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
        <exclude-pattern>*/config/Migrations/*</exclude-pattern>
    </rule>
    <rule ref="PSR1.Files.SideEffects">
        <exclude-pattern>*/config/*</exclude-pattern>
        <exclude-pattern>*/tests/*</exclude-pattern>
    </rule>
    <rule ref="PSR1.Methods.CamelCapsMethodName">
        <exclude-pattern>*/src/Controller/*</exclude-pattern>
        <exclude-pattern>*/src/Command/*</exclude-pattern>
        <exclude-pattern>*/src/Shell/*</exclude-pattern>
        <exclude-pattern>*/tests/*</exclude-pattern>
    </rule>

    <!-- Additional sniffs outside of PSR12 -->
</ruleset>

コーディング規約の場所を変更

コーディング規約の場所を変更する場合は
installed_pathsに追加します。
vendor配下に置きたくない場合などに活用できます。

$ phpcs --config-set installed_paths /path/to/one,/path/to/two

デフォルトのコーディング規約の設定

デフォルトの標準を変更する場合は、
default_standardを設定します。

$ phpcs --config-set default_standard CakePHP

最後に

各プロジェクトにおいて、コーディング規約を定義する場合は
PHP_CodeSnifferを揃えなければエラーが正しく表示されないため、おすすめです。

参考

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3