← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

PSR-12 Coding Standard

Style PHP 7.1+ Beginner
debt(d3/e1/b2/t3)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3), php-cs-fixer and phpcs with PSR-12 ruleset catch violations automatically and are standard in PHP toolchains.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1), quick_fix is to add php-cs-fixer with PSR-12 ruleset which auto-fixes violations on save — essentially a tool config addition.

b2 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3) but lighter, scored b2 — applies across all PHP contexts but is a near-universal community standard with tooling support, so imposes minimal ongoing weight beyond CI enforcement.

t3 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'minor surprise' (t3), misconception is that PSR-12 covers all style decisions when it deliberately omits trailing commas, quote style, etc. — a documented gap teams discover but it's not catastrophic.

About DEBT scoring →

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;
    }
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 66
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 1 ping W 1 ping T 4 pings F 1 ping S 5 pings S 4 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 2 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Scrapy 16 Perplexity 9 Ahrefs 8 Amazonbot 7 Google 3 Unknown AI 3 ChatGPT 3 SEMrush 3 Claude 1 Meta AI 1
crawler 50 crawler_json 3 pre-tracking 1
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
🔗 Prerequisites
🔍 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


✓ schema.org compliant