PHP - PER Coding Style 2.0

PER Coding Style 2.0 is a set of coding conventions primarily aimed at PHP projects. It is an evolution of the PER Coding Style 1.0, offering stricter rules and modern coding practices. The PHP Extended Rules (PER) provide guidelines that go beyond PSR (PHP Standards Recommendations) to improve code readability, maintainability, and quality. Key Guidelines in PER Coding Style 2.0 1. Strict Typing PER 2.0 enforces strict typing in PHP files. ✅ Correct

Mar 8, 2025 - 21:31
 0
PHP - PER Coding Style 2.0

PER Coding Style 2.0 is a set of coding conventions primarily aimed at PHP projects. It is an evolution of the PER Coding Style 1.0, offering stricter rules and modern coding practices. The PHP Extended Rules (PER) provide guidelines that go beyond PSR (PHP Standards Recommendations) to improve code readability, maintainability, and quality.

Key Guidelines in PER Coding Style 2.0

1. Strict Typing

PER 2.0 enforces strict typing in PHP files.

✅ Correct

 declare(strict_types=1);

function add(int $a, int $b): int {
    return $a + $b;
}

❌ Incorrect



function add($a, $b) {
    return $a + $b; // No type declarations
}

2. Return Type Declarations

Functions and methods must have explicit return types.

✅ Correct

function getUserName(int $id): string {
    return 'User_' . $id;
}

❌ Incorrect

function getUserName($id) {
    return 'User_' . $id; // No return type specified
}

3. Class Property Visibility

All properties in a class must have a visibility modifier (private, protected, public).

✅ Correct

class User {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }
}

❌ Incorrect

class User {
    var $name; // 'var' is discouraged
}

4. Single Responsibility Principle

Each class should have only one responsibility.

✅ Correct

class User {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    public function getName(): string {
        return $this->name;
    }
}

❌ Incorrect

class User {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    public function getName(): string {
        return $this->name;
    }

    public function saveToDatabase() {
        // Mixing responsibilities
    }
}

5. Consistent Naming Conventions

  • Class names: PascalCase
  • Method names: camelCase
  • Variable names: camelCase
  • Constants: UPPER_CASE_SNAKE

✅ Correct

class OrderProcessor {
    private int $orderId;

    public function processOrder(): void {
        // Process order
    }
}

❌ Incorrect

class order_processor { // Not PascalCase
    private int $OrderId; // Incorrect case

    public function ProcessOrder() { // Incorrect case
        // Process order
    }
}

6. No Use of Global Variables

PER 2.0 discourages the use of global variables.

✅ Correct

class Config {
    public static function getDbHost(): string {
        return 'localhost';
    }
}

❌ Incorrect

global $dbHost;
$dbHost = 'localhost';

7. No Use of null Defaults for Scalars

Functions should not use null as a default value for scalar types.

✅ Correct

function greet(string $name = 'Guest'): void {
    echo "Hello, $name!";
}

❌ Incorrect

function greet(?string $name = null): void { // Discouraged
    echo "Hello, " . ($name ?? 'Guest') . "!";
}

8. No Use of else When Avoidable

PER 2.0 recommends early return instead of else.

✅ Correct

function getDiscount(int $age): int {
    if ($age < 18) {
        return 10;
    }

    return 0;
}

❌ Incorrect

function getDiscount(int $age): int {
    if ($age < 18) {
        return 10;
    } else { // Unnecessary 'else'
        return 0;
    }
}

9. Avoiding isset and empty

Use null coalescing (??) or array_key_exists() instead.

✅ Correct

$value = $data['key'] ?? 'default';

❌ Incorrect

$value = isset($data['key']) ? $data['key'] : 'default';

10. Using Enums Instead of Constants

Prefer Enums over class constants.

✅ Correct

enum OrderStatus: string {
    case Pending = 'pending';
    case Shipped = 'shipped';
}

❌ Incorrect

class OrderStatus {
    const PENDING = 'pending';
    const SHIPPED = 'shipped';
}

Conclusion

PER Coding Style 2.0 refines best practices for PHP, ensuring code is strictly typed, readable, maintainable, and modern. By following these rules, developers can produce high-quality, robust PHP applications.