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 //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!
How to install phpcs in Visual Studio Code on Windows 10 also remove the error phpcs: Unable to locate phpcs. Please add phpcs to your global path or use composer dependency manager to install it in your project locally.
Install Composer
- Step 1 - Download it.
- Install it, the instructions are on the download page.
- Make it globally accessible in your PATH.
- In your System Environment Variables make sure Composer is in your PATH.
- i.e. C:\ProgramData\ComposerSetup\bin;
PHP
Download PHP 7 and save to C:\php7
PEAR
Using Git Bash:
cd C:
cd php7/pear
#Download the go-pear using CURL
curl -OL //pear.php.net/go-pear.phar
Now open Windows Command Prompt as an Administrator
#Run PEAR
php go-pear.phar
As it states at the end of the install instruction, install Reg edits.
PHP Codesniffer
pear install PHP_CodeSniffer
PHPCS
Close all Command Prompts and open a new one with Admin Rights
composer global require "squizlabs/php_codesniffer=*"
phpcs --version
Visual Studio Code
Restart your Visual Studio Code and you should now not get the error: phpcs: Unable to locate phpcs. Please add phpcs to your global path or use composer dependency manager to install it in your project locally. Find a PHP file and incorrectly format it and it should now alert you.
Notes
Issue: The language server needs at least PHP 7 installed. Version found: Fix: Add C:\php7 to System Environment PATH File->Preferences->Settings->Add "php.validate.executablePath": "C:\\php7\\php.exe" Issue: The openssl extension is required for SSL/TLS protection but is not available. If you can not enable the openssl extension, you can disable this error, at your own risk, by setting the 'disable-tls' option to true. Fix: Open php.ini
Change
;extension=php_openssl.dll
#TO
extension=php_openssl.dll
#AND CHANGE
;extension_dir = "ext"
#TO
extension_dir = "ext"