Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check PHP code against a specific set of coding conventions?

I have a PHP library that I'm planning to submit to the PEAR database. In order to do this, the library needs to follow the PEAR Coding Standards.

Is there a tool that can check my code to make sure it follows the standards completely?

like image 221
Nathan Osman Avatar asked Sep 28 '10 04:09

Nathan Osman


People also ask

What does PHP Code Sniffer do?

PHP Code Sniffer (PHPCS) is a package for syntax checking, available from PEAR. It can check code against defined rules covering anything from whitespace through doc comments to variable naming conventions and beyond.

How do you use a code sniffer?

In the Settings dialog, go to Editor > Inspections. From the inspections screen, expand the PHP | Quality tools node and enable “PHP CodeSniffer validation”. In the configuration pane that is now enabled, select “Custom” from the “Coding standard” dropdown, locate the ruleset configuration ( phpcs.

What do you need to know about PHP coding style?

Overview. Code MUST follow a "coding style guide" PSR [PSR-1]. Code MUST use 4 spaces for indenting, not tabs. There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.


2 Answers

http://pear.php.net/manual/en/package.php.php-codesniffer.php

By default, PHP_CodeSniffer will use the PEAR coding standard if no standard is supplied on the command line.

like image 108
B00MER Avatar answered Oct 06 '22 23:10

B00MER


As B00MER mentioned, PHP_CodeSniffer defaults to using the PEAR coding standard, if you don't specify a different one. But you can change what it defaults to - handy if you have an in-house standard (which isn't really a standard then, but anyway) or prefer one of the other standards that ships with PHP_CodeSniffer and don't want to keep specifying them with the --standard argument.

To get a list of what Standards are available to choose from, do this:

$ phpcs -i

And to change the default to Zend:

$ sudo phpcs --config-set default_standard Zend

If you want to check what the default is:

$ phpcs --config-show
like image 45
kguest Avatar answered Oct 07 '22 01:10

kguest