A collection of PHPStan rules enforcing the principles of Elegant Objects: immutability, composition, no static state, and explicit contracts.
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;composer require --dev haspadar/phpstan-eo-rulesEnable in your phpstan.neon:
includes:
- vendor/haspadar/phpstan-eo-rules/extension.neonRequirements: PHP 8.2+
Contributions are welcome! Please read the Contributing Guide for details on how to get started, run tests, and submit your work.
This project is licensed under the MIT License. See the LICENSE file for details.