|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code when working with this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**phpstan/phpstan-nette** is a PHPStan extension that provides static analysis support for the [Nette Framework](https://nette.org/). It teaches PHPStan to understand Nette-specific patterns such as dynamic method return types, magic properties, component model conventions, and framework-specific coding rules. |
| 8 | + |
| 9 | +The extension is distributed via Composer as `phpstan/phpstan-nette` and is of type `phpstan-extension`. |
| 10 | + |
| 11 | +## PHP Version Support |
| 12 | + |
| 13 | +This repository supports **PHP 7.4+** (not just PHP 8.1+ like phpstan-src). All code must be compatible with PHP 7.4, 8.0, 8.1, 8.2, 8.3, and 8.4. Do not use language features unavailable in PHP 7.4. |
| 14 | + |
| 15 | +## Nette Version Support |
| 16 | + |
| 17 | +The extension supports multiple versions of Nette libraries: |
| 18 | +- `nette/application` ^3.0 (conflicts with <2.3.0) |
| 19 | +- `nette/di` ^3.0 (conflicts with <2.3.0) |
| 20 | +- `nette/forms` ^3.0 (conflicts with <2.3.0) |
| 21 | +- `nette/utils` ^2.3.0 || ^3.0.0 || ^4.0 (conflicts with <2.3.0) |
| 22 | + |
| 23 | +CI runs tests with both `--prefer-lowest` and highest dependency versions to ensure compatibility across all supported Nette versions. |
| 24 | + |
| 25 | +## Repository Structure |
| 26 | + |
| 27 | +``` |
| 28 | +├── extension.neon # Main extension config (type extensions, reflection, stubs) |
| 29 | +├── rules.neon # Optional rules config (framework-specific rules) |
| 30 | +├── src/ |
| 31 | +│ ├── Type/Nette/ # Dynamic return type extensions (12 files) |
| 32 | +│ ├── Reflection/Nette/ # Class reflection extensions (6 files) |
| 33 | +│ ├── Rule/Nette/ # PHPStan rules (4 files) |
| 34 | +│ └── Stubs/Nette/ # Stub file loader (1 file) |
| 35 | +├── stubs/ # PHPStan stub files for Nette classes (26 files) |
| 36 | +├── tests/ |
| 37 | +│ ├── Type/Nette/ # Type extension tests + data/ |
| 38 | +│ ├── Reflection/Nette/ # Reflection extension tests |
| 39 | +│ ├── Rule/Nette/ # Rule tests + data/ |
| 40 | +│ └── bootstrap.php # Test bootstrap (autoloader only) |
| 41 | +├── phpstan.neon # PHPStan config for analysing this project itself |
| 42 | +├── phpunit.xml # PHPUnit configuration |
| 43 | +├── Makefile # Build commands |
| 44 | +└── composer.json # Package definition |
| 45 | +``` |
| 46 | + |
| 47 | +## Key Concepts |
| 48 | + |
| 49 | +### Extension Types |
| 50 | + |
| 51 | +1. **Dynamic Return Type Extensions** (`src/Type/Nette/`): Teach PHPStan the return types of Nette methods that depend on arguments or context (e.g., `Container::getComponent()` return type based on `createComponent*` methods, `ServiceLocator::getByType()` based on class string argument). |
| 52 | + |
| 53 | +2. **Class Reflection Extensions** (`src/Reflection/Nette/`): Make PHPStan understand magic properties and methods on `Nette\Utils\Html` and `Nette\SmartObject`/`Nette\Object` classes. |
| 54 | + |
| 55 | +3. **Rules** (`src/Rule/Nette/`): Framework-specific static analysis rules: |
| 56 | + - `DoNotExtendNetteObjectRule` — Forbids extending deprecated `Nette\Object` |
| 57 | + - `RethrowExceptionRule` — Ensures exceptions like `AbortException` are rethrown |
| 58 | + - `RegularExpressionPatternRule` — Validates regex patterns in Nette Strings methods |
| 59 | + - `PresenterInjectedPropertiesExtension` — Read/write extension for `@inject` properties |
| 60 | + |
| 61 | +4. **Stub Files** (`stubs/`): Provide more precise type information for Nette classes than the original source code. Referenced in `extension.neon` under `parameters.stubFiles`. |
| 62 | + |
| 63 | +### Configuration Files |
| 64 | + |
| 65 | +- **`extension.neon`**: Loaded automatically by phpstan/extension-installer. Registers all type extensions, reflection extensions, stub files, early terminating methods, and universal object crates. |
| 66 | +- **`rules.neon`**: Optional, must be explicitly included. Registers framework-specific rules and defines which Nette methods throw exceptions. |
| 67 | + |
| 68 | +## Common Commands |
| 69 | + |
| 70 | +```bash |
| 71 | +# Run all checks (lint, coding standard, tests, PHPStan analysis) |
| 72 | +make check |
| 73 | + |
| 74 | +# Run tests only |
| 75 | +make tests |
| 76 | + |
| 77 | +# Run PHPStan analysis on this project at level 8 |
| 78 | +make phpstan |
| 79 | + |
| 80 | +# Run PHP parallel lint |
| 81 | +make lint |
| 82 | + |
| 83 | +# Run coding standard checks (requires build-cs setup) |
| 84 | +make cs-install # First time: clone and install build-cs |
| 85 | +make cs # Check coding standard |
| 86 | +make cs-fix # Auto-fix coding standard violations |
| 87 | +``` |
| 88 | + |
| 89 | +## Testing Patterns |
| 90 | + |
| 91 | +Tests use PHPStan's built-in testing infrastructure. There are three patterns: |
| 92 | + |
| 93 | +### Type Inference Tests |
| 94 | +- Extend `PHPStan\Testing\TypeInferenceTestCase` |
| 95 | +- Data files in `tests/Type/Nette/data/` contain PHP code with `assertType()` calls |
| 96 | +- Test methods yield from `$this->gatherAssertTypes(__DIR__ . '/data/file.php')` |
| 97 | +- Additional PHPStan config loaded via `getAdditionalConfigFiles()` |
| 98 | + |
| 99 | +### Reflection Extension Tests |
| 100 | +- Extend `PHPStan\Testing\PHPStanTestCase` |
| 101 | +- Create reflection provider with `$this->createReflectionProvider()` |
| 102 | +- Test `hasMethod()`, `getMethod()`, `hasProperty()`, `getProperty()` on class reflections |
| 103 | +- Use `@dataProvider` for parameterized tests |
| 104 | + |
| 105 | +### Rule Tests |
| 106 | +- Extend `PHPStan\Testing\RuleTestCase<RuleClass>` |
| 107 | +- Implement `getRule()` to return the rule instance |
| 108 | +- Call `$this->analyse([files], [[message, line], ...])` to verify expected errors |
| 109 | +- Test data files in `tests/Rule/Nette/data/` |
| 110 | + |
| 111 | +## CI Pipeline |
| 112 | + |
| 113 | +GitHub Actions workflow (`.github/workflows/build.yml`) on the `2.0.x` branch runs: |
| 114 | +1. **Lint** — PHP parallel lint on PHP 7.4–8.4 |
| 115 | +2. **Coding Standard** — phpcs via phpstan/build-cs (2.x branch) |
| 116 | +3. **Tests** — PHPUnit on PHP 7.4–8.4 with lowest and highest dependencies |
| 117 | +4. **Static Analysis** — PHPStan level 8 on PHP 7.4–8.4 with lowest and highest dependencies |
| 118 | + |
| 119 | +## Development Guidelines |
| 120 | + |
| 121 | +- The main development branch is `2.0.x`. |
| 122 | +- PSR-4 autoloading: namespace `PHPStan\` maps to `src/`. |
| 123 | +- Tests use classmap autoloading from `tests/`. |
| 124 | +- PHPStan analysis runs at **level 8** with strict rules, phpunit extension, and deprecation rules. |
| 125 | +- Test data directories (`tests/*/data/*`) are excluded from PHPStan analysis. |
| 126 | +- The coding standard is defined externally in `phpstan/build-cs` (2.x branch). |
0 commit comments