benmatselby / sublime-phpcs

🔍 PHP CodeSniffer, PHP Coding Standard Fixer, Linter, and Mess Detector Support for Sublime Text
Other
811 stars 127 forks source link

Per project xml rulesets must be linked in *.sublime-project? #197

Open Brugman opened 4 years ago

Brugman commented 4 years ago

Thanks for making this. I got the basic linting working really fast after failing with SublimeLinter-phpcs.

I'm confused by something. I've read about per project rulesets using a phpcs.xml, and I work in a team, on a few different kinds of PHP projects, so that sounds great.

To test I made the following minimal ruleset:

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
    <description>Ruleset for this project.</description>
    <rule ref="Generic.ControlStructures.InlineControlStructure.NotAllowed"/>
</ruleset>

Now I have the following files:

/webdev/project1/app.php
/webdev/project1/phpcs.xml
/webdev/project1/project1.sublime-project
/webdev/project1/project1.sublime-workspace

When I go to the command line, cd to /webdev/project1/, and run phpcs app.php, I see the xml ruleset in action. The only errors I get are Inline control structures are not allowed.

But when I open the project in Sublime and run Sniff this file, I get all kinds of PSR2 errors. The default ruleset.

From other peoples issues and Googling I found that people add..

    "settings":
    {
        "phpcs":
        {
            "phpcs_additional_args": {
                "--standard": "./phpcs.xml",
                "-n": ""
            },
        }
    }

..to their project1.sublime-project files. So I tried it, and sure enough, now it works.

But *.sublime-project files are not really meant to be committed to repos. And if it's not committed, then every developer has to edit that file manually. Doesn't that defeat the purpose of distributing a per project settings file?

Why can't Sublime check for a phpcs.xml in the current directory, and then its parent, and then its parent, and then its parent, and use it, without me explicitly stating its location? I thought it would.

And lastly, from the command line it does work automatically. Is the command line smarter than Sublime?

I'm confused. I am I doing it wrong? I am missing something?

Thanks for your time.

Brugman commented 4 years ago

I did some reading in the SublimeLinter-phpcs issues, played around with debug mode, and I found a setup that's a little better, but not perfect.

The Sublime plugin runs phpcs from the command line behind the scenes. Obviously. Should've thought of that before. The phpcs.xml gets detected by phpcs when the argument --standard=* is not present.

The default we get out of the box is

    "phpcs_additional_args": {
        "--standard": "PSR2",
        "-n": ""
    },

We can override that in our User settings. But no matter if you set "false", false, "", "null" or null... the plugin passes it onto the command line with --standard in front of it.

What does work, is stupid simple:

    "phpcs_additional_args": {
        // "--standard": "PSR2",
        "-n": ""
    },

Comment the arg out in your User settings, or delete it.

(Con) If you want to sniff without a phpcs.xml you have to re-enable this line temporarily,.. or write a phpcs.xml. (Con) Teach the Sublime users on your dev team this, once. The default is not good enough. (Pro) Teach the Sublime users on your dev team this, once. Once is better than editing *.sublime-project every project. (Pro) Your phpcs.xml gets detected.

I still welcome feedback. This may just be an unnecessary hack.