|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**phpstan/phpstan-symfony** is a PHPStan extension that provides static analysis support for Symfony framework applications. It enhances PHPStan's understanding of Symfony-specific patterns by providing accurate return types, detecting common errors, and understanding the dependency injection container. |
| 6 | + |
| 7 | +### Key Features |
| 8 | + |
| 9 | +- Correct return types for service container methods (`ContainerInterface::get()`, `::has()`, `::getParameter()`, `::hasParameter()`) |
| 10 | +- Correct return types for controller methods (`Controller::get()`, `AbstractController::get()`, etc.) |
| 11 | +- Return type inference for `Request::getContent()`, `HeaderBag::get()`, `InputBag::get()`, `Envelope::all()` |
| 12 | +- Return types for `TreeBuilder` and `NodeDefinition` configuration objects |
| 13 | +- Detection of unregistered services accessed from the container |
| 14 | +- Detection of private service access attempts |
| 15 | +- Console command argument/option type inference via optional console application loader |
| 16 | +- Serializer/Denormalizer return type inference |
| 17 | +- Form interface type support |
| 18 | +- `@required` autowiring property support |
| 19 | + |
| 20 | +## Repository Structure |
| 21 | + |
| 22 | +``` |
| 23 | +├── src/ |
| 24 | +│ ├── Rules/Symfony/ # PHPStan rules for Symfony-specific checks |
| 25 | +│ ├── Symfony/ # Core infrastructure (service maps, parameter maps, etc.) |
| 26 | +│ └── Type/Symfony/ # Dynamic return type extensions |
| 27 | +├── tests/ |
| 28 | +│ ├── Rules/Symfony/ # Rule tests |
| 29 | +│ ├── Symfony/ # Infrastructure tests |
| 30 | +│ └── Type/Symfony/ # Type extension tests (with data/ subdirectories) |
| 31 | +├── stubs/ # Type stubs for Symfony and PSR classes |
| 32 | +├── extension.neon # Main extension config (type extensions, stubs, services) |
| 33 | +├── rules.neon # Rule registrations |
| 34 | +├── phpstan.neon # PHPStan config for analysing this project itself |
| 35 | +├── phpstan-baseline.neon # Baseline of accepted PHPStan errors |
| 36 | +├── phpunit.xml # PHPUnit configuration |
| 37 | +├── composer.json # Dependencies and autoloading |
| 38 | +└── Makefile # Build/check commands |
| 39 | +``` |
| 40 | + |
| 41 | +### Source Code Architecture |
| 42 | + |
| 43 | +- **`src/Rules/Symfony/`** - PHPStan `Rule` implementations that report errors (private service access, unknown services, undefined console arguments/options, invalid defaults) |
| 44 | +- **`src/Symfony/`** - Infrastructure for parsing Symfony's container XML dump into service/parameter maps. Contains `ServiceMap`, `ParameterMap`, `MessageMap`, `ConsoleApplicationResolver`, and related factories/interfaces |
| 45 | +- **`src/Type/Symfony/`** - Dynamic return type extensions (`DynamicMethodReturnTypeExtension`, `TypeSpecifyingExtension`) that teach PHPStan the return types of Symfony methods. Organized by feature area (console, config, form, etc.) |
| 46 | +- **`stubs/`** - `.stub` files providing type information for Symfony, PSR, and Twig classes. Registered via `extension.neon` |
| 47 | + |
| 48 | +### Configuration Files |
| 49 | + |
| 50 | +- **`extension.neon`** - Registers all type extensions, stubs, and services. Defines the `symfony` parameter schema (`containerXmlPath`, `constantHassers`, `consoleApplicationLoader`) |
| 51 | +- **`rules.neon`** - Registers the 6 PHPStan rules |
| 52 | +- **`phpstan.neon`** - Config for self-analysis at level 8, includes strict rules, phpunit extension, and bleeding edge |
| 53 | + |
| 54 | +## PHP Version Support |
| 55 | + |
| 56 | +This repository supports **PHP 7.4+** (see `composer.json`: `"php": "^7.4 || ^8.0"`). All code must be compatible with PHP 7.4. Do not use language features introduced in PHP 8.0+ (named arguments, match expressions, union types in signatures, etc.). |
| 57 | + |
| 58 | +## Symfony Version Compatibility |
| 59 | + |
| 60 | +The extension supports multiple Symfony versions: |
| 61 | +- Conflicts with `symfony/framework-bundle` below 3.0 |
| 62 | +- Dev dependencies target `^5.4 || ^6.1` for most components |
| 63 | +- Tests use `class_exists()` / `interface_exists()` checks to conditionally skip tests for features unavailable in certain Symfony versions |
| 64 | + |
| 65 | +## Development Commands |
| 66 | + |
| 67 | +All commands are defined in the `Makefile`: |
| 68 | + |
| 69 | +```bash |
| 70 | +make check # Run all checks (lint, cs, tests, phpstan) |
| 71 | +make tests # Run PHPUnit tests |
| 72 | +make lint # Run PHP parallel lint on src/ and tests/ |
| 73 | +make cs # Check coding standards (requires build-cs, see below) |
| 74 | +make cs-fix # Auto-fix coding standard violations |
| 75 | +make cs-install # Clone and set up phpstan/build-cs repository |
| 76 | +make phpstan # Run PHPStan analysis at level 8 |
| 77 | +make phpstan-generate-baseline # Regenerate the PHPStan baseline file |
| 78 | +``` |
| 79 | + |
| 80 | +### Coding Standards Setup |
| 81 | + |
| 82 | +Coding standards use the [phpstan/build-cs](https://github.com/phpstan/build-cs) repository (branch `2.x`): |
| 83 | + |
| 84 | +```bash |
| 85 | +make cs-install # Clone build-cs and install its dependencies |
| 86 | +make cs # Run the coding standards check |
| 87 | +``` |
| 88 | + |
| 89 | +## Testing |
| 90 | + |
| 91 | +- **Framework**: PHPUnit 9.6 |
| 92 | +- **Bootstrap**: `tests/bootstrap.php` (requires autoloader) |
| 93 | +- **Test discovery**: All `*Test.php` files under `tests/` |
| 94 | + |
| 95 | +### Test Patterns |
| 96 | + |
| 97 | +- **Rule tests** extend `RuleTestCase` and use `$this->analyse()` to test file/error pairs |
| 98 | +- **Type tests** extend `TypeInferenceTestCase` and use assertion functions (`assertType()`) in data files under `tests/Type/Symfony/data/` |
| 99 | +- Tests conditionally skip based on available Symfony classes/interfaces to support multiple Symfony versions |
| 100 | +- Test container XML files (`container.xml`) in test directories provide fixture service definitions |
| 101 | +- Console application loaders (`console_application_loader.php`) provide fixture console apps for command tests |
| 102 | + |
| 103 | +### Running Tests |
| 104 | + |
| 105 | +```bash |
| 106 | +composer install |
| 107 | +make tests |
| 108 | +``` |
| 109 | + |
| 110 | +## Static Analysis |
| 111 | + |
| 112 | +The project analyses itself with PHPStan at **level 8** using strict rules and bleeding edge features: |
| 113 | + |
| 114 | +```bash |
| 115 | +make phpstan |
| 116 | +``` |
| 117 | + |
| 118 | +The baseline file (`phpstan-baseline.neon`) tracks accepted errors, primarily internal API usage required for the extension to function. |
| 119 | + |
| 120 | +## CI Pipeline |
| 121 | + |
| 122 | +GitHub Actions (`.github/workflows/build.yml`) runs on PRs and pushes to `2.0.x`: |
| 123 | + |
| 124 | +- **Lint**: PHP 7.4 - 8.5 |
| 125 | +- **Coding Standard**: PHP 8.2 |
| 126 | +- **Tests**: PHP 7.4 - 8.5, both lowest and highest dependency versions |
| 127 | +- **PHPStan**: PHP 7.4 - 8.5, both lowest and highest dependency versions |
| 128 | + |
| 129 | +## Autoloading |
| 130 | + |
| 131 | +- **PSR-4**: `PHPStan\` namespace maps to `src/` |
| 132 | +- **Dev classmap**: `tests/` directory (classmap autoloading) |
| 133 | + |
| 134 | +## Extension Registration |
| 135 | + |
| 136 | +The extension auto-registers via `composer.json` `extra.phpstan.includes` when used with [phpstan/extension-installer](https://github.com/phpstan/extension-installer). Both `extension.neon` and `rules.neon` are included automatically. |
| 137 | + |
| 138 | +## Key Concepts |
| 139 | + |
| 140 | +### Service Map |
| 141 | + |
| 142 | +The extension reads Symfony's compiled container XML dump to build a map of services. This enables detection of unknown/private services and correct return types for `get()` calls. The `ServiceMap` interface has two implementations: |
| 143 | +- `DefaultServiceMap` - populated from XML parsing |
| 144 | +- `FakeServiceMap` - no-op fallback when no container XML is configured |
| 145 | + |
| 146 | +### Parameter Map |
| 147 | + |
| 148 | +Similar to ServiceMap, reads container parameters from the XML dump for type-aware `getParameter()` return types. |
| 149 | + |
| 150 | +### Console Application Resolver |
| 151 | + |
| 152 | +When configured with `consoleApplicationLoader`, loads the actual Symfony console application to discover command definitions. This enables type inference for `$input->getArgument()` and `$input->getOption()` calls. |
| 153 | + |
| 154 | +### Type Extensions vs Rules |
| 155 | + |
| 156 | +- **Type extensions** (in `src/Type/Symfony/`) modify return types — they make PHPStan understand what Symfony methods return |
| 157 | +- **Rules** (in `src/Rules/Symfony/`) report errors — they detect incorrect usage patterns |
| 158 | + |
| 159 | +### Stubs |
| 160 | + |
| 161 | +Stub files in `stubs/` provide PHPStan with type information (generics, more precise signatures) for Symfony classes that may not have native type declarations. These are `.stub` files using PHPDoc annotations. |
0 commit comments