Visual Studio Code: Unable to locate phpcs - php

I am facing this issue in Visual Studio Code. I have already tried reinstalling the phpcs extension but still facing this issue.
Unable to locate phpcs
Please add phpcs to your global path or use the composer dependency manager to install it in your project locally in Visual studio Code

I had the same issue, I did the following to fix it:
Install the phpcs by using composer with composer global require squizlabs/php_codesniffer
Press Command + , (Click Code -> Preferences -> Settings)
Select User Settings and locate 'PHP CodeSniffer'
Scroll to 'Executatble Path' and put
/Users/your-username/.composer/vendor/bin/phpcs
This fixed the issue for me, I hope it does for you too. Cheers!

open the project in the terminal and run this command
composer global require squizlabs/php_codesniffer
Hope that helps!

I think uninstalling it does not solve the issue.
i had the same issue. So,
Go to extensions and install the plugin...."phpcs".
Search for the extension if you had install it.
Disable the extension.
it resolved the issue in my VS code.

On Debian based systems run as root:
apt-get install php-codesniffer

From a GitHub comment.
If someone still needs help about this "Unable to locate phpcs ..."
error message, try this: you just need to install phpcs globally
via Composer.
Install Composer from this resource if you don't have it,
After Composer is installed, open a command prompt wherever you want and run this command
composer global require squizlabs/php_codesniffer

My solution without the need to install php_codesniffer globally.
Just go to VSCode settings and define the local path ./vendor/squizlabs/php_codesniffer/bin/phpcs
Or you can set the path directly in your settings.json
"phpcs.executablePath": "./vendor/squizlabs/php_codesniffer/bin/phpcs"

In my case and in Windows 10, composer located at:
C:\Users\{user_name}\AppData\Roaming\Composer\vendor\bin\
and when i added this to VSCode settings at PHP CodeSniffer section this solve my problem.

I had the same problem and i did this to fix it in my windows:
You will Download the Composer (From here)
Go to Vs code and open your terminal and run this command
composer global require squizlabs/php_codesniffer
Go to (settings--> User Settings), then search PHP CodeSniffer
Scroll to Executatble Path and type
\Users\{user_name}\AppData\Roaming\Composer\vendor\bin\phpcs
and restart VS code.
This works For me, I hope it Works for you too :D

paste the following in your terminal for system-wide installation
composer global require squizlabs/php_codesniffer
This stopped the error message from popping up in mine.

Install PHP Sniffer
instead of phpcs
PHP Sniffer worked on the fly without any extra configuration
phpcs could not recognize my commands, despite a configured PATH, Composer & PHPCS installed.
The commands were recognized in the terminal, but not by the plugin
(under windows 10)

For anyone else still struggling with this on Windows 11, I had to put double backslashes in the path, and link to the *.bat file instead (for both phpcs and phpcbf), in settings.json:
C:\\Users\\user-name\\AppData\\Roaming\\Composer\\vendor\\squizlabs\\php_codesniffer\\bin\\phpcs.bat
I had run all the global installation commands, etc, as described by all the other comments but still no joy in VS Code. I also tried without the phpcs filename (ie, just ending in \\bin), and ending in phpcs without the file extension, but those didn't work. I had also set the PATH to phpcs and phpcbf in my environment variables.

