PSR-12 Coding Standard
Also Known As
PSR-12
PSR-12 coding style
extended coding standard
TL;DR
The PHP-FIG extended coding style guide that supersedes PSR-2, covering namespaces, imports, classes, and control structures.
Explanation
PSR-12 (accepted 2019) extends and replaces PSR-2, building on PSR-1. It specifies: files must use Unix LF line endings and end with a blank line; 4-space indentation; lines up to 120 characters (soft limit 80); one blank line between use declarations and class body; opening braces for classes and methods on their own line; no space before parentheses in control structures. Most PHP projects enforce PSR-12 via PHP_CodeSniffer or PHP-CS-Fixer in CI pipelines.
Common Misconception
✗ PSR-12 covers all PHP coding style decisions. PSR-12 covers structure, braces, visibility, and spacing but deliberately omits many style choices (trailing commas, single vs double quotes) — teams still need agreed conventions or a tool like PHP CS Fixer for the gaps.
Why It Matters
PSR-12 extends PSR-1 and PSR-2 with modern PHP coding style rules — consistent formatting across a team means diffs show logic changes, not whitespace preferences.
Common Mistakes
- Not using an automated formatter (PHP CS Fixer) — manual PSR-12 compliance is inconsistent.
- Braces on the same line for class and function declarations — PSR-12 requires them on the next line.
- Missing blank lines between method declarations.
- Not enforcing PSR-12 in CI — the standard degrades without automated enforcement.
Code Examples
💡 Note
Key PSR-12 rules: opening brace on new line for classes/methods; 4-space indent; one blank line between methods; use statements grouped alphabetically.
✗ Vulnerable
<?php
class Foo{
public function bar($x,$y){
if($x){return $y;}
return null;
}}
// PSR-12 compliant:
class Foo
{
public function bar(int $x, int $y): ?int
{
if ($x) {
return $y;
}
return null;
}
}
✓ Fixed
<?php
declare(strict_types=1);
namespace App\Domain\Order;
use App\Domain\User\User;
use DateTimeImmutable;
class Order
{
public function __construct(
private readonly int $id,
private readonly User $customer,
private readonly DateTimeImmutable $placedAt,
) {
}
public function getId(): int
{
return $this->id;
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
31
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Perplexity 9
Amazonbot 6
Ahrefs 6
Unknown AI 3
Google 2
Also referenced
How they use it
crawler 24
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Add php-cs-fixer to your project with a PSR-12 rule set — it auto-fixes most style issues on every save; run --dry-run in CI to catch any remaining issues
📦 Applies To
PHP 7.1+
web
cli
queue-worker
🔍 Detection Hints
Inconsistent brace placement; mixed indentation; no blank line between class methods; non-PSR-12 compliant file structure
Auto-detectable:
✓ Yes
php-cs-fixer
phpcs
editorconfig
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Low
✓ Auto-fixable
Fix: Low
Context: File