- Published on
Installing PHP7, Composer and CodeSniffer on Windows 10 (for VSCode)
This post will guide you through clean (non-polluting) Windows 10 installations of PHP 7, Composer and PHP CodeSniffer and will show you how to integrate it all with Visual Studio Code as a bonus.
1. Installing PHP
First download one of the PHP binaries suitable for your system (we used the 64-bit Thread Safe version of PHP 7.1.2 when creating this post but any version will do).
Next:
- Extract the downloaded zip file
- Rename the resultant directory to
php-x64
- Move the renamed directory to
"C:\Program Files"
FYI: may choose any other directory name but this post will assume PHP installed in "C:\Program Files\php-x64"
Enable OpenSSL
PHP will not have OpenSSL enabled by default which will cause Composer errors later on so we might as well enable it right now by opening a Command Prompt (with elevated admin permissions) and running:
cd "C:\Program Files\php-x64"
cp php.ini-production php.ini
Close the Command Prompt and open "C:\Program Files\php-x64\php.ini"
with your editor to uncomment the following lines:
extension_dir = "ext"
extension=php_openssl.dll
2. Installing Composer
Download and run the Composer Windows installer.
Make sure that the installer detects your PHP installation before completing the installation:
Good to know:
- The composer binary is found in
"C:\ProgramData\ComposerSetup\bin"
- The
PATH
(system) environment variable has been updated to include the above bin directory
Change the global install path
By default Composer will install global packages into C:\Users\%username%\AppData\Roaming
which can become quite cluttering so we change it.
Create new directory
"C:\Program Files\php-x64\composer"
Open the system control panel by pressing
WINDOWS KEY
+PAUSE/BREAK KEY
Select
Advanced system Settings
Select
Environmental variables
Under
System Variables
, pressNew
and enter the following settings:
- Select
OK
to round up this step
Add vendor bin directory to PATH
To make sure your system will be able to find composer installed binaries like phpcs
we will need to add the global vendor directory to the system PATH
environment variable.
Open the system control panel (WINDOWS KEY +PAUSE/BREAK)
Select
Advanced system Settings
Select
Environmental variables
Under
System Variables
, select existingPath
variableSelect
Edit
Select
New
and enterC:\Program Files\php-x64\composer\vendor\bin
like shown below:
- Press
OK
trice to apply the setting
Make sure to close all open any CMD boxes or they will not be aware of the new environment variable.
3. Installing PHP_CodeSniffer
Open a Command Prompt (without elevated permissions) and run:
composer global require "squizlabs/php_codesniffer=*"
phpcs --version
4. Installing additional Coding Standards
Installation instructions will differ per additional coding standard so we will just use cakephp-codesniffer as an example here.
Open your global C:\Program Files\php-x64\composer\composer.json
file and manually add the cakephp-sniffer package so it looks like this:
{
"require": {
"squizlabs/php_codesniffer": "*",
"cakephp/cakephp-codesniffer": "2.*"
}
}
Now open a Command Prompt (without elevated permissions) and run the following commands to install the coding standard and make the path known to phcs:
composer global update
phpcs --config-set installed_paths "C:\Program Files\php-x64\composer\vendor\cakephp\cakephp-codesniffer"
phpcs --config-show
There is no need to configure the default coding standard as this will be handled on a per-project basis inside Visual Studio Code but you are free to do so anyways by running:
phpcs --config-set default_standard CakePHP
FYI: all Sniffer settings also in C:\Program Files\php-x64\composer\vendor\squizlabs\php_codesniffer\CodeSniffer.conf
5. Reboot your computer
To be absolutely sure all programs and services are aware of the new environment variables you should now reboot your computer. If you are not using Visual Studio Code you may also skip the rest of this tutorial.
6. Integrating Composer with VSCode
Ioannis Kappas has created a very neat VSCode composer plugin that allows you to lint composer.json
files. To set it up:
Open Visual Studio Code
Press CTRL+P
Paste
ext install Composer publisher:"Ioannis Kappas"
Install the plugin by clicking that green button
Reload the window by clicking that blue button to activate the plugin
Open the settings window (File > Preferences > Settings)
Add the following line to the
USER SETTINGS
section:
"composer.executablePath": "c:\\ProgramData\\ComposerSetup\\bin\\composer.bat"
Verify
To verify the plugin is functioning properly:
Open a
composer.json
file in Visual Studio CodePress F1
Type
validate
Select
Composer: Validate
and pressing enterIf things went well you should see something like this:
7. Integrating PHP CodeSniffer with VSCode
The VSCode phpcs plugin is another great add-on by Ioannis Kappas and allows you to check your code against various coding standards. To set ip up:
Open Visual Studio Code
Press CTRL+P
Paste
ext install phpcs publisher:"Ioannis Kappas"
Install the plugin by clicking that green button
Reload the window by clicking that blue button to activate the plugin
Please note that code sniffing will be active from this point on.
Configure
Even though you are free to use your own configuration strategy we will configure the plugin to disable code sniffing by default, basically requiring you to enable it on a per project/VSCode workspace basis.
To disable sniffing on the global level:
Open Visual Studio Code
Open the settings window (File > Preferences > Settings)
Add the following line to the
USER SETTINGS
section:"phpcs.enable": false
To enable code sniffing for your project/workspace:
Open Visual Studio Code
Open one of your projects/workspaces
Open the settings window (File > Preferences > Settings)
Add the following lines to the
WORKSPACE SETTINGS
section:"phpcs.enable": true, "phpcs.standard": "CakePHP", "phpcs.ignore": "config, webroot"
Please note that the ignore part is optional and that non-existing directories/files will cause errors.
Verify
To verify the plugin is functioning properly:
Open one of your projects with Visual Studio Code
Open one of your
.php
filesMake a change that will trigger a sniffer violation
Save the file (realtime sniffing not supported yet)
You should see a red tilde (
~
) explaining the sniff violation when hovered:
- The
Problems
window (View > Problems) should display a summary of all phpcs violations: