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

PSR-12 Coding Standard

style PHP 7.1+ Beginner

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 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 0 pings F 3 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T
No pings yet today
Perplexity 9 Amazonbot 6 Ahrefs 6 Unknown AI 3 Google 2
crawler 24 crawler_json 1 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