wongjn / vscode-php-sniffer

Visual Studio Code extension for PHP_Codesniffer validation and formatting.
https://marketplace.visualstudio.com/items?itemName=wongjn.php-sniffer
MIT License
46 stars 7 forks source link

Working with project dependencies #1

Closed swashata closed 5 years ago

swashata commented 5 years ago

Hello,

First of all, thank you very much for this extension. I really appreciate the time and effort you've put behind this. Kudos.

So I am trying to switch over from vscode-phpcs extension. I have phpcs installed as my project dependency. My composer.json is something like this

{
    "require": {},
    "require-dev": {
        "squizlabs/php_codesniffer": "3.3.1",
        "wp-coding-standards/wpcs": "1.0.0",
        "wimg/php-compatibility": "8.2.0",
        "dealerdirect/phpcodesniffer-composer-installer": "0.4.4",
        "giacocorsiglia/wordpress-stubs": "^4.9"
  }
}

And I have a phpcs.xml file in the root of my project/repo.

<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Plugins">
    <description>Generally-applicable sniffs for WordPress plugins</description>

    <rule ref="WordPress-Core">
        <exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/>
        <exclude name="WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned"/>
        <exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning"/>
        <exclude name="WordPress.PHP.YodaConditions"/>
    </rule>

    <rule ref="WordPress.WP.I18n">
        <properties>
            <property name="text_domain" type="array" value="ipt_fsqm" />
        </properties>
    </rule>
    <rule ref="WordPress.Files.FileName">
        <properties>
            <property name="strict_class_file_names" value="false" />
        </properties>
    </rule>
    <rule ref="WordPress.NamingConventions.ValidVariableName">
        <properties>
            <property name="customPropertiesWhitelist" type="array" value="myMixedCasePropery,AnotherMixedCaseProperty" />
        </properties>
    </rule>
    <rule ref="WordPress.Arrays.MultipleStatementAlignment">
        <properties>
            <property name="exact" value="false" />
        </properties>
    </rule>
    <rule ref="Generic.Formatting.MultipleStatementAlignment">
        <properties>
            <property name="maxPadding" value="10" />
        </properties>
    </rule>

    <rule ref="WordPress-Docs">
        <exclude name="Squiz.Commenting.InlineComment.InvalidEndChar"/>
        <exclude name="Squiz.PHP.CommentedOutCode"/>
        <exclude-pattern>*/tests/phpunit/*</exclude-pattern>
    </rule>

    <rule ref="WordPress-Docs">
        <exclude name="Squiz.Commenting.FileComment.Missing"/>
        <exclude name="Squiz.Commenting.FunctionComment.Missing"/>
        <exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop"/>
    </rule>

    <!-- Check all PHP files in directory tree by default. -->
    <arg name="extensions" value="php"/>
    <file>.</file>

    <!-- Show sniff codes in all reports -->
    <arg value="s"/>

    <!-- PHPCompatibility -->
    <config name="testVersion" value="5.6-7.2"/>
    <rule ref="PHPCompatibility"/>

    <exclude-pattern>*/node_modules/*</exclude-pattern>
    <exclude-pattern>*/vendor/*</exclude-pattern>
    <exclude-pattern>*/bower_components/*</exclude-pattern>
    <exclude-pattern>*/dist/*</exclude-pattern>
</ruleset>

Within vscode preference, I have set the following to tell the extension about phpcs path.

{
    "phpSniffer.executablesFolder": "${workspaceFolder}/vendor/bin/"
}

PS: I do have the trailing slash added, but still nothing really happens.

But nothing really happens. I am not sure how the extension is supposed to work. I assumed, it would show lint errors when I open a file? I can confirm phpcs itself works because if I run

./vendor/bin/phpcs somefile.php

It works just fine. Am I missing something? Please let me know.

swashata commented 5 years ago

I also realize that ${workspaceFolder} may not work for vscode settings. So I have set the following, yet no result :(

{
  "phpSniffer.executablesFolder": "./vendor/bin/",
  "phpSniffer.standard": "./phpcs.xml"
}
swashata commented 5 years ago

Ok, it looks like both the above settings require absolute path. So when I changed

{
  "phpSniffer.executablesFolder": "/Volumes/Development/vagrant/www/eform/public_html/wp-content/plugins/wp-fsqm-pro/vendor/bin/",
  "phpSniffer.standard": "/Volumes/Development/vagrant/www/eform/public_html/wp-content/plugins/wp-fsqm-pro/phpcs.xml"
}

It started working. Maybe you can consider this a feature request? I will try to add this in a fork and get back to you.

wongjn commented 5 years ago

I think passing the workspace folder as the CWD to the command execution might work.

swashata commented 5 years ago

Yes, that should definitely do. If someone is passing in absolute path, then the CWD wouldn't matter anyway.

elvishp2006 commented 5 years ago

I'm having the same problem, but my php is running inside a docker container, I have a /usr/bin/php that looks like this:

#!/bin/bash

docker exec \
    -it \
    -u $(id -u) \
    -w "$PWD" \
    php7 \
    php "$@"
wongjn commented 5 years ago

Are you running the latest version of the extension? What is the command to run phpcs with PHP in docker? Could you provide the settings you are using?

elvishp2006 commented 5 years ago

Yes, I'm using the latest version of the extension. I got a script on my composer.json:

  "scripts": {
    "lint": "phpcs",
    "lint:fix": "phpcbf"
  }

I just need to run composer lint to run my phpcs.