Setting Up A PHP CodeSniffer For WordPress using Composer

Setting Up A PHP CodeSniffer For WordPress using Composer

A PHP CodeSniffer is a program that helps developers keep code clean and uniform or in sync across teams. In PHP, you can easily set up a code sniffer program to help lint and watch your files while you work and ensure that you keep to standards with everyone in the team.

Let’s get started.

Step 1. Install Composer

The first thing would be to ensure you have Composer installed on your computer. No point going forward if you don’t have that installed. You can install Composer on your computer using Homebrew like so:

brew install composer        

Step 2.?Install PHPCS

Next would be to install PHPCS (PHP CodeSniffer) globally on your Computer terminal using Composer like so:

composer global require squizlabs/php_codesniffer        

Step 3. Expose PHPCS to system $PATH

To ensure you can access PHPCS globally or anywhere on your computer, you need to expose it to the PATH environment variable on your computer by adding the following line to the .zshrc or .bashrc file found on your root folder:

export PATH="$PATH:$HOME/.composer/vendor/squizlabs/php_codesniffer/bin"        

If you're not sure where to find your .zshrc or .bashrc file, you can type the following command on your terminal:

cd ~ && ls -al        

It will take you to your root folder and display all the files in your root folder (including the hidden ones).

Step 4. Confirm PHPCS

Once the above step is completed, we need to ensure that the PHPCS is installed properly and accessible on your terminal, type in the following command:

phpcs -i        

You should see something similar to this:

The installed coding standards are MySource, PEAR, PSR1, Squiz, Zend        

Step 5. Install WP Coding Standard

Next, we install WP Coding standards as a global dependency. This will enable us get access to the WP coding standards package via composer like so:

composer global require wp-coding-standards/wpcs        

Step 6. Clone PHP Compatibility Git Repo

We'll also need to clone the PHP Compatibility Git repo into our .composer vendor bin folder like so:

cd ~/.composer/vendor && git clone https://github.com/wimg/PHPCompatibility.git        

Step 7. Configure PHPCS Paths

Now that you have WP Coding Standards and PHP Comptability installed successfully, all that's left is to configure the PHPCS paths like so:

phpcs --config-set installed_paths $HOME/.composer/vendor/PHPCompatibility, $HOME/.composer/vendor/wp-coding-standards/wpcs        

Now run the following command on your terminal:

phpcs -i        

You should see something similar to this:

The installed coding standards are MySource, PEAR, Zend, Squiz, PSR1, PHPCompatibility, WordPress-VIP, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core        

Congratulations!!!

You're now all set up to start using PHPCS to lint your PHP files in your WordPress projects.

How to use PHPCS

To use PHPCS in your WP plugin or theme repo, you have to ensure your WP plugin or theme repo has a phpcs.xml set up with WP rulesets defined.

Here's an example of an Auttomatic's phpcs.xml with WP rulesets defined:

https://github.com/Automattic/_s/blob/master/phpcs.xml.dist        

Copy the code in the above XML file and save it into your own phpcs.xml file in the root path of your WP project. Now, you are ready to run your PHPCS and PHPCBF commands.

To sniff for errors in your WP project, run the following command on your terminal like so:

phpcs        

To fix errors in your PHP files you can type the following command on your terminal like so:

phpcbf        

Hope this helps in your WP projects. Please leave a comment below, thanks.

Chigozie Orunta

Senior WordPress Engineer @ X-Team | Enterprise WP, Plugin Development.

1 年

You’re welcome Kate Bugay. If you have any other problems related to WordPress or software development, pls don’t hesitate to hit me up. I’ll be more than happy to help. ??

Kate Bugay

WordPress Developer

1 年

Thank you SO much for this guide! I've spent a whole day trying to install everything to work with VSCode and the steps here helped me figure out how to do this and what the problem was :) Thank you!

要查看或添加评论,请登录

Chigozie Orunta的更多文章

  • Common Mistakes WP Engineers Make

    Common Mistakes WP Engineers Make

    WordPress is famous for being one of the most popular content management systems (CMS) worldwide, powering millions of…

  • Applying SOLID Principles to WordPress Development

    Applying SOLID Principles to WordPress Development

    SOLID Principles in modern software development refers to the design approach intended in making software code more…

    2 条评论
  • Speed Up Your WordPress Website

    Speed Up Your WordPress Website

    As a website owner, you probably already know how important it is for your site to load fast and efficiently so that…

    1 条评论
  • Creating A Custom Divi Module or Extension Plugin in WordPress

    Creating A Custom Divi Module or Extension Plugin in WordPress

    Divi is one of the most powerful WordPress page builders out there in the market. Its ease of use and WYSIWYG nature…

    4 条评论
  • Building A Custom Gutenberg Block In WordPress

    Building A Custom Gutenberg Block In WordPress

    Gutenberg Blocks in WordPress have been around for a while, ever since the builder was released on the WordPress…

  • Working With WordPress Hooks, How To Create & Use Them.

    Working With WordPress Hooks, How To Create & Use Them.

    WordPress Hooks are one of the most powerful features in theme and plugin development. They enable WP developers to add…

  • Most Useful WordPress Plugins in 2022

    Most Useful WordPress Plugins in 2022

    Plugins are an invaluable tool for today’s WordPress websites. There are extremely few websites that exist today that…

    1 条评论
  • Safeguarding Your WordPress Site

    Safeguarding Your WordPress Site

    WordPress accounts for almost 30% of all websites on the Internet and is one of the most popular CMS (Content…

  • 4 Ways To Style Your React Components

    4 Ways To Style Your React Components

    So you’ve built your first React Component but don’t know how to style it. Well, here are 4 quick ways to style your…

    1 条评论

社区洞察

其他会员也浏览了