Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to do more with PHPCS

How to do more with PHPCS

Whilst it started as a tool for checking coding standards compliance, PHPCS can be used for much more than that.

On the WordPress.com VIP team, it’s used to help detect potential security and performance issues and maintain the quality of client codebases.

In this talk, we’ll discover what PHPCS is, how it works, what sets of rules are available for it, as well as integration with IDEs like PHPStorm. We'll also see how the partner script PHPCBF can help automatically fix many coding standard violations for you. And we’ll find out how it helps the WordPress.com VIP team protect client sites in real-world situations every day.

Sarah Pantry

May 30, 2019
Tweet

More Decks by Sarah Pantry

Other Decks in Technology

Transcript

  1. PHP CodeSniffer is a pair of scripts that tokenize PHP,

    JavaScript and CSS files to detect and fix violations of a defined coding standard -phpcs Detects violations -phpcbf Fixes violations What is PHPCS
  2. How you get it? You can find PHPCS at https://github.com/squizlabs/PHP_CodeSniffer

    - composer global require "squizlabs/php_codesniffer=*" - pear install PHP_CodeSniffer
  3. There are many sets of standards available for free download.

    -Doctrine -Drupal -Joomla -Magento -Symfony -WordPress -WordPress-VIP -Yii -And More Available Standards
  4. How it works Coding Standards - Specified by an XML

    file - Can extend a parent standard - Specifies which rules apply - Assigns or overrides severity and messages for individual rules
  5. Sniffs - Defined by a PHP file - Lists supported

    languages eg PHP, JS or CSS - Registers for specific tokens - Defines tests for compliance - Adds errors, warnings and descriptive messages How it works
  6. - Security - escaping, sanitizing, nonces, injection - Performance -

    slow queries, uncached functions, enqueing, remote calls - Platform issues - disk writes, Memcached, Batcache or Varnish blockers - Settings alterations - ini_set, error_reporting - Unused and undefined variables - usually caused by typos - Unnecessary code - blocks of commented out or debug code - Adding more all the time What we look for