--- name: php-guide description: | PHP language guardrails, patterns, and best practices for AI-assisted development. Use when working with PHP files (.php), composer.json, or when the user mentions PHP. Provides type declaration patterns, Composer conventions, PSR standards, and testing guidelines specific to this project's coding standards. license: MIT metadata: author: samuel version: "1.0" category: language language: php extensions: ".php" --- # PHP Guide > Applies to: PHP 8.1+, Web Applications, APIs, CLIs, Microservices ## Core Principles 1. **Strict Types Always**: Every PHP file starts with `declare(strict_types=1)` 2. **Type Declarations Everywhere**: All parameters, return types, and properties must have type declarations 3. **PSR Standards**: Follow PSR-12 coding standard, PSR-4 autoloading, PSR-7 HTTP messages 4. **Composition Over Inheritance**: Prefer interfaces, traits, and dependency injection over deep class hierarchies 5. **Modern PHP First**: Use PHP 8.1+ features (enums, readonly properties, fibers, named arguments, match expressions) ## Guardrails ### Version & Dependencies - Target PHP 8.1+ (enums, readonly properties, fibers, intersection types) - Define all dependencies in `composer.json` with version constraints - Run `composer validate` and `composer audit` before committing - Use `composer.lock` for applications (commit it), omit for libraries - Separate `require-dev` for development-only dependencies - Never use `composer update` in production (use `composer install --no-dev`) ### Code Style (PSR-12) - Run PHP-CS-Fixer or PHP_CodeSniffer before every commit - Naming: `PascalCase` classes/enums, `camelCase` methods/properties, `UPPER_SNAKE` constants - One class per file, file name matches class name - Opening braces on same line for control structures, next line for classes/methods - `declare(strict_types=1)` as first statement after `` tag in pure PHP files - Imports: one `use` per declaration, grouped (classes, functions, constants), alphabetized ```php repository->existsByEmail($emailVO)) { throw ValidationException::duplicateEmail($email); } $user = User::create(name: $name, email: $emailVO); $this->repository->save($user); $this->dispatcher->dispatch(new UserRegistered($user->id)); return $user; } } ``` ### Type Declarations - All function parameters MUST have type declarations - All methods MUST declare return types (including `void`) - Use union types (`string|int`) instead of `mixed` when possible - Use intersection types (`Countable&Iterator`) for combined type constraints - Use `Type|null` for nullable parameters (prefer explicit over `?Type`) - Use `never` return type for functions that always throw or exit - Avoid `mixed` -- if truly needed, document why ### Error Handling - Never use `@` error suppression operator - Convert PHP errors to exceptions with `set_error_handler` at bootstrap - Create domain-specific exception hierarchies extending a base exception - Use specific exception types (not generic `\Exception` or `\RuntimeException`) - Always include context in exception messages - Catch specific exceptions, never bare `catch (\Throwable $e)` without re-throwing - Use `previous` parameter to chain exceptions ```php 'Administrator', self::Editor => 'Editor', self::Viewer => 'Viewer', }; } /** @return list */ public function permissions(): array { return match ($this) { self::Admin => Permission::cases(), self::Editor => [Permission::Read, Permission::Write], self::Viewer => [Permission::Read], }; } } ``` ### Readonly Classes (PHP 8.2+) ```php currency !== $other->currency) { throw new \InvalidArgumentException('Cannot add different currencies'); } return new self($this->amount + $other->amount, $this->currency); } } ``` See [references/patterns.md](references/patterns.md) for additional patterns: dependency injection, repository pattern, value objects, command/handler, fibers, match expressions, and tooling configurations. ## Testing ### Standards - Test files: `*Test.php` in `tests/` mirroring `src/` structure - Test methods: `test___` or `#[Test]` attribute - PHPUnit as primary framework; Pest as alternative - Coverage: >80% business logic, >60% overall - Use data providers for parameterized tests - Mock external dependencies with Mockery or PHPUnit mocks - No database or network calls in unit tests - Each test method tests one behavior ### PHPUnit with Data Providers ```php toString()); } /** @return iterable */ public static function validEmailProvider(): iterable { yield 'simple' => ['user@example.com']; yield 'with subdomain' => ['user@mail.example.com']; yield 'with plus' => ['user+tag@example.com']; yield 'uppercase' => ['User@Example.COM']; } #[Test] #[DataProvider('invalidEmailProvider')] public function it_rejects_invalid_emails(string $input): void { $this->expectException(ValidationException::class); Email::fromString($input); } /** @return iterable */ public static function invalidEmailProvider(): iterable { yield 'empty string' => ['']; yield 'no at sign' => ['userexample.com']; yield 'no domain' => ['user@']; yield 'no local part' => ['@example.com']; yield 'spaces' => ['user @example.com']; } } ``` ## Tooling ### Required Dev Dependencies - `phpunit/phpunit` ^10.0 -- testing - `phpstan/phpstan` ^1.10 -- static analysis (level 8) - `friendsofphp/php-cs-fixer` ^3.0 -- code style - `mockery/mockery` ^1.6 -- mocking - `vimeo/psalm` ^5.0 -- alternative static analysis ### PHPStan: Always Level 8 ```neon # phpstan.neon parameters: level: 8 paths: - src treatPhpDocTypesAsCertain: false checkMissingIterableValueType: true ``` See [references/patterns.md](references/patterns.md) for full `composer.json`, `phpstan.neon`, and `.php-cs-fixer.php` configurations. ## Essential Commands ```bash composer install # Install dependencies composer validate # Validate composer.json composer audit # Check for security vulnerabilities php vendor/bin/phpunit # Run all tests php vendor/bin/phpunit --coverage-text # With coverage summary php vendor/bin/phpstan analyse # Static analysis (level 8) php vendor/bin/psalm # Alternative static analysis php vendor/bin/php-cs-fixer fix # Auto-fix code style php vendor/bin/php-cs-fixer fix --dry-run --diff # Preview fixes ``` ## References For detailed code examples, see: - [references/patterns.md](references/patterns.md) -- Enum patterns, readonly classes, dependency injection, repository pattern, type declarations, error handling, modern PHP features, mocking, tooling configurations ## External References - [PHP: The Right Way](https://phptherightway.com/) - [PSR-12: Extended Coding Style](https://www.php-fig.org/psr/psr-12/) - [PSR-4: Autoloading Standard](https://www.php-fig.org/psr/psr-4/) - [PHPStan Documentation](https://phpstan.org/user-guide/getting-started) - [PHPUnit Documentation](https://docs.phpunit.de/) - [Psalm Documentation](https://psalm.dev/docs/) - [PHP-CS-Fixer Documentation](https://cs.symfony.com/)