prinsfrank/phpstan-doc-code-analyzer
Analyze and validate PHP code examples in your documentation using PHPStan. This package scans doc blocks and docs for code snippets, runs static analysis, and helps catch outdated or incorrect examples early in CI.
phpstan.neon with a new rule (PrinsFrank\DocCodeAnalyzer\Rule\DocCodeRule).includes:
- src/
- tests/
- docs/**/*.md # Target markdown files
services:
- PrinsFrank\DocCodeAnalyzer\Extension
$user->id where id is not statically analyzable) may trigger false positives.@var annotations or exclude paths for unanalyzable snippets.--parallel flag.docs/?README.md:42 uses deprecated Carbon::now()").$request->input('dynamic_key'))?src/ references in README.md).CONTRIBUTING.md, API docs).composer.json:
"require-dev": {
"prinsfrank/phpstan-doc-code-analyzer": "^1.0"
}
phpstan.neon to target docs (see Technical Evaluation).docs/legacy/).php-markdown internally. May struggle with custom markdown syntax (e.g., GitHub-flavored extensions).README.md, docs/installation.md).@var annotations).DOCS.md file.composer.json or fork if critical.docs/ section to the contributing guide with examples.line 10"). Enhance error messages with:
// Custom error formatter in phpstan.neon
errorLevel: Error
message: "Documentation snippet error in {file}:{line}. Expected type {expected}, got {actual}."
--parallel..phpstan.cache).| Failure Scenario | Impact | Recovery |
|---|---|---|
| CI pipeline fails on doc errors | Blocks PRs | Temporarily exclude failing files, fix later. |
| False positives in critical docs | Delays releases | Add @var annotations or suppress rules. |
| Package abandonment | Broken validation | Fork or replace with a custom solution. |
| Over-reliance on tool | Docs become overly rigid | Balance with manual reviews for high-level docs. |
@var annotations for dynamic values.How can I help you explore Laravel packages today?