ekino/phpstan-banned-code
PHPStan extension to ban unwanted code in your project. Detects calls like var_dump, dd, eval, exit/die, echo/print, shell exec/backticks, and even “use” imports from Tests in non-test files. Configurable rules for CI enforcement.
phpstan.neon configuration paradigm.dd(), dump(), exit() in controllers/middleware) and integrates with Laravel’s debug mode (APP_DEBUG) for context-aware enforcement.echo in loops but allowing print in CLI scripts), enabling alignment with team-specific coding standards.| Risk | Mitigation |
|---|---|
| False Positives | Configure non_ignorable: false and whitelist exceptions in .neon; use PHPStan’s baseline feature to suppress known issues temporarily. |
| Performance Impact | Minimal; AST analysis runs during static checks (not runtime). Benchmark with phpstan analyze --generate-report to validate overhead (<5% in tests). |
| Configuration Complexity | Provide team-wide templates for .neon files (e.g., phpstan-banned-code.neon.example) and document common use cases (e.g., banning dd() in production-only builds). |
| PHPStan Version Lock | Pin PHPStan version in composer.json (e.g., ^1.10) to avoid breaking changes. Monitor PHPStan’s BC policy. |
| Custom AST Nodes | Extend the package via custom rules (PHPStan’s addRule()) if new banned nodes are needed (e.g., Laravel-specific abort() in production). |
| CI Pipeline Failures | Use GitHub/GitLab’s "required checks" to enforce the rule without blocking all PRs; add a /phpstan-banned-code-allow comment for exceptions. |
production vs. staging)? Requires .neon configuration per environment.dd() in CLI tools) be handled? Options:
/* @phpstan-ignore-next-line */).dd() in tests/ but ban in app/).var_dump in 10K+ LOC) be handled? Options:
non_ignorable: false + baseline.sed/Rector to replace echo with response()->json()).laravel-debugbar functions)? Assign a tech lead or use community-maintained configs (e.g., phpstan-preset).nikic/PHP-Parser for bespoke rules (higher maintenance).dd(), dump()), middleware, and CLI tools. Works alongside:
phpstan/extension-installer).grep -r "var_dump\|dd\|exec")..neon with minimal rules (e.g., ban var_dump, exit).non_ignorable: true)..neon based on false positives.dd()-related bugs).| Component | Compatibility |
|---|---|
| PHP Versions | 8.0–8.4 (tested via GitHub Actions). Laravel 9+ recommended. |
| PHPStan Versions | 1.x and 2.x (v3.0.0+ of this package). |
| Laravel Versions | 8.x–11.x (no core changes needed). |
| CI Systems | GitHub Actions, GitLab CI, CircleCI (Composer-based execution). |
| IDE Support | Works with PHPStorm, VSCode (PHP Intelephense), and PhpStorm’s PHPStan plugin for real-time feedback. |
| Monorepos | Supports multi-repo setups via global PHPStan config or per-repo .neon overrides. |
| Docker/Containerized | Runs in any PHP environment (no OS dependencies). |
composer require --dev phpstan/phpstan).composer require --dev ekino/phpstan-banned-code
phpstan/extension-installer (auto-configures).phpstan.neon:
includes:
- vendor/ekino/phpstan-banned-code/extension.neon
extension.neon or override in project’s phpstan.neon:
parameters:
banned_code:
nodes:
- { type: Expr_FuncCall, functions: ["dd", "
How can I help you explore Laravel packages today?