squizlabs / sublime-PHP_CodeSniffer

Sublime Text plugin for PHP_CodeSniffer
51 stars 14 forks source link

Configure to use a projects ruleset #10

Open ghost opened 3 years ago

ghost commented 3 years ago

Our team is currently in the process of trying to get codesniffer working in sublime.

So far we have been able to set it up and get it running as it should with the default properties.

However, due to our different codebases we ideally need to use a different ruleset per project.

Having looked at the PHP_CodeSniffer documentation it says that If you run PHP_CodeSniffer without specifying a coding standard, PHP_CodeSniffer will look in the current directory, and all parent directories, for a file called either .phpcs.xml, phpcs.xml, .phpcs.xml.dist, or phpcs.xml.dist

I've removed the phpcs_standard parameter from the PHP_CodeSniffer.sublime-settings file but it doesn't seem to be picking up my projects phpcs.xml file at all.

Is there another setting I need to change somewhere to tell this plugin to use my projects phpcs.xml file?

We have installed PHP_CodeSniffer globally via composer.

This is the current PHP_CodeSniffer.sublime-settings file:

{
     // Run PHPCS when a buffer is saved.
    "run_on_save": false,

    // Path to PHP.
    "php_path": "C:/WAMP/PHP/7.1.33/php.exe",

    // Path to the PHPCS script.
    "phpcs_path": "C:/Users/rob/AppData/Roaming/Composer/vendor/squizlabs/php_codesniffer/bin/phpcs",

    // Path to the PHPCBF script.
    "phpcbf_path": "C:/Users/rob/AppData/Roaming/Composer/vendor/squizlabs/php_codesniffer/phpcbf",

    // PHPCS Standard to Use. Can be a string or a dict (folder => standard).
    // "phpcs_standard": "Squiz",

    // Additional arguments to pass to PHPCS/PHPCBF.
    "additional_args": [],

    // Gutter error icon colour.
    "error_scope": "comment.block",

    // Gutter warning icon colour.
    "warning_scope": "function"
}

This is what our custom ruleset looks like:

<?xml version="1.0"?>

<ruleset name="Custom-Ruleset">
    <description>Custom Ruleset</description>
    <arg name="tab-width" value="4"/>

     <exclude-pattern>*/vendor/*</exclude-pattern>

    <rule ref="PSR12">
        <exclude name="PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose" />
        <exclude name="PSR12.Operators.OperatorSpacing.NoSpaceBefore" />
        <exclude name="PSR12.Operators.OperatorSpacing.NoSpaceAfter" />
        <exclude name="PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon" />
        <exclude name="PEAR.ControlStructures.MultiLineCondition.StartWithBoolean" />
        <exclude name="PEAR.ControlStructures.MultiLineCondition.SpacingAfterOpenBrace" />
    </rule>

    <rule ref="Squiz">
        <exclude name="Squiz.Commenting.ClosingDeclarationComment.Missing" />
        <exclude name="Squiz.Commenting.FileComment.Missing" />
        <exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop" />
        <exclude name="Squiz.Commenting.FunctionComment.SpacingAfterParamType" />
        <exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" />
        <exclude name="Squiz.Files.FileExtension.ClassFound" />
        <exclude name="Squiz.Operators.ComparisonOperatorUsage.ImplicitTrue" />
        <exclude name="Squiz.Operators.ComparisonOperatorUsage.NotAllowed" />
        <exclude name="Squiz.PHP.DisallowComparisonAssignment.AssignedComparison" />
        <exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen" />
        <exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose" />
        <exclude name="Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose" />
        <exclude name="Squiz.WhiteSpace.FunctionOpeningBraceSpace.SpacingAfter" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.After" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.AfterLast" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.Before" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.BeforeFirst" />
    </rule>

    <rule ref="Generic.Formatting.SpaceAfterCast">
        <properties>
            <property name="spacing" value="1" />
        </properties>
    </rule>

    <rule ref="Generic.Formatting.SpaceAfterNot">
        <properties>
            <property name="spacing" value="0" />
        </properties>
    </rule>

    <rule ref="Generic.PHP.ForbiddenFunctions">
        <properties>
            <property name="forbiddenFunctions" type="array">
                <element key="count" value="sizeof"/>
                <element key="print" value="echo"/>
            </property>
         </properties>
    </rule>
</ruleset>

The ruleset seems to work fine in other editor plugins (i.e. linter-phpcs for Atom), so I'm fairly certain that I'm missing an important configuration setting in this plugin.

gsherwood commented 3 years ago

This project is no longer maintained and doesn't support this feature.

As an explanation, PHPCS itself will go looking for a ruleset.xml file if asked to scan a file or directory, but this plugin sends the content as STDIN so it needs to specify a coding standard path for PHPCS to use.

There are other PHPCS plugins for Sublime that may support this feature, so it's probably worth having a look at what else is available.