PHP CodeSniffer (PHPcs)

From HaFrWiki
Revision as of 14:16, 15 May 2021 by Hjmf (talk | contribs)
Jump to: navigation, search

PHP CodeSniffer (PHPcs) is a useful tool for PHP developers [1].

Introduction

PHPcs is a PHP5 script that tokenises and "sniffs" PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.

Installation

The preferred way to install PHPcs is PHP composer.

$ composer global require "squizlabs/php_codesniffer=*"


Bug Workarounds

Some users experience warning about not finding the CLI.php file after installing and trying the tools.

  • Search for the location of the phpcs file.
  • open the file with a normal developer text editor.
  • Adjust

<syntaxhighlight lang="php" line start=1> if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {

  include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';

else {

  include_once 'PHP/CodeSniffer/CLI.php';

} </syntaxhighlight> Into something like: <syntaxhighlight lang="php" line start=1> if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {

  include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';

} else if (is_file('/usr/local/pear/share/pear/PHP/CodeSniffer/CLI.php')) {

  include_once '/usr/local/pear/share/pear/PHP/CodeSniffer/CLI.php';

} else {

  include_once 'PHP/CodeSniffer/CLI.php';

} </syntaxhighlight>

See also

top

Reference

top

  1. PHP CodeSniffer, PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.