PHP_CodeSniffer is an important development tool for ensuring clean and consistent PHP code. It contains two scripts: phpcs and phpcbf. phpcs detects the violation of defined coding standards in PHP, JavaScript, and CSS files. On the other hand, phpcbf, known as PHP Code Beautifier and Fixer, automatically formats and fixes the violation of coding standards. You may read the official GitHub document on PHP_CodeSniffer.
Installation of PHP_CodeSniffer via Composer
The easiest way to install PHP_CodeSniffer is with Composer, a PHP dependency management tool. With Composer, developers only need to declare the libraries their project depends on; it installs and upgrades packages and libraries to the required versions.
If you haven’t installed Composer, you can download the latest version of Composer-Setup.exe to install it on a Windows machine. This is a super-easy method. Set the composer bin directory to the PATH variable so the composer command can be run from any directory on your command line. If everything goes as expected, the composer -V command in your terminal should result in a version of Composer.
You can install PHP_CodeSniffer in the global scope of your system by executing the following command:
composer global require "squizlabs/php_codesniffer=*"
The global installation is likely to create a composer.json file in C:/Users/<Username>/AppData/Roaming/Composer directory. You can find the phpcs and phpcbf files inside the vendor/bin directory of the Composer directory.
Alternatively, you can create and configure the composer.json file in the local working directory like so:
{
"require-dev": {
"squizlabs/php_codesniffer": "^3.0"
}
}
After that, run the composer install command to complete the installation process or the composer update command to update the existing packages. If the installation is correct, you can run the phpcs -h and phpcbf -h commands in your terminal if the vendor/bin directory is in the system PATH environment variable. If the PATH variable is not set up, you can go to the vendor/bin directory and run the command.
Installation of WordPress Coding Standard
The WordPress Coding Standards (WPCS) is the required package for maintaining PHP coding standards and works alongside PHP_CodeSniffer. You need to run the following command to install the WPCS globally on a Windows machine:
composer global require --dev wp-coding-standards/wpcs:"^3.0"
If you need to install the WPCS on a project basis, run the following command:
composer require --dev wp-coding-standards/wpcs:"^3.0"
Installing WPCS allows the phpcs and phpcbf commands to use WordPress as the default value for the standard argument. You can go ahead and follow this WPCS page to learn more.
Command Line Usages of PHPCS and PHPCBF
The PEAR coding standard is PHPCS’s default. You need to configure it to meet WordPress coding standards. You can pass the --standard command line argument to check against WordPress. The command to check for the coding standard of a file is phpcs <path-to-directory/file-name.php>, and a directory is phpcs <path-to-directory>. In addition, if you need to pass WordPress as the coding standard in the command line argument, you can do it like this: phpcs --standard=WordPress <path-to-directory>.
Similarly, you can use phpcbf to correct violations of coding standards automatically. You can check for the coding standard of a file with phpcbf <path-to-directory/file-name.php> and a directory with phpcbf <path-to-directory> commands. In addition, use phpcs --standard=WordPress <path-to-directory> command to pass WordPress as the coding standard.
JSON Configuration of PHPCS and PHPCFB in VS Code
VS Code can be configured to use phpcs and phpcbf. To do this, open VS Code and go to File>Preferences>Settings. Then go to either User or Workspace in Settings, select Edit in settings.json, and click the link to open the JSON file. After opening the settings.json file, edit it with the following content.
{
"php.validate.executablePath": "C:\\wamp64\\bin\\php\\php8.3.6\\php.exe",
"phpcs.enable": true,
"phpcbf.enable": true,
"phpcs.executablePath": "C:\\Users\\xxx\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcs.bat",
"phpcbf.executablePath": "C:\\Users\\xxx\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
"phpcbf.documentFormattingProvider": true,
"phpcbf.onsave": false,
"phpcs.standard": "WordPress",
"phpcbf.standard": "WordPress",
"phpcs.showWarnings": true,
"editor.defaultFormatter": "persoderlind.vscode-phpcbf",
"[php]": {
"editor.tabSize": 4,
"editor.defaultFormatter": "persoderlind.vscode-phpcbf"
},
"files.associations": {
"*.php": "php"
}
}
Tasks in VS code
The Tasks are a powerful feature in VS Code that streamlines your development process and integrates various workflows directly into your coding environment. Tasks automate common workflows and repetitive actions directly from the editor. They allow you to run commands, scripts, or external programs with a single command or shortcut, saving time and reducing the need to switch between the editor and a terminal.
To create Tasks in VS Code, go to File > Preferences > Tasks > Others. This will generate a tasks.json file in the .vscode folder of your project directory. If it is not created within the .vscode folder, open Command Palette by pressing F1 or Ctrl + Shift + P, and select Tasks: Configure Task to generate a tasks.json file. The tasks.json file defines tasks and contains their settings, including the command to run, arguments, and other options. The tasks.json file should include the following code to run the phpcbf command, which automatically fixes violations of WordPress coding standards.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run phpcbf",
"type": "shell",
"command": "phpcbf",
"args": [
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"detail": "Run PHP Code Beautifier and Fixer (phpcbf) on the current file"
}
]
}
The keyboard shortcut Ctrl+Shift+B will run the command mentioned in the tasks.json file.