ikappas / vscode-phpcs

PHP CodeSniffer for Visual Studio Code
MIT License
128 stars 56 forks source link

phpcs: The "WordPress" coding standard set in your configuration is not installed. #58

Closed kirtangajjar closed 6 years ago

kirtangajjar commented 7 years ago

It has been set, I can set the standards through command line and it just works fine. However, I can't get it to work in vscode

etiennejcharles commented 7 years ago

1) To check your code standard installed via phpcs, in the commands line you should be able to run

phpcs -i 

It should contain "WordPress" in the results, if not, it's not installed. see here

image

2) Did you include in your settings the standard to use ?

right-here
{
     "phpcs.standard": "WordPress"
}

As we're speaking, it's working on my vscode install. (1.15.1)

shrop commented 7 years ago

I am experiencing the same issue with my settings: "phpcs.standard": "Drupal,DrupalPractice"

"The "Drupal" coding standard set in your configuration is not installed." Related, does it only work with one standard at a time?

Here are my phpcs -i results phpcs -i The installed coding standards are Drupal, DrupalPractice, MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend

shrop commented 7 years ago

It seems like it is working for me if I enter full paths for where they live on my file system. Would like to get it working again with the short names (and multiples if possible) :)

kevinwhoffman commented 6 years ago

Here is how I was able to get phpcs working with the WordPress coding standards.

Note: This installs phpcs and the WordPress coding standards locally into each project. This is the approach I recommend since it will work everywhere the project is installed, regardless of whether the developer has already set up phpcs globally on their machine.

  1. Insert the following into composer.json:
"require-dev": {
    "squizlabs/php_codesniffer": "*",
    "wp-coding-standards/wpcs": "^0.13.1"
},
"scripts": {
    "post-install-cmd": [
        "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs"
    ],
    "post-update-cmd": [
        "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs"
    ]
}
  1. Insert the following into your Visual Studio Code User Settings:
"phpcs.enable": true,
"phpcs.standard": "WordPress"
  1. Run composer install or composer update.

Now the WordPress coding standards in your vendor directory will be made available to phpcs within Visual Studio Code and you should begin to see PHP errors reported in you Problems panel as you type.

dingo-d commented 6 years ago

I have tried with projects that have vendor folder, and without, and it's not working...

kevinwhoffman commented 6 years ago

@dingo-d Please provide more information about what is not working.

Did you run composer install and confirm that you have a directory at vendor/bin/phpcs?

dingo-d commented 6 years ago

Yes, I have the vendor folder with phpcs installed with the specific coding standard. I have put in my settings

"phpcs.enable": true,
"phpcs.standard": "vendor/infinum/coding-standards-wp/Infinum",

still get the

phpcs: Unexpected token in JSON at position 24379

error when opening any php file.

ikappas commented 6 years ago

@dingo-d There is an issue with Infinum declaring default report type (see here) in the ruleset. You can verify this yourself by running the following command in your workspace root:

./vendor/bin/phpcs --report=json --standard=Infinum .

which will output the json report followed by the list of errors. That is why you get an error with all of your php files because the extension expects to get an json response from phpcs but instead gets something else.

For the record, and if Infinum was working properly you could have followed the following steps to set it up:

In order to configure Infinum with phpcs and composer you need to add the following snippet in your composer.json:

"require-dev": {
    "squizlabs/php_codesniffer": "*",
    "infinum/coding-standards-wp": "^0.2.3"
},
"scripts": {
    "post-install-cmd": [
        "\"vendor/bin/phpcs\" --config-set installed_paths vendor/infinum/coding-standards-wp"
    ],
    "post-update-cmd": [
        "\"vendor/bin/phpcs\" --config-set installed_paths vendor/infinum/coding-standards-wp"
    ]
}

You can verify whether Infinium is available to your project's vendored phpcs by running:

bash$ ./vendor/bin/phpcs -i
The installed coding standards are PEAR, PHPCS, Zend, PSR2, MySource, Squiz, PSR1 and Infinum

After that in your vscode settings.json:

