Skip to content

PHPStan ruleset enforcing Elegant Objects principles: forbids static, null, isset, empty, instanceof, promotes immutability and object composition.

License

Notifications You must be signed in to change notification settings

haspadar/phpstan-eo-rules

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHPStan EO Rules

PHP Version


📦 About

A collection of PHPStan rules enforcing the principles of Elegant Objects: immutability, composition, no static state, and explicit contracts.

🔍 Available Rules

NoNullReturnRule

Prohibits returning null from methods. Instead of returning null, methods should either return a meaningful value or throw an exception. This eliminates null checks and makes code more predictable.

❌ Bad:

public function find(int $id): ?User
{
    return null; // Error: Returning null is forbidden
}

✅ Good:

public function find(int $id): User
{
    throw new UserNotFoundException($id);
}

// Or use a Null Object pattern
public function find(int $id): User
{
    return new GuestUser();
}

To suppress this rule for specific cases:

/** @phpstan-ignore NoNullReturn */
return null;

⚙️ Installation

composer require --dev haspadar/phpstan-eo-rules

Enable in your phpstan.neon:

includes:
    - vendor/haspadar/phpstan-eo-rules/extension.neon

Requirements: PHP 8.2+

🤝 Contributing

Contributions are welcome! Please read the Contributing Guide for details on how to get started, run tests, and submit your work.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

About

PHPStan ruleset enforcing Elegant Objects principles: forbids static, null, isset, empty, instanceof, promotes immutability and object composition.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published