bg_image
header

PSR-12

PSR-12 is a coding style guideline defined by the PHP-FIG (PHP Framework Interoperability Group). It builds on PSR-1 (Basic Coding Standard) and PSR-2 (Coding Style Guide), extending them to include modern practices and requirements.


Purpose of PSR-12

PSR-12 aims to establish a consistent and readable code style for PHP projects, facilitating collaboration between developers and maintaining a uniform codebase.


Key Guidelines of PSR-12

1. Indentation

  • Use 4 spaces for indentation (no tabs).

2. Line Length

  • Maximum line length should not exceed 120 characters.
  • Code may be broken into multiple lines for better readability.

3. Namespace and Use Statements

  • Add one blank line after the namespace declaration.
  • use statements should follow the namespace declaration.
  • Imported classes, functions, and constants should be alphabetically sorted without blank lines between them.
namespace App\Controller;

use App\Service\MyService;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerInterface;

4. Classes

  • The opening { for a class or method must be placed on the next line.
  • Visibility (public, protected, private) is mandatory for all methods and properties.
class MyClass
{
    private string $property;

    public function myMethod(): void
    {
        // code
    }
}

5. Methods and Functions

  • Each parameter must be placed on a new line if the parameter list is wrapped.
  • Return types should be explicitly declared.
public function myFunction(
    int $param1,
    string $param2
): string {
    return 'example';
}

6. Control Structures (if, while, for, etc.)

  • The opening { must be on the same line as the control structure.
  • A space is required between the control structure and the condition.
if ($condition) {
    // code
} elseif ($otherCondition) {
    // code
} else {
    // code
}

7. Arrays

  • Use the short syntax ([]) for arrays.
  • In multiline arrays, each element should appear on a new line.
$array = [
    'first' => 'value1',
    'second' => 'value2',
];

8. Type Declarations

  • Parameter, return, and property types are mandatory (where possible).
  • Nullable types are prefixed with ?.
public function getValue(?int $id): ?string
{
    return $id !== null ? (string) $id : null;
}

9. Files

  • PHP files must start with the <?php tag and must not include a closing ?> tag.
  • Add blank lines between declarations like classes or functions.

Differences from PSR-2

PSR-12 extends PSR-2 by:

  • Supporting modern PHP features (e.g., nullable types, declare(strict_types=1), traits, type hinting).
  • Clarifying rules for line lengths, wrapped method parameters, and arrays.
  • Requiring explicit type declarations.

Benefits of PSR-12

  • Simplifies code reviews.
  • Improves readability and maintainability.
  • Enhances interoperability between PHP projects.
  • Ensures consistency with modern PHP practices.

Summary

PSR-12 is the standard for modern and consistent PHP code. It improves code quality and simplifies collaboration, especially in team environments. Tools like PHP_CodeSniffer or PHP-CS-Fixer can help ensure adherence to PSR-12 effortlessly.