... and small improvement to the text about running PHPCS in a git hook. _When this text was originally written, the `--filter=GitStaged` option didn't exist yet, but now it does, it seems like a good pointer to add to the text._ Ref: * https://github.com/squizlabs/PHP_CodeSniffer/issues/3932 (about the repo URL change) * https://github.com/squizlabs/PHP_CodeSniffer/pull/2137 (about the GitStaged filter)
3.6 KiB
anchor
anchor |
---|
code_style_guide |
Code Style Guide
The PHP community is large and diverse, composed of innumerable libraries, frameworks, and components. It is common for PHP developers to choose several of these and combine them into a single project. It is important that PHP code adheres (as close as possible) to a common code style to make it easy for developers to mix and match various libraries for their projects.
The Framework Interop Group has proposed and approved a series of style recommendations. Not all of them relate to code-style, but those that do are PSR-1, PSR-12, PSR-4 and PER Coding Style. These recommendations are merely a set of rules that many projects like Drupal, Zend, Symfony, Laravel, CakePHP, phpBB, AWS SDK, FuelPHP, Lithium, etc. are adopting. You can use them for your own projects, or continue to use your own personal style.
Ideally, you should write PHP code that adheres to a known standard. This could be any combination of PSRs, or one of the coding standards made by PEAR or Zend. This means other developers can easily read and work with your code, and applications that implement the components can have consistency even when working with lots of third-party code.
- Read about PSR-1
- Read about PSR-12
- Read about PSR-4
- Read about PER Coding Style
- Read about PEAR Coding Standards
- Read about Symfony Coding Standards
You can use PHP_CodeSniffer to check code against any one of these recommendations, and plugins for text editors like Sublime Text to be given real-time feedback.
You can fix the code layout automatically by using one of the following tools:
- One is the PHP Coding Standards Fixer which has a very well tested codebase.
- Also, the PHP Code Beautifier and Fixer tool which is included with PHP_CodeSniffer can be used to adjust your code accordingly.
And you can run phpcs manually from shell:
phpcs -sw --standard=PSR1 file.php
It will show errors and describe how to fix them.
It can also be helpful to include the phpcs
command in a git pre-commit hook with the --filter=GitStaged
CLI argument.
That way, code which contain violations against the chosen standard cannot enter the repository until those
violations have been fixed.
If you have PHP_CodeSniffer, then you can fix the code layout problems reported by it, automatically, with the PHP Code Beautifier and Fixer.
phpcbf -w --standard=PSR1 file.php
Another option is to use the PHP Coding Standards Fixer. It will show what kind of errors the code structure had before it fixed them.
php-cs-fixer fix -v --rules=@PSR1 file.php
English is preferred for all symbol names and code infrastructure. Comments may be written in any language easily readable by all current and future parties who may be working on the codebase.
Finally, a good supplementary resource for writing clean PHP code is Clean Code PHP.