https://github.com/xenioushk/phpcs_101
This guide provides a comprehensive guideline for setting up PHPCS for a WordPress theme or plugin. You will also get instructions for setting up the PHPCS Visual Studio Code extension setup.
https://github.com/xenioushk/phpcs_101
phpcs phpcs-standard
Last synced: 5 months ago
JSON representation
This guide provides a comprehensive guideline for setting up PHPCS for a WordPress theme or plugin. You will also get instructions for setting up the PHPCS Visual Studio Code extension setup.
- Host: GitHub
- URL: https://github.com/xenioushk/phpcs_101
- Owner: xenioushk
- Created: 2024-09-03T06:36:33.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-27T10:37:20.000Z (over 1 year ago)
- Last Synced: 2025-08-11T17:53:58.190Z (11 months ago)
- Topics: phpcs, phpcs-standard
- Homepage: https://bluewindlab.net
- Size: 315 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHPCS Setup
This guide provides a comprehensive guideline for setting up PHPCS for a WordPress theme or plugin. You will also get instructions for setting up the PHPCS Visual Studio Code extension setup.
## Requirements
You need to install the following softwares on your machine.
- [composer](https://getcomposer.org/)
- [npm](https://www.npmjs.com/)
- [node](https://nodejs.org/en)
## Steps
1. Navigate to the project root directory.
2. Open composer.json file and add/update the codes.
```json
"require": {
"php": ">=7.4|^8.0",
"composer/installers": "^1.0.12"
},
```
```json
"require-dev": {
"wp-coding-standards/wpcs": "^3.1",
"phpcompatibility/phpcompatibility-wp": "2.1",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"squizlabs/php_codesniffer": "^3.10"
},
```
```json
"config": {
"allow-plugins": {
"composer/installers": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"johnpbloch/wordpress-core-installer": true
}
}
```
3. Run the command to install composer packages.
```bash
composer update
```
4. You will now see a bin folder inside the `vendor` directory.

5. Next, open the `package.json` file and add this code.
```json
"lint:php": "vendor/bin/phpcs",
"lint:php:fix": "vendor/bin/phpcbf",
```

6. Now, copy/download the `phpcs.xml` file from this [link](phpcs.xml) and paste it to the project root directory.

7. All done. Now, we can run this command to check the php linting errors and fix them.
```bash
npm run lint:php
npm run lint:php:fix
```
**Output:**

## Ignoring the issue
Sometimes we need to ignore some of the error highlights.
Just add the `// phpcs:ignore` text after the line and issue will be fixed.
```php
function init_bwl_advanced_faq_manager() { // phpcs:ignore
new BWL_Advanced_Faq_Manager();
}
```
## Install VSCode PHP Sniffer extension
1. Search and install visual studio code PHP Sniffer & Beautifer extension.

2. Navigate to settings of the visual studio code and search for `.json` file. Click on the `Edit in settings.json`link and add following lines of code.

```json
{
"phpSniffer.autoDetect": true,
"phpSniffer.run": "onSave", // Runs PHPCS on save
"[php]": {
"editor.defaultFormatter": "valeryanm.vscode-phpsab",
"editor.insertSpaces": false,
"editor.tabSize": 4
},
"files.trimTrailingWhitespace": false,
"editor.renderFinalNewline": "on",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": false,
"files.eol": "\n" // Enforces Unix-style line endings
}
```
3. Test a particular file.
```bash
./vendor/bin/phpcs --standard=WordPress bwl-services-api.php
```
4. Fix a particular file.
```bash
./vendor/bin/phpcbf bwl-services-api.php
```
That's it.
## PHP Function comment parameters.
In PHPDoc comments, there are several fields you can use to provide additional information about the function. Here are some commonly used fields:
```php
@param: Describes the parameters the function accepts.
@return: Describes the return value of the function.
@throws: Describes any exceptions the function might throw.
@deprecated: Indicates that the function is deprecated and should not be used.
@see: Provides a reference to another function or resource.
@link: Provides a URL to a related resource.
@author: Indicates the author of the function.
@version: Indicates the version of the function.
@since: Indicates the version since the function is available.
@example: Provides an example of how to use the function.
```
### Acknowledgement
- [bluewindlab.net](https://bluewindlab.net)
- [composer](https://getcomposer.org/)
- [npm](https://www.npmjs.com/)