A coding standard is an agreed set of style and quality rules. The point is not that the rules are perfect, but that everyone follows the same ones, so code looks the same and reviews focus on logic.
You already know the PSR-12 style from Modern PHP Coding Style. This lesson is about tools that enforce standards, so you do not have to remember them.
The standards you will meet
| Standard | Covers |
|---|---|
| PSR-1 | Basic rules (file names, class names) |
| PSR-12 | Detailed coding style (replaces PSR-2) |
| PER Coding Style | The evolving successor to PSR-12 |
| PSR-4 | Autoloading (PSR-4 Basics) |
| WordPress Coding Standards | WordPress style (tabs, snake_case) |
| Symfony / Laravel conventions | Framework-specific style |
EditorConfig
A small .editorconfig file makes every editor use the same indentation and line endings.
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.php]
indent_style = space
indent_size = 4
Style checkers and fixers
| Tool | What it does |
|---|---|
PHP_CodeSniffer (phpcs) | Reports style violations. phpcbf fixes many of them |
| PHP CS Fixer | Rewrites code to match a style automatically |
| Laravel Pint | A friendly wrapper around PHP CS Fixer |
composer require --dev squizlabs/php_codesniffer
vendor/bin/phpcs --standard=PSR12 src/
vendor/bin/phpcbf --standard=PSR12 src/
Static analysis
Static analysers read your code without running it and find mistakes: calling a method that does not exist, passing the wrong type, using an undefined variable, dead code.
| Tool | Notes |
|---|---|
| PHPStan | Very popular. Strictness levels 0 to 9 and a max level |
| Psalm | Similar, with extra type features |
| Rector | Automated refactoring and PHP version upgrades |
composer require --dev phpstan/phpstan
vendor/bin/phpstan analyse src --level=6
Start at a low level and raise it as you clean up. Static analysis is a very cheap way to find bugs before users do.
For example, this code has a bug that no test needs to run to find:
<?php
function total(array $items): int
{
return array_sum($items) . ""; // PHPStan: should return int, string given
}
Syntax check
php -l file.php
Checks a file for syntax errors without running it. A one-liner for a whole project:
find src -name "*.php" -print0 | xargs -0 -n1 php -l
Tests
Style and static analysis do not prove the code is correct. Automated tests (PHPUnit or Pest) do. Even a few tests on your most important logic catch regressions.
<?php
function slugify(string $text): string
{
return trim(preg_replace("/[^a-z0-9]+/", "-", strtolower($text)), "-");
}
// A tiny self-check, the seed of a real test
$cases = [
"Hello World" => "hello-world",
" PHP & MySQL! " => "php-mysql",
"Already-slugged" => "already-slugged",
];
foreach ($cases as $input => $expected) {
echo slugify($input) === $expected ? "ok " : "FAIL($input) ";
}
ok ok ok
Run the checks automatically
| Where | How |
|---|---|
| In your editor | Format on save, show PHPStan and phpcs warnings |
| Before each commit | A Git pre-commit hook that runs the fixer and php -l |
| On every push | CI (GitHub Actions, GitLab CI) runs phpcs, phpstan and the tests, and fails the build if something is wrong |
A minimal composer script keeps the commands short:
{
"scripts": {
"lint": "phpcs --standard=PSR12 src",
"analyse": "phpstan analyse src --level=6",
"test": "phpunit",
"check": ["@lint", "@analyse", "@test"]
}
}
Run composer check before you push.
Adopt a standard, then stop arguing
Choose one, write it down (a CONTRIBUTING.md helps), let the tools enforce it, and spend review time on design and correctness.