An open API service indexing awesome lists of open source software.

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.

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.

![phpcs bin directory](/previews/composer/phpcs_bin_folder.jpg)

5. Next, open the `package.json` file and add this code.

```json
"lint:php": "vendor/bin/phpcs",
"lint:php:fix": "vendor/bin/phpcbf",
```

![Update package.json file](/previews/npm/phplintcode.jpg)

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

![Update package.json file](/previews/npm/phplintcode.jpg)

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:**

![PHPCS linting report](/previews/phpcs/phpcs_log.jpg)

## 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.

![install_php_sniffer_and_beautifer_vscode_extension](/previews/phpcs/install_php_sniffer_and_beautifer_vscode_extension.jpg)

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.

![create workspace based settings.json file](/previews/phpcs/edit_settings.json_file.jpg)

```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/)