"phpcs.enable": true,
"phpcs.standard": "Infinum",
ikappas commented 6 years ago

@kirtangajjar Thank you for submitting this issue. Can you please provide some more information on how you have your project/phpcs/vscode setup?

dingo-d commented 6 years ago

Hi @ikappas thanks for the answer. I will update the standards with the correct post install and update scripts, although we had the post install commands set up in the composer file: link.

As for json report, this seems like an upstream issue. I will report this in the PHPCS repo. If you specify the standard that has the standard defined, it should override it.

kirtangajjar commented 6 years ago

@ikappas I encountered this issue long time ago. Maybe I was able to solve it, or just gave up. I just can't recall. I would've loved to help, but unfortunately I can't as I don't recall much details.

Even the project I was working on was kind of an assignment and I've finished it. Hence, I don't need this particular functionality now :(

dingo-d commented 6 years ago

I reported it to the official repo and will see what they'll say. In the meantime, the best course of actions would be to remove the report type from the standard altogether.

ikappas commented 6 years ago

@kirtangajjar No problem! I will mark this issue as resolved. Please feel free to open a new issue if you continue to experience any issues.

kirtangajjar commented 6 years ago

@ikappas I have installed phpcs globally using composer. My phpcs -i output is:

The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, WordPress-VIP, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core

Yet I get the error mentioned in title when I set phpcs.standard to WordPress. Can you reopen the issue?

KamWis commented 6 years ago

Same issue - bumping it up

madbbb commented 6 years ago

I ran into the same problem today. I have phpcs installed globally and phpcs into my project. My phpcs -i output includes all needed standards, but when I ran vendor/bin/phpcs -i it had it's own installed presets. So I had to configure phpcs installed in vendor/bin/ again vendor/bin/phpcs --config-set installed_paths vendor/path-to-standards

KamWis commented 6 years ago

I have also found the solution :) Thanks @madbbb

phpcs installation in project I am working with resides in /vendor/squizlabs/php_codesniffer/scripts/phpcs - therefore I needed to point it directly in the project's folder /vendor/squizlabs/php_codesniffer/scripts/phpcs --config-set installed_paths ~/wpcs

That was confusing for me because earlier I was looking for vendor/bin/phpcs/.

I mean - it's good that local installation overrides global settings. But an error is thrown at me when it happens and specific settings aren't present, is very unclear. I would be happy if instead of error phpcs could:

The second point is easier, though, and can still be implemented w/o first one. Even when global settings won't be used - error at least can point out to the path of the failing installation.

I believe either of those points would resolve bunch of issues :)

GaryJones commented 6 years ago

If you want to avoid having to manually registering the WPCS standards, try adding https://github.com/Dealerdirect/phpcodesniffer-composer-installer to your require-dev - it will automatically register the found standards and works great with WPCS.

benosho commented 6 years ago

I have phpcs installed globally so I can access it from any folder. I get the errors when I install wpcs in a folder other than the project root directory - phpcs -i says everything is okay, but vscode complains.

What has worked for me is installing wpcs in the project root directory. With composer installed run: composer create-project wp-coding-standards/wpcs:dev-master --no-dev from your project root directory (many thanks to Tom McFarlin for this -- https://code.tutsplus.com/tutorials/using-php-codesniffer-with-wordpress-installing-and-using-the-wordpress-rules--cms-26443). Inform phpcs of the location of your wpcs by running: _phpcs --config-set installedpaths wpcs. Run: phpcs -i to confirm that phpcs sees your WordPress standards. Set phpcs.standard to WordPress in vscode. That's it.

wpamitkumar commented 5 years ago

i have found one solution :

-run this command from terminal phpcs --config-set default_standard WordPress

and restart vscode.

ihsanberahim commented 5 years ago

I managed to solve it this way on my Mac

composer global require wp-coding-standards/wpcs phpcs --config-set installed_paths <my user dir>/.composer/vendor/wp-coding-standards/wpcs

in settings:

    "phpcs.executablePath": "<my user dir>/.composer/vendor/bin/phpcs",
    "phpcs.standard": "WordPress",