An optimize set of rules for Adobe Commerce and PHP8.2 oriented in user readability and performances.
- Optimize for readability over premature optimization
- Write modular code
- Use strict types: declare(strict_types=1);
- Follow PSR-1, PSR-12 coding standards
- Ensure compliance with PHP 8.2 features and best practices
- Prefer typed properties and readonly properties when applicable
- Use enums instead of constants where relevant
- Leverage PHP 8+ attributes instead of DocBlock annotations
- Use array_map, array_filter over foreach when performance is a concern
- Avoid excessive use of exceptions, handle errors explicitly
- Use early returns to reduce nesting
- Follow SOLID principles
- Keep functions and classes small and focused
- Prevent access superglobals directly, use Request objects
- Try to keep business logic out of controllers
- Favor immutability (readonly properties, private setters)
- Escape all user input (htmlspecialchars, filter_var)
- In Magento 2, use Dependency Injection
- Use Interface instead of direct model access
- Write self-documenting code, keeping comments minimal and only when necessary
- Use PHPDoc for public methods
- Ensure meaningful variable and function names, avoiding generic names like tmp, data, res
- Maintain single responsibility principle, breaking large classes into separate files