Post Reads 24 reads

Installation Installation

Install PHP Code Sniffer Install PHP Code Sniffer

composer global require squizlabs/php_codesniffer

You can see something similar:

λ composer global require squizlabs/php_codesniffer
Changed current directory to C:/Users/ADMIN/AppData/Roaming/Composer
Using version ^3.5 for squizlabs/php_codesniffer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing squizlabs/php_codesniffer (3.5.5): Downloading (100%)
Writing lock file
Generating autoload files

Install WordPress Coding Standerds Install WordPress Coding Standerds

composer global require wp-coding-standards/wpcs

Like:

λ composer global require wp-coding-standards/wpcs
Changed current directory to C:/Users/ADMIN/AppData/Roaming/Composer
Using version ^2.3 for wp-coding-standards/wpcs
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing wp-coding-standards/wpcs (2.3.0): Downloading (100%)
wp-coding-standards/wpcs suggests installing dealerdirect/phpcodesniffer-composer-installer (^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.)
Writing lock file
Generating autoload files

Create phpcs.xml.dist file. Create phpcs.xml.dist file.

Now we need to create the PHPCS file and add the rules which allow:

<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Plugins">
    <description>Generally-applicable sniffs for WordPress plugins</description>

    <config name="testVersion" value="5.3-"/>

    <!-- Check all PHP files in directory tree by default. -->
    <arg name="extensions" value="php"/>
    <file>.</file>

    <!-- Show sniff codes in all reports -->
    <arg value="s"/>

    <exclude-pattern>*/node_modules/*</exclude-pattern>
    <exclude-pattern>*/vendor/*</exclude-pattern>

</ruleset>

PHPCS sniffs. PHPCS sniffs.

PHPCS sniffs are the rulesets which are required to install to check the code against that ruleset.

When we try to exeucte the command phpcs then we get such error.

λ phpcs
ERROR: No sniffs were registered

Run "phpcs --help" for usage information

Install & Use the PHPCompatibilityWP sniff Install & Use the PHPCompatibilityWP sniff

Add below line into the file phpcs.xml.dist

    <rule ref="PHPCompatibilityWP"/>

and then now try to use phpcs

You’ll get the error if you have not installed PHPCompatibilityWP sniff.

λ phpcs
ERROR: Referenced sniff "PHPCompatibilityWP" does not exist

Run "phpcs --help" for usage information

Lets install PHPCompatibilityWP ruleset with below command.

composer global require phpcompatibility/php-compatibility

LIke:

λ composer global require phpcompatibility/php-compatibility
Changed current directory to C:/Users/ADMIN/AppData/Roaming/Composer
Using version ^9.3 for phpcompatibility/php-compatibility
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing phpcompatibility/php-compatibility (9.3.5): Downloading (100%)
phpcompatibility/php-compatibility suggests installing dealerdirect/phpcodesniffer-composer-installer (^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.)
phpcompatibility/php-compatibility suggests installing roave/security-advisories (dev-master || Helps prevent installing dependencies with known security issues.)
Writing lock file
Generating autoload files
composer require --dev phpcompatibility/phpcompatibility-wp:* &amp;&amp; composer install

Like:

λ composer require --dev phpcompatibility/phpcompatibility-wp:* &amp;&amp; composer install
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
PHP CodeSniffer Config installed_paths set to ../../phpcompatibility/php-compatibility,../../phpcompatibility/phpcompatibility-paragonie,../../phpcompatibility/phpcompatibility-wp,../../wp-coding-standards/wpcs
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
PHP CodeSniffer Config installed_paths set to ../../phpcompatibility/phpcompatibility-paragonie,../../phpcompatibility/phpcompatibility-wp,../../wp-coding-standards/wpcs,../../phpcompatibility/php-compatibility

====

composer global require dealerdirect/phpcodesniffer-composer-installer
λ composer global require dealerdirect/phpcodesniffer-composer-installer
Changed current directory to C:/Users/ADMIN/AppData/Roaming/Composer
Using version ^0.6.2 for dealerdirect/phpcodesniffer-composer-installer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing dealerdirect/phpcodesniffer-composer-installer (v0.6.2): Downloading (100%)
Writing lock file
Generating autoload files
PHP CodeSniffer Config installed_paths set to ../../phpcompatibility/php-compatibility,../../wp-coding-standards/wpcs

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: