mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 06:42:54 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+182
-332
@@ -1,334 +1,184 @@
|
||||
---
|
||||
description: This rule provides guidelines for PHP coding best practices, covering code structure, security, performance, and testing to improve code quality and maintainability.
|
||||
globs: *.php
|
||||
description: This guide provides definitive best practices for writing clean, secure, and maintainable PHP code, strictly adhering to PSR-12 and modern PHP 8.4+ features.
|
||||
globs: **/*
|
||||
---
|
||||
# PHP Best Practices and Coding Standards
|
||||
|
||||
This document outlines best practices and coding standards for PHP development to ensure code quality, maintainability, security, and performance.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
### 1.1. Directory Structure Best Practices
|
||||
|
||||
* **`src/`:** Contains the core source code of your application or library.
|
||||
* **`public/`:** The document root of your application, containing publicly accessible files like `index.php`, CSS, JavaScript, and images. All requests should be routed through this directory.
|
||||
* **`config/`:** Stores configuration files for different environments (development, testing, production).
|
||||
* **`tests/`:** Contains unit, integration, and functional tests.
|
||||
* **`vendor/`:** Managed by Composer, containing third-party libraries and dependencies. Do not modify contents manually.
|
||||
* **`cache/`:** Directory used for caching data.
|
||||
* **`logs/`:** Directory for storing application logs. Use descriptive names for log files.
|
||||
* **`data/`:** Stores data files (e.g., SQLite databases).
|
||||
* **`resources/`:** Contains view templates, language files, and other resources.
|
||||
* **`migrations/`:** Database migration files for version control of your database schema.
|
||||
|
||||
|
||||
project/
|
||||
├── src/
|
||||
│ ├── Controller/
|
||||
│ │ └── UserController.php
|
||||
│ ├── Model/
|
||||
│ │ └── User.php
|
||||
│ └── ...
|
||||
├── public/
|
||||
│ └── index.php
|
||||
├── config/
|
||||
│ └── config.php
|
||||
├── tests/
|
||||
│ ├── Unit/
|
||||
│ │ └── UserTest.php
|
||||
│ └── ...
|
||||
├── vendor/
|
||||
│ └── ...
|
||||
├── cache/
|
||||
│ └── ...
|
||||
├── logs/
|
||||
│ └── app.log
|
||||
├── data/
|
||||
│ └── database.sqlite
|
||||
├── resources/
|
||||
│ ├── views/
|
||||
│ │ └── user.php
|
||||
│ └── lang/
|
||||
│ └── en.php
|
||||
└── migrations/
|
||||
└── 2024_01_01_create_users_table.php
|
||||
|
||||
|
||||
### 1.2. File Naming Conventions
|
||||
|
||||
* **Classes:** Use PascalCase (e.g., `UserController.php`). The filename should match the class name.
|
||||
* **Functions and methods:** Use camelCase (e.g., `getUserById()`).
|
||||
* **Variables:** Use camelCase (e.g., `$userName`).
|
||||
* **Constants:** Use UPPER_SNAKE_CASE (e.g., `MAX_USER_AGE`).
|
||||
* **Configuration files:** Use lowercase with underscores (e.g., `database_config.php`).
|
||||
* **View files:** Use lowercase with hyphens (e.g., `user-profile.php`).
|
||||
|
||||
### 1.3. Module Organization
|
||||
|
||||
* **Separate concerns:** Organize code into modules based on functionality (e.g., authentication, user management, payment processing).
|
||||
* **Use namespaces:** Group related classes and interfaces within namespaces to avoid naming conflicts and improve code organization (e.g., `namespace App\Controllers;`). Follow PSR-4 autoloading standards.
|
||||
* **Dependency Injection:** Use dependency injection to decouple modules and make them easier to test and maintain.
|
||||
* **Interfaces and Abstract Classes:** Define interfaces for modules to ensure loose coupling and allow for different implementations.
|
||||
|
||||
### 1.4. Component Architecture
|
||||
|
||||
* **Components:** Independent, reusable pieces of functionality that can be composed to build larger applications.
|
||||
* **Thin Controllers:** Keep controllers light, delegate business logic to service classes or model layer.
|
||||
* **Repositories:** Abstract data access logic from the rest of the application. This allows you to easily switch between different data sources (e.g., databases, APIs) without modifying the core application code.
|
||||
* **Services:** Encapsulate complex business logic.
|
||||
|
||||
### 1.5. Code Splitting
|
||||
|
||||
* **Lazy loading:** Load modules or components only when they are needed to improve initial load time.
|
||||
* **Composer autoloading:** Composer efficiently handles autoloading of classes based on namespaces, splitting code into separate files.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1. Design Patterns
|
||||
|
||||
* **Singleton:** Ensures that a class has only one instance and provides a global point of access to it. Use sparingly, as overuse can lead to tight coupling and make testing difficult.
|
||||
* **Factory:** Creates objects without specifying the exact class to create. Useful for abstracting object creation.
|
||||
* **Observer:** Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Useful for event handling.
|
||||
* **Strategy:** Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Lets the algorithm vary independently from clients that use it.
|
||||
* **Model-View-Controller (MVC):** Separates application logic (Model), user interface (View), and input handling (Controller). Most PHP frameworks are based on MVC.
|
||||
* **Repository Pattern:** Abstract data access logic. Useful when you want to be able to easily switch data sources or when your data access logic is complex.
|
||||
|
||||
### 2.2. Recommended Approaches
|
||||
|
||||
* **Database interactions:** Use prepared statements with parameterized queries to prevent SQL injection.
|
||||
* **Templating:** Use a templating engine like Twig or Blade to separate presentation logic from PHP code. Helps to maintain code clean and readable.
|
||||
* **Routing:** Use a routing component to map URLs to controllers and actions. Simplifies URL handling.
|
||||
* **Error handling:** Implement a consistent error handling strategy using exceptions and logging.
|
||||
* **Dependency Injection:** Use a dependency injection container to manage dependencies and promote loose coupling. Frameworks often include DI containers.
|
||||
|
||||
### 2.3. Anti-patterns and Code Smells
|
||||
|
||||
* **Global state:** Avoid using global variables as they can lead to unpredictable behavior and make code difficult to test.
|
||||
* **Tight coupling:** Minimize dependencies between classes and modules to improve reusability and maintainability.
|
||||
* **Long methods/functions:** Break down large methods into smaller, more focused functions.
|
||||
* **Copy-pasted code:** Extract common functionality into reusable functions or classes.
|
||||
* **Ignoring errors:** Always handle exceptions and log errors appropriately.
|
||||
* **Over-use of static methods:** Limit the use of static methods, which can make testing more difficult.
|
||||
* **Code duplication:** Follow the DRY (Don't Repeat Yourself) principle.
|
||||
|
||||
### 2.4. State Management
|
||||
|
||||
* **Sessions:** Use PHP sessions for managing user authentication and temporary data. Configure session security settings (e.g., `session.cookie_httponly`, `session.cookie_secure`).
|
||||
* **Cookies:** Use cookies for storing small amounts of data on the client-side. Be mindful of cookie size limits and security.
|
||||
* **Caching:** Use caching mechanisms (e.g., Memcached, Redis, file-based caching) to improve performance by storing frequently accessed data.
|
||||
* **Database:** Store persistent data in a database. Use appropriate data types and indexes for optimal performance.
|
||||
* **Statelessness:** Design APIs to be stateless whenever possible to improve scalability.
|
||||
|
||||
### 2.5. Error Handling
|
||||
|
||||
* **Exceptions:** Use exceptions to handle errors and exceptional situations. Throw exceptions when something goes wrong.
|
||||
* **Try-catch blocks:** Wrap code that might throw an exception in try-catch blocks to handle errors gracefully.
|
||||
* **Custom exception classes:** Create custom exception classes to represent specific error conditions in your application.
|
||||
* **Logging:** Log errors, warnings, and informational messages using a logging library (e.g., Monolog). Configure logging levels.
|
||||
* **Error reporting:** Configure PHP error reporting settings (`error_reporting`, `display_errors`) appropriately for different environments. Disable displaying errors in production.
|
||||
* **Global Exception Handler:** Set a global exception handler to catch uncaught exceptions and log them.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1. Optimization Techniques
|
||||
|
||||
* **Opcode caching:** Use an opcode cache like OPcache (built into PHP) to store compiled PHP code in memory, reducing the need to recompile code on each request.
|
||||
* **Caching:** Implement caching strategies at different levels (e.g., database caching, object caching, page caching).
|
||||
* **Database optimization:** Optimize database queries, use indexes, and avoid N+1 query problems.
|
||||
* **Code profiling:** Use a profiler (e.g., Xdebug) to identify performance bottlenecks in your code.
|
||||
* **Minimize file I/O:** Reduce the number of file read/write operations.
|
||||
* **Use appropriate data structures:** Choose the right data structures for your needs (e.g., arrays, linked lists, hash tables).
|
||||
* **Avoid resource-intensive operations:** Minimize the use of functions that consume a lot of resources (e.g., regular expressions).
|
||||
* **Use PHP 7.x/8.x features:** Take advantage of performance improvements in newer versions of PHP.
|
||||
|
||||
### 3.2. Memory Management
|
||||
|
||||
* **Unset variables:** Unset variables that are no longer needed to free up memory.
|
||||
* **Avoid large arrays:** Avoid storing large amounts of data in arrays. Use iterators or generators if possible.
|
||||
* **Garbage collection:** PHP automatically manages memory using garbage collection. Ensure that your code doesn't create memory leaks (e.g., circular references).
|
||||
* **Limit session size:** Be mindful of the amount of data stored in sessions, as large sessions can consume a lot of memory.
|
||||
|
||||
### 3.3. Rendering Optimization
|
||||
|
||||
* **Minimize HTTP requests:** Reduce the number of HTTP requests by combining CSS and JavaScript files, using CSS sprites, and inlining small images.
|
||||
* **Optimize images:** Compress images and use appropriate image formats (e.g., JPEG, PNG, WebP).
|
||||
* **Use browser caching:** Configure browser caching to store static assets (e.g., CSS, JavaScript, images) in the browser cache.
|
||||
* **Content Delivery Network (CDN):** Use a CDN to serve static assets from geographically distributed servers.
|
||||
* **Gzip compression:** Enable Gzip compression to reduce the size of HTTP responses.
|
||||
* **Minify HTML, CSS, and JavaScript:** Remove unnecessary characters from HTML, CSS, and JavaScript files.
|
||||
|
||||
### 3.4. Bundle Size Optimization (Less relevant for traditional PHP, but important for frontend assets)
|
||||
|
||||
* **Code splitting:** Split frontend code into smaller chunks that can be loaded on demand. (Relevant if using a frontend framework like React, Vue, or Angular alongside PHP).
|
||||
* **Tree shaking:** Remove unused code from your JavaScript bundles. (Relevant if using a frontend framework).
|
||||
|
||||
### 3.5. Lazy Loading
|
||||
|
||||
* **Lazy-load images:** Load images only when they are visible in the viewport.
|
||||
* **Lazy-load modules:** Load modules or components only when they are needed.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1. Common Vulnerabilities
|
||||
|
||||
* **SQL injection:** Occurs when untrusted data is used to construct SQL queries. Prevent by using prepared statements with parameterized queries.
|
||||
* **Cross-Site Scripting (XSS):** Occurs when malicious scripts are injected into a website. Prevent by sanitizing user input and escaping output.
|
||||
* **Cross-Site Request Forgery (CSRF):** Occurs when a malicious website tricks a user into performing unwanted actions on another website. Prevent by using CSRF tokens.
|
||||
* **Remote Code Execution (RCE):** Occurs when an attacker can execute arbitrary code on the server. Prevent by avoiding dangerous functions (e.g., `eval()`, `system()`) and carefully validating user input.
|
||||
* **File Inclusion:** Occurs when an attacker can include arbitrary files on the server. Prevent by carefully validating user input and restricting file access.
|
||||
* **Session Hijacking:** Occurs when an attacker steals a user's session ID. Prevent by using secure session management practices.
|
||||
* **Denial of Service (DoS):** Occurs when an attacker overwhelms a server with requests, making it unavailable to legitimate users. Prevent by implementing rate limiting and other security measures.
|
||||
|
||||
### 4.2. Input Validation
|
||||
|
||||
* **Sanitize user input:** Remove or encode potentially dangerous characters from user input.
|
||||
* **Validate user input:** Verify that user input conforms to expected formats and values.
|
||||
* **Use whitelisting:** Allow only known good values for user input. Avoid blacklisting, which can be easily bypassed.
|
||||
* **Escape output:** Escape output to prevent XSS attacks.
|
||||
|
||||
### 4.3. Authentication and Authorization
|
||||
|
||||
* **Use strong passwords:** Enforce strong password policies and use a secure password hashing algorithm (e.g., bcrypt, Argon2).
|
||||
* **Salt passwords:** Add a unique salt to each password before hashing it.
|
||||
* **Store passwords securely:** Store password hashes in a secure database.
|
||||
* **Implement access control:** Restrict access to resources based on user roles and permissions.
|
||||
* **Use multi-factor authentication (MFA):** Add an extra layer of security by requiring users to provide multiple forms of authentication.
|
||||
* **Secure cookies:** Set the `HttpOnly` and `Secure` flags on cookies to prevent XSS and man-in-the-middle attacks.
|
||||
* **Use HTTPS:** Encrypt all communication between the client and server using HTTPS.
|
||||
|
||||
### 4.4. Data Protection
|
||||
|
||||
* **Encrypt sensitive data:** Encrypt sensitive data at rest and in transit.
|
||||
* **Use encryption keys securely:** Store encryption keys in a secure location and rotate them regularly.
|
||||
* **Comply with data privacy regulations:** Comply with data privacy regulations like GDPR and CCPA.
|
||||
* **Implement data masking:** Mask sensitive data when it is not needed for processing.
|
||||
* **Regularly back up data:** Regularly back up data to protect against data loss.
|
||||
|
||||
### 4.5. Secure API Communication
|
||||
|
||||
* **Use HTTPS:** Enforce HTTPS for all API endpoints.
|
||||
* **Authenticate API requests:** Use API keys, OAuth 2.0, or other authentication mechanisms to verify the identity of API clients.
|
||||
* **Authorize API requests:** Restrict access to API endpoints based on user roles and permissions.
|
||||
* **Rate limit API requests:** Implement rate limiting to prevent abuse.
|
||||
* **Validate API input:** Validate all API input to prevent injection attacks.
|
||||
* **Sanitize API output:** Sanitize all API output to prevent XSS attacks.
|
||||
* **Log API requests:** Log all API requests for auditing and security monitoring.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1. Unit Testing
|
||||
|
||||
* **Test individual units of code:** Focus on testing individual functions, methods, or classes in isolation.
|
||||
* **Use a testing framework:** Use a unit testing framework like PHPUnit or Codeception.
|
||||
* **Write testable code:** Design code to be easily testable (e.g., use dependency injection).
|
||||
* **Follow the Arrange-Act-Assert pattern:** Arrange the test data, act on the code being tested, and assert the expected results.
|
||||
* **Test edge cases:** Test edge cases and boundary conditions to ensure that code handles unexpected input correctly.
|
||||
|
||||
### 5.2. Integration Testing
|
||||
|
||||
* **Test interactions between components:** Focus on testing how different components of the application interact with each other.
|
||||
* **Use a testing framework:** Use an integration testing framework like PHPUnit or Codeception.
|
||||
* **Test database interactions:** Test database interactions to ensure that data is stored and retrieved correctly.
|
||||
* **Test API integrations:** Test API integrations to ensure that data is exchanged correctly.
|
||||
|
||||
### 5.3. End-to-End Testing
|
||||
|
||||
* **Test the entire application flow:** Focus on testing the entire application flow from start to finish.
|
||||
* **Use a testing framework:** Use an end-to-end testing framework like Selenium or Cypress.
|
||||
* **Automate tests:** Automate end-to-end tests to ensure that the application continues to work correctly as it evolves.
|
||||
|
||||
### 5.4. Test Organization
|
||||
|
||||
* **Create a `tests/` directory:** Store all test files in a `tests/` directory.
|
||||
* **Mirror the source code structure:** Organize test files to mirror the source code structure (e.g., `tests/Unit/UserControllerTest.php`).
|
||||
* **Use namespaces:** Use namespaces to organize test classes.
|
||||
* **Use descriptive test names:** Use descriptive test names that clearly indicate what is being tested.
|
||||
|
||||
### 5.5. Mocking and Stubbing
|
||||
|
||||
* **Use mocks to isolate units of code:** Use mocks to replace dependencies with controlled test doubles.
|
||||
* **Use stubs to provide canned responses:** Use stubs to provide canned responses from dependencies.
|
||||
* **Use a mocking framework:** Use a mocking framework like Mockery or Prophecy.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1. Frequent Mistakes
|
||||
|
||||
* **Not escaping output:** Failing to escape output, leading to XSS vulnerabilities.
|
||||
* **Using `mysql_*` functions:** Using deprecated `mysql_*` functions instead of `mysqli_*` or PDO.
|
||||
* **Not using prepared statements:** Not using prepared statements with parameterized queries, leading to SQL injection vulnerabilities.
|
||||
* **Ignoring errors:** Ignoring errors and warnings, making it difficult to debug problems.
|
||||
* **Not using namespaces:** Not using namespaces, leading to naming conflicts.
|
||||
* **Over-complicating code:** Writing overly complex code that is difficult to understand and maintain.
|
||||
* **Not following coding standards:** Not following coding standards, leading to inconsistent code.
|
||||
|
||||
### 6.2. Edge Cases
|
||||
|
||||
* **Handling different character encodings:** Handling different character encodings correctly.
|
||||
* **Dealing with time zones:** Dealing with time zones correctly.
|
||||
* **Handling large file uploads:** Handling large file uploads without running out of memory.
|
||||
* **Dealing with concurrency:** Handling concurrency correctly in multi-threaded applications (less common in typical web applications).
|
||||
|
||||
### 6.3. Version-Specific Issues
|
||||
|
||||
* **Deprecated features:** Being aware of deprecated features and replacing them with their recommended alternatives.
|
||||
* **Compatibility issues:** Ensuring that code is compatible with different versions of PHP.
|
||||
|
||||
### 6.4. Compatibility Concerns
|
||||
|
||||
* **Database drivers:** Ensuring that the correct database drivers are installed and configured.
|
||||
* **Web server configuration:** Configuring the web server (e.g., Apache, Nginx) correctly to serve PHP applications.
|
||||
* **Operating system compatibility:** Ensuring that the application works correctly on different operating systems.
|
||||
|
||||
### 6.5. Debugging Strategies
|
||||
|
||||
* **Use a debugger:** Use a debugger (e.g., Xdebug) to step through code and inspect variables.
|
||||
* **Log messages:** Insert log messages to track the execution flow and identify problems.
|
||||
* **Use `var_dump()` or `print_r()`:** Use `var_dump()` or `print_r()` to inspect variables and data structures (use with caution in production).
|
||||
* **Read error messages:** Carefully read error messages and warnings to understand the cause of problems.
|
||||
* **Use a linter:** Use a linter to identify potential errors and code style violations.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1. Recommended Development Tools
|
||||
|
||||
* **IDE:** PhpStorm, VS Code with PHP extensions.
|
||||
* **Debugger:** Xdebug.
|
||||
* **Package manager:** Composer.
|
||||
* **Database client:** Dbeaver, phpMyAdmin.
|
||||
* **Version control:** Git.
|
||||
* **Virtualization:** Docker, Vagrant.
|
||||
* **Profiler:** Xdebug, Blackfire.io
|
||||
|
||||
### 7.2. Build Configuration
|
||||
|
||||
* **Use Composer:** Use Composer to manage dependencies.
|
||||
* **Define dependencies in `composer.json`:** List all dependencies in the `composer.json` file.
|
||||
* **Use semantic versioning:** Use semantic versioning to specify dependency versions.
|
||||
* **Use a `composer.lock` file:** Commit the `composer.lock` file to version control to ensure that everyone is using the same versions of dependencies.
|
||||
* **Configure autoloading:** Configure autoloading to automatically load classes.
|
||||
|
||||
### 7.3. Linting and Formatting
|
||||
|
||||
* **Use a linter:** Use a linter (e.g., PHPStan, Psalm) to identify potential errors and code style violations.
|
||||
* **Use a code formatter:** Use a code formatter (e.g., PHP CS Fixer) to automatically format code according to coding standards.
|
||||
* **Configure linting and formatting rules:** Configure linting and formatting rules to enforce coding standards.
|
||||
* **Integrate linting and formatting into the development workflow:** Integrate linting and formatting into the development workflow to automatically check code for errors and style violations.
|
||||
|
||||
### 7.4. Deployment
|
||||
|
||||
* **Use version control:** Use version control to track changes to code.
|
||||
* **Automate deployments:** Automate deployments using a deployment tool (e.g., Deployer, Capistrano).
|
||||
* **Use environment variables:** Use environment variables to configure application settings for different environments.
|
||||
* **Separate code and configuration:** Separate code and configuration to make it easier to deploy applications to different environments.
|
||||
* **Use a build process:** Use a build process to prepare code for deployment (e.g., run linters, formatters, and tests).
|
||||
* **Test deployments:** Test deployments in a staging environment before deploying to production.
|
||||
|
||||
### 7.5. CI/CD
|
||||
|
||||
* **Use a CI/CD platform:** Use a CI/CD platform (e.g., Jenkins, Travis CI, CircleCI, GitHub Actions) to automate the build, test, and deployment process.
|
||||
* **Configure CI/CD pipelines:** Configure CI/CD pipelines to run tests, linters, and formatters automatically on every commit.
|
||||
* **Automate deployments:** Automate deployments using CI/CD pipelines.
|
||||
* **Use infrastructure as code:** Use infrastructure as code (e.g., Terraform, Ansible) to manage infrastructure.
|
||||
|
||||
This document aims to provide a comprehensive overview of PHP best practices and coding standards. By following these guidelines, developers can create high-quality, maintainable, secure, and performant PHP applications.
|
||||
# PHP Best Practices
|
||||
|
||||
This document outlines the definitive best practices for PHP development within our team. Adherence to these guidelines ensures consistency, maintainability, and security across all projects.
|
||||
|
||||
## Critical Guidelines:
|
||||
|
||||
### 1. Code Organization and Structure (PSR-12 & Strict Types)
|
||||
|
||||
Always adhere to PSR-12 for coding style. This includes indentation, brace placement, and line length. Crucially, enable strict typing in every PHP file.
|
||||
|
||||
* **Indentation**: Use 4 spaces, never tabs.
|
||||
* **Line Endings**: Unix LF only.
|
||||
* **Closing Tag**: Omit `?>` from files containing only PHP code.
|
||||
* **File Header Order**:
|
||||
1. `<?php`
|
||||
2. File-level docblock (optional)
|
||||
3. `declare(strict_types=1);`
|
||||
4. `namespace` declaration
|
||||
5. `use` statements (class, then function, then constant, grouped)
|
||||
6. The rest of the code.
|
||||
|
||||
❌ BAD:
|
||||
```php
|
||||
<?php
|
||||
namespace App\Service;
|
||||
use App\Model\User;
|
||||
declare(strict_types=1);
|
||||
|
||||
class UserService {
|
||||
public function getUser($id) { // Missing return type, no strict types at top
|
||||
return User::find($id);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Model\User;
|
||||
use App\Repository\UserRepository;
|
||||
|
||||
class UserService
|
||||
{
|
||||
public function __construct(private UserRepository $userRepository) {}
|
||||
|
||||
public function getUser(int $id): ?User
|
||||
{
|
||||
return $this->userRepository->find($id);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Common Patterns and Anti-patterns
|
||||
|
||||
* **Type Hinting**: Always use scalar and return type declarations. Leverage PHP 8+ union types, intersection types, and `mixed`/`static`/`never` where appropriate.
|
||||
* **Constructor Property Promotion**: Use for concise dependency injection.
|
||||
* **Match Expression**: Prefer `match` over `switch` for cleaner, more expressive comparisons.
|
||||
* **Null Coalescing Operator (`??`)**: Use to simplify checks for `null` or unset variables.
|
||||
* **Attributes**: Prefer native PHP attributes (`#[Attribute]`) over docblock annotations for metadata.
|
||||
|
||||
❌ BAD:
|
||||
```php
|
||||
class LegacyProcessor {
|
||||
public function process($type, $data) {
|
||||
switch ($type) { // Old switch, no types
|
||||
case 'foo': return 'Foo ' . $data;
|
||||
case 'bar': return 'Bar ' . $data;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class MyServiceAttribute {}
|
||||
|
||||
#[MyServiceAttribute]
|
||||
class ModernProcessor
|
||||
{
|
||||
public function process(string $type, string $data): ?string
|
||||
{
|
||||
return match ($type) {
|
||||
'foo' => 'Foo ' . $data,
|
||||
'bar' => 'Bar ' . $data,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public function getSetting(array $config): string
|
||||
{
|
||||
// Use null coalescing for default values
|
||||
return $config['setting'] ?? 'default_value';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Performance Considerations
|
||||
|
||||
* **Latest PHP Version**: Always use the current stable PHP version (8.4+).
|
||||
* **Composer Autoloading**: Optimize Composer's autoloader for production (`composer dump-autoload --optimize --no-dev --classmap-authoritative`).
|
||||
* **Avoid N+1 Queries**: Eager load relationships in ORMs.
|
||||
* **Caching**: Implement application-level caching for frequently accessed data.
|
||||
|
||||
### 4. Common Pitfalls and Gotchas
|
||||
|
||||
* **Loose Comparisons**: Always use strict comparison operators (`===`, `!==`).
|
||||
* **Error Suppression**: Never use the `@` operator. Handle errors gracefully with exceptions.
|
||||
* **Modifying Arrays During Iteration**: Leads to unpredictable behavior. Iterate over a copy or use appropriate collection methods.
|
||||
|
||||
### 5. Security Best Practices
|
||||
|
||||
* **Input Validation & Sanitization**: Validate all external input. Use specific validation rules, not just `filter_var`.
|
||||
* **Output Escaping**: Escape all output based on context (HTML, URL, JavaScript, CSS). Use templating engines that auto-escape or dedicated functions (e.g., `htmlspecialchars`).
|
||||
* **Prepared Statements**: Always use parameterized queries (PDO or ORM) to prevent SQL injection.
|
||||
* **Password Hashing**: Use `password_hash()` with `PASSWORD_ARGON2ID` (or `PASSWORD_BCRYPT` as fallback). Never store plain text passwords.
|
||||
* **Dependency Updates**: Regularly update Composer dependencies and run `composer audit`.
|
||||
* **CSRF Protection**: Implement CSRF tokens for state-changing requests.
|
||||
|
||||
❌ BAD:
|
||||
```php
|
||||
$username = $_POST['username'];
|
||||
$password = $_POST['password'];
|
||||
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; // SQL Injection!
|
||||
$db->query($sql);
|
||||
echo "Hello, " . $_GET['name']; // XSS vulnerability!
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// Input Validation
|
||||
if (!isset($_POST['username']) || !is_string($_POST['username'])) {
|
||||
throw new InvalidArgumentException('Invalid username.');
|
||||
}
|
||||
$username = trim($_POST['username']);
|
||||
|
||||
// Password Hashing
|
||||
$hashedPassword = password_hash($_POST['password'], PASSWORD_ARGON2ID);
|
||||
|
||||
// Prepared Statements
|
||||
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
|
||||
$stmt->execute([':username' => $username, ':password' => $hashedPassword]);
|
||||
|
||||
// Output Escaping
|
||||
echo "Hello, " . htmlspecialchars($_GET['name'] ?? 'Guest', ENT_QUOTES, 'UTF-8');
|
||||
```
|
||||
|
||||
### 6. Error Handling
|
||||
|
||||
* **Exceptions**: Use exceptions for all exceptional conditions. Create custom exception classes for domain-specific errors.
|
||||
* **Centralized Handling**: Implement a global exception handler to catch unhandled exceptions and log them.
|
||||
* **Logging**: Use a robust logging library (e.g., Monolog) for all errors, warnings, and important events.
|
||||
|
||||
### 7. API Design
|
||||
|
||||
* **RESTful Principles**: Design APIs to be resource-oriented, using standard HTTP methods (GET, POST, PUT, DELETE) and status codes.
|
||||
* **Consistent Naming**: Use consistent pluralized resource names (e.g., `/users`, not `/user`).
|
||||
* **Versioning**: Implement API versioning (e.g., `/v1/users`).
|
||||
* **Payload Validation**: Strictly validate all incoming request payloads.
|
||||
|
||||
### 8. Testing Approaches
|
||||
|
||||
* **PHPUnit**: Write comprehensive unit and integration tests using PHPUnit.
|
||||
* **Test Doubles**: Use mocks, stubs, and fakes to isolate units of code during testing.
|
||||
* **Static Analysis**: Integrate PHPStan or Psalm into your CI/CD pipeline with a high strictness level.
|
||||
* **Continuous Integration**: Automate tests and static analysis checks on every commit.
|
||||
Reference in New Issue
Block a user