I had the same issue on my Mac in Visual Studio Code (I used PEAR to install PHPCS).
I do not know why but PHPCS couldn't find autoload.php of CodeSniffer.
You can try to type on terminal command line:
$ phpcs
I got an error:
Warning: include_once(PHP/CodeSniffer/autoload.php): failed to open
stream: No such file or directory in /Users/david/pear/bin/phpcs on
line 14
Warning: include_once(): Failed opening 'PHP/CodeSniffer/autoload.php'
for inclusion (include_path='.:') in /Users/david/pear/bin/phpcs on
line 14
Solution:
Open Pear Bin folder in your user folder, e.g. in my case: /Users/david/pear/bin/ in Finder,
Find phpcs file inside and edit it (e.g. in Visual Studio Code :),
You will find these lines there:
if (is_file(__DIR__.'/../autoload.php') === true) {
include_once __DIR__.'/../autoload.php';
} else {
include_once 'PHP/CodeSniffer/autoload.php';
}
Add two new lines there with the proper location of CodeSniffer/autoload.php file (place it before } else { line and modify the path according to your needs - replace "david" with your user name...):
} else if (is_file('/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php')) {
include_once '/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php';
So the final code of the condition is:
if (is_file(__DIR__.'/../autoload.php') === true) {
include_once __DIR__.'/../autoload.php';
} else if (is_file('/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php')) {
include_once '/users/david/pear/share/pear/PHP/CodeSniffer/autoload.php';
} else {
include_once 'PHP/CodeSniffer/autoload.php';
}
Then save it and restart Visual Studio Code. That's it.

I had the same problem with Drupal, the solutions is specify coding standards.
So create phpcs.xml and enable the next options.

"phpcs.standard": "PSR2",
"phpcs.enable": true,
"phpcs.executablePath": "/home/<username>/.config/composer/vendor/squizlabs/php_codesniffer/bin/phpcs",
"phpcs.autoConfigSearch": true

I had this same error and I noticed that phpcs was working from the command line but not working in vscode. I had to read the linter.js file (of the vscode extension) to see where this error is coming from. This fix is more of an info.
So, to fix this: First, run the command phpcs --version and check if the command does not print any warning or error before outputting a line similar to PHP_CodeSniffer version x.x.x (stable) by Squiz (HTTP://www.squiz.net).
If any error or warning (usually unrelated to phpcs) is thrown before the line showing the phpcs version, please go fix that warning or error first. According to linter.js, it expects that the first output (when phpcs.bat is executed) must start with the PHP_CodeSniffer version string. Thus, If anything should print before the PHP_CodeSniffer version ...., then it throws the error.
So, You MUST fix any warning or error printed before the phpcs version if you want phpcs to work in VSCode. I hope this fix works for you. cheers!

I struggled with all these answers and I think it may be confusing to someone who is new to composer on Windows 10
This gives you better understanding of paths and Windows Environment Variables settings. However, it also includes installing php7 and integration with cakephp which you might want to skip and focus on (Composer and phpcs)
I found the fix at https://www.bravo-kernel.com/2017/03/installing-php7-composer-and-codesniffer-on-windows10-for-vscode/
And it had everything I needed to get phpcs working perfectly.
PROCEEDURE:
Download Composer for Windows 10 Here
Install Composer on Windows 10
Open Command Prompt(cmd.exe) and type "composer" and hit the enter key (Just search the windows start for "cmd")
Microsoft Windows [Version 10.0.19042.1165]
(c) Microsoft Corporation. All rights reserved.
C:\Users\systemuser>composer
If composer is properly installed you will get a response with something like this
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.1.5 2021-07-23 10:35:47
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
-d, --working-dir=WORKING-DIR If specified, use the given directory as
working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for
normal output, 2 for more verbose output and 3 for debug
Available commands:
(It continues)
If you do not intend to change the default directory you can now install phpcs otherwise you can use instructions from the link above to set a new executable path. The default composer directory is
C:/Users/systemuser/AppData/Roaming/Composer
Now in Command prompt add the following command (Global installation):
composer global require squizlabs/php_codesniffer
Here is what it will look like:
C:\Users\systemuser>composer global require squizlabs/php_codesniffer
Changed current directory to C:/Users/systemuser/AppData/Roaming/Composer
Using version ^3.6 for squizlabs/php_codesniffer
./composer.json has been updated
Running composer update squizlabs/php_codesniffer
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
C:\Users\systemuser>
Note: Nothing installed because I already installed phpcs. Also you can see that composer switched to the default directory, if this doesn't happen you need to navigate to the appropriate directory with "cd newpath" before installing phpcs:
C:\Users\systemuser>cd AppData\Roaming\Composer
The result is:
C:\Users\systemuser\AppData\Roaming\Composer>
You have completed the installation so verify the app path by navigating the path
Now you can copy the link to the phpcs file and paste in VSCode (If you changed your composer bin path then you need to copy the appropriate directory). It should look like this
C:\Users\sytemuser\AppData\Roaming\Composer\vendor\bin\phpcs
Now phpcs should work fine after installing the extension!

According to the best answer 👆
In my case(in Windows 10), the composer located at:
C:\Users\user-name\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\bin

Filling in the Executatble Path in VSCode settings for PHPCS will work if you have it installed through composer globally. You can also set it for individual project workspaces if you have a PHPCS configured for your project. To do that, create a workspace file through File->Save Workspace As and place this in your setting object:
"phpcs.enable": true,
"phpcs.standard": "WordPress", // or another standard
"phpcs.executablePath": "relative/path/to/vendor/bin/phpcs",
"phpcs.showWarnings": true,
"phpcs.composerJsonPath": "composer.json",
"phpcs.errorSeverity": 5,
"php.suggest.basic": true
Here is a link to a resource describing the process more in depth.

You stated that you already uninstalled it, so i'm assuming your trying to get rid of the extension and error.
How to fix:
Uninstall extension.
Delete the entire phpcs folder from the VS Code folder.
From the VS Code Docs:
Q: Where are extensions installed?
A: Depending on your platform, the global location is in the following folders:
Windows %USERPROFILE%\.vscode\extensions
Mac ~/.vscode/extensions
Linux ~/.vscode/extensions

Related

Configuring Remote (WSL) Composer in PhpStorm

I'm trying to configure a PhpStorm project to utilize both remote PHP and a remote Composer. Remote being my Windows Subsystem for Linux (WSL) installation. However, when I initiate any Composer commands from PhpStorm, I get errors. And unfortunately, the errors are not very indicative of the cause or offer a link for additional details.
Errors received in my "Event Log":
12:21 PM Composer
Failed to install packages for ./composer.json.
Show in Log
And in my "Composer Log":
install --no-interaction --no-ansi
/bin/sh: 1: composer: not found
Failed to install packages for ./composer.json.
So far, I've followed the instructions located at:
https://www.jetbrains.com/help/phpstorm/how-to-use-wsl-development-environment-in-product.html
https://www.jetbrains.com/help/phpstorm/configuring-remote-interpreters.html
I also followed the answer at this post to set WSL as my default terminal:
How to use WSL as default terminal in WebStorm or any other JetBrains' products?
In my WSL installation, composer is located at /home/<user>/.local/bin/composer and is included in my path upon login in my /home/<user>/.profile file. And I was sure to restart PhpStorm after doing so to ensure it has the updated path. Additionally, running composer install within my project, on WSL works without issue.
I'm not sure how to determine which terminal or composer file PhpStorm is using. Either it's still trying to find and use a composer on my host Windows install (which doesn't exist) or I'm missing a path which I have to map for WSL.
Any idea how I can obtain more details as to what I have misconfigured?
I figured out my issue.
When in Settings -> PHP -> Composer, in the "Execution" section. I was already selecting "Remote Interpreter", but the "Composer executable" field requires an absolute path. I was simply supplying the name of the executable since the field is asking for the executable.
I just figured it would utilize my $PATH to resolve the executable, but this isn't using an SSH session so it wouldn't have my custom $PATH values. So it all makes sense.

"Unable to locate phpcs" error in Visual Studio Code

Can someone help me solve a small problem with Windows? I've always used Linux or Apple and have never had this problem.
Issue Type: Bug
phpcs: Request workspace/configuration failed with message: Unable to locate phpcs. Please add phpcs to your global path or use composer dependency manager to install it in your project locally.
VS Code version: Code 1.29.1
OS version: Windows 10
As you say it says phpcs not located which can mean one of two things either you don't have phpcs installed or you haven't specified the path in VS Code
If you do have it installed you may follow these instructions.
Press CTRL + P to open the VS Quick Dialog.
Type ext install phpcs to find the extension.
Press Enter.
Restart VS Code when prompted to.
If you haven't installed it yet.
1. Install Composer.
2. Require PHPCs by typing
composer global require squizlabs/php_codesniffer
in the terminal.
3.Follow steps above.
Information found at Visual Studio

How do I fix the following phpcs error in Visual Studio Code?

I am new to VS code. I am trying to work with PHP, but I keep getting this notification.
phpcs: Request workspace/configuration failed with message:unable to
locate phpcs. please add phpcs to your global path or use composer
dependency manager to install it in your project locally.
Intellisense doesn't work for PHP too.
I am using a Windows system. I tried to download and install Composer, but I am still getting the problem as shown in the screenshot.
]1
I have some extensions installed which are visible in the screenshot, but the issue persists.
The easiest way is to use composer to install phpcs globally and symlink the binary into your path;
Assuming you have composer installed and are using osx or linux (if not, follow instructions from here: composer) then install phpcs globally: open your terminal and type:
composer global require "squizlabs/php_codesniffer=*"
You will then need to make sure phpcs is in your path. The easiest way is to symlink into /usr/local/bin. open your terminal and type:
sudo ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin/phpcs

Getting [ErrorException] - installing yii2 using composer

I am trying to install Yii2 on WAMP using composer.
c:\Users\username>php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.0
I am getting the following error when I run the command which is given above.
[ErrorException]
Argument 1 passed to Fxp\Composer\AssetPlugin\Repository\BowerRepository::createVcsRepositoryConfig() must be of the type array, null given, called in C:\Users\username\AppData\Roaming\Composer\vendor\fxp\composer-asset-plugin\Repository\AbstractAssetsRepository.php on line 136 and defined
Can someone help solving this error? But, it is created a folder named "basic".
I have copied this folder to D:\wamp\www and when I access localhost/basic/web/ from browser, I am getting the following error.
ReflectionException
Class yii\debug\Module does not exist
I am following the instructions to install yii2 from http://www.yiiframework.com/download/
Please assist me to resolve these issues.
Thanks
First windows does not have a curl processor like unix so you need to use the other option for installing Composer
php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
For this to work you need the php.exe processor to be on your path, so you have 2 options here. Either add the c:\wamp\bin\php\phpx.y.z folder to your PATH or my prefered option write yourself a little .cmd file which will do it for you like this
addphp.cmd
PATH=%PATH%;c:\wamp\bin\php\phpx.y.z
Put this file in a folder already registered on your path so you can run it from anywhere in a command window.
Now you will have to edit the \wamp\bin\php\phpx.y.z\php.ini file. This is similiar to the one used by php code run through the Apache web server but is only used by the PHP CLI (Command Line Interpreter)
Make sure the extension php_curl is uncommented or the above line wont work i.e. remove the ; comment symbol
extension=php_curl.dll
So now run a command window, cd into the folder that you want composer installed into and run the command above, then follow the rest of the install instructions on Install instructions
EDIT: Thank you for reminding me.
This error happens because of invalid json on plugin you or application requested (npm or bower).
The link here suggests you to run:
composer global update.
This will update composer cache. Hope it helps.

phpunit run problems in phpstorm, Cannot create phar

I am running phpStorm 7.1.3 version on Windows 7 and I have done everything accordingly to install composer and then phpunit. Now I have it in vendors directory. But every time I run my program which is a simple Yii application I get the same error.
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with
message 'Cannot create phar
'C:/wamp/www/myproject/vendor/bin/phpunit', file extension (or
combination) not recognised or the directory does not exist' in
C:\Users\myUser\AppData\Local\Temp\ide-phpunit.php:177
Please help, I am stuck here already two days and cant get this working, almost changed and reinstalled everything but is not helping.
I didn't use pear, I downloaded everything through the phpStorm. I downloaded composer, and after that through phpStorm composer I downloaded phpUnit 3.7.37.
Here is the screenshot of my settings for composer
Here is the content of my composer.json
{
"require": {
"phpunit/phpunit": "3.7.37"
}
}
Here is the screenshot of my settings for PHPunit
On your 2nd screenshot (PHPUnit settings):
Why did you choose PHAR option if you are using COMPOSER?
Why did you point to .../bin/phpunit (even if you choose 3rd option .. you should point it to a PHAR file then)?
Therefore:
For composer installations you should use 2nd ("Use custom loader") option.
The edit box -- you should point it to your autoloader script (yes -- composer autoloader script).
Related (similar issue): https://stackoverflow.com/a/17316104/783119
If you are not using composer, but the 'Path to phpunit.phar' option and still receive this error, just make sure the path you specify ends with a .phar (usually ends with phpunit.phar) I was seeing this same error but because I had the path pointing to my command-line alias, not the actual .phar file, it gets hung up on the improper extension and bails.
I was trying to run phpUnit on zend framework 1.12.9 and had the same problem - solved it by changing path to phpunit.phar to use custom loader.
Then You have to point it to the generated autoload.php by composer.
In my case that was
C:\wamp\www\phpu3\vendor\autoload.php
Later, the important thing to do was to point the bootstrap file from tests.
When you download the phpunit.phar file using the wget command, following the instruction on the getting started manual -
https://phpunit.de/getting-started/phpunit-4.html
the "phar" extension is being removed, and PHPSTORM has issues to interrupt this file. I've changed the extension:
mv phpunit phpunit.phar
make sure you update the path to phpunit.phar file in the IDE setting accordingly

Categories