|  | 
|  | 1 | +--- | 
|  | 2 | +description: Enforces best practices for PHP development, focusing on context-aware code generation, modern patterns, and maintainable architecture. Provides comprehensive guidelines for writing clean, efficient, and secure PHP code with proper context. | 
|  | 3 | +globs: **/*.php | 
|  | 4 | +--- | 
|  | 5 | +# PHP Best Practices | 
|  | 6 | + | 
|  | 7 | +You are an expert in PHP programming, Laravel, Symfony, and related PHP technologies. | 
|  | 8 | +You understand modern PHP development practices, architectural patterns, and the importance of providing complete context in code generation. | 
|  | 9 | + | 
|  | 10 | +### Context-Aware Code Generation | 
|  | 11 | +- Always provide complete class context including namespaces and use statements | 
|  | 12 | +- Include relevant configuration files (composer.json) when generating projects | 
|  | 13 | +- Generate complete method signatures with proper parameters, return types, and PHPDoc | 
|  | 14 | +- Include comprehensive PHPDoc blocks explaining the purpose, parameters, and return values | 
|  | 15 | +- Provide context about the class's role in the larger system architecture | 
|  | 16 | + | 
|  | 17 | +### Code Style and Structure | 
|  | 18 | +- Follow PSR-12 style guide and clean code principles | 
|  | 19 | +- Structure code in logical namespaces following domain-driven design | 
|  | 20 | +- Implement proper separation of concerns (controllers, models, services, repositories) | 
|  | 21 | +- Use modern PHP features (typed properties, attributes, enums) appropriately | 
|  | 22 | +- Maintain consistent code formatting using PHP-CS-Fixer | 
|  | 23 | +- Use proper autoloading and namespace structure | 
|  | 24 | + | 
|  | 25 | +### Framework Best Practices | 
|  | 26 | +- Use Laravel/Symfony best practices and patterns | 
|  | 27 | +- Implement proper dependency injection and service containers | 
|  | 28 | +- Configure proper routing and middleware | 
|  | 29 | +- Use proper ORM patterns and database migrations | 
|  | 30 | +- Implement proper error handling and logging | 
|  | 31 | +- Configure proper testing setup (PHPUnit, Pest) | 
|  | 32 | + | 
|  | 33 | +### Testing and Quality | 
|  | 34 | +- Write comprehensive unit tests with proper test context | 
|  | 35 | +- Include integration tests for critical paths | 
|  | 36 | +- Use proper mocking strategies with PHPUnit | 
|  | 37 | +- Implement E2E tests with Laravel Dusk or similar | 
|  | 38 | +- Include performance tests for critical components | 
|  | 39 | +- Maintain high test coverage for core business logic | 
|  | 40 | + | 
|  | 41 | +### Security and Performance | 
|  | 42 | +- Implement proper input validation and sanitization | 
|  | 43 | +- Use secure authentication and token management | 
|  | 44 | +- Configure proper CORS and CSRF protection | 
|  | 45 | +- Implement rate limiting and request validation | 
|  | 46 | +- Use proper caching strategies | 
|  | 47 | +- Optimize database queries and indexes | 
|  | 48 | + | 
|  | 49 | +### API Design | 
|  | 50 | +- Follow RESTful principles with proper HTTP methods | 
|  | 51 | +- Use proper status codes and error responses | 
|  | 52 | +- Implement proper versioning strategies | 
|  | 53 | +- Document APIs using OpenAPI/Swagger | 
|  | 54 | +- Include proper request/response validation | 
|  | 55 | +- Implement proper pagination and filtering | 
|  | 56 | + | 
|  | 57 | +### Database and Data Access | 
|  | 58 | +- Use proper ORM patterns (Eloquent, Doctrine) | 
|  | 59 | +- Implement proper transaction management | 
|  | 60 | +- Use database migrations | 
|  | 61 | +- Optimize queries and use proper indexing | 
|  | 62 | +- Implement proper connection pooling | 
|  | 63 | +- Use proper database isolation levels | 
|  | 64 | + | 
|  | 65 | +### Build and Deployment | 
|  | 66 | +- Use Composer for dependency management | 
|  | 67 | +- Implement proper CI/CD pipelines | 
|  | 68 | +- Use Docker for containerization | 
|  | 69 | +- Configure proper environment variables | 
|  | 70 | +- Implement proper logging and monitoring | 
|  | 71 | +- Use proper deployment strategies | 
|  | 72 | + | 
|  | 73 | +### Examples | 
|  | 74 | + | 
|  | 75 | +```php | 
|  | 76 | +<?php | 
|  | 77 | + | 
|  | 78 | +namespace App\Services; | 
|  | 79 | + | 
|  | 80 | +use App\Models\User; | 
|  | 81 | +use App\Exceptions\ApiException; | 
|  | 82 | +use Illuminate\Support\Facades\Cache; | 
|  | 83 | + | 
|  | 84 | +/** | 
|  | 85 | + * UserService handles user-related operations. | 
|  | 86 | + * Provides methods for user management and authentication. | 
|  | 87 | + */ | 
|  | 88 | +class UserService | 
|  | 89 | +{ | 
|  | 90 | + private $apiClient; | 
|  | 91 | + private $cache; | 
|  | 92 | + | 
|  | 93 | + public function __construct($apiClient, $cache) | 
|  | 94 | + { | 
|  | 95 | + $this->apiClient = $apiClient; | 
|  | 96 | + $this->cache = $cache; | 
|  | 97 | + } | 
|  | 98 | + | 
|  | 99 | + /** | 
|  | 100 | + * Finds a user by their email address. | 
|  | 101 | + * | 
|  | 102 | + * @param string $email The email address to search for | 
|  | 103 | + * @return User|null The user if found, null otherwise | 
|  | 104 | + * @throws ApiException If the request fails | 
|  | 105 | + */ | 
|  | 106 | + public function findUserByEmail(string $email): ?User | 
|  | 107 | + { | 
|  | 108 | + try { | 
|  | 109 | + $cachedUser = $this->cache->get("user:{$email}"); | 
|  | 110 | + if ($cachedUser) { | 
|  | 111 | + return new User($cachedUser); | 
|  | 112 | + } | 
|  | 113 | + | 
|  | 114 | + $userData = $this->apiClient->get("/users?email={$email}"); | 
|  | 115 | + if ($userData) { | 
|  | 116 | + $user = new User($userData); | 
|  | 117 | + $this->cache->set("user:{$email}", $userData); | 
|  | 118 | + return $user; | 
|  | 119 | + } | 
|  | 120 | + return null; | 
|  | 121 | + } catch (\Exception $e) { | 
|  | 122 | + throw new ApiException("Failed to find user by email: " . $e->getMessage()); | 
|  | 123 | + } | 
|  | 124 | + } | 
|  | 125 | +} | 
|  | 126 | + | 
|  | 127 | +/** | 
|  | 128 | + * Tests for UserService functionality. | 
|  | 129 | + */ | 
|  | 130 | +class UserServiceTest extends \PHPUnit\Framework\TestCase | 
|  | 131 | +{ | 
|  | 132 | + private $service; | 
|  | 133 | + private $apiClient; | 
|  | 134 | + private $cache; | 
|  | 135 | + | 
|  | 136 | + protected function setUp(): void | 
|  | 137 | + { | 
|  | 138 | + $this->apiClient = $this->createMock(\App\Clients\ApiClient::class); | 
|  | 139 | + $this->cache = $this->createMock(\Illuminate\Cache\Repository::class); | 
|  | 140 | + $this->service = new UserService($this->apiClient, $this->cache); | 
|  | 141 | + } | 
|  | 142 | + | 
|  | 143 | + public function testFindUserByEmailWhenUserExists() | 
|  | 144 | + { | 
|  | 145 | + // Given | 
|  | 146 | + $email = "test@example.com"; | 
|  | 147 | + $userData = ["id" => 1, "email" => $email]; | 
|  | 148 | + $this->apiClient->method('get')->willReturn($userData); | 
|  | 149 | + | 
|  | 150 | + // When | 
|  | 151 | + $result = $this->service->findUserByEmail($email); | 
|  | 152 | + | 
|  | 153 | + // Then | 
|  | 154 | + $this->assertNotNull($result); | 
|  | 155 | + $this->assertEquals($email, $result->getEmail()); | 
|  | 156 | + $this->apiClient->expects($this->once()) | 
|  | 157 | + ->method('get') | 
|  | 158 | + ->with("/users?email={$email}"); | 
|  | 159 | + } | 
|  | 160 | + | 
|  | 161 | + public function testFindUserByEmailWhenUserNotFound() | 
|  | 162 | + { | 
|  | 163 | + // Given | 
|  | 164 | + $email = "nonexistent@example.com"; | 
|  | 165 | + $this->apiClient->method('get')->willReturn(null); | 
|  | 166 | + | 
|  | 167 | + // When | 
|  | 168 | + $result = $this->service->findUserByEmail($email); | 
|  | 169 | + | 
|  | 170 | + // Then | 
|  | 171 | + $this->assertNull($result); | 
|  | 172 | + $this->apiClient->expects($this->once()) | 
|  | 173 | + ->method('get') | 
|  | 174 | + ->with("/users?email={$email}"); | 
|  | 175 | + } | 
|  | 176 | +} | 
0 commit comments