ekino/phpstan-banned-code
PHPStan extension that flags banned code patterns in your project (e.g., var_dump, dd, exit/die, eval, echo/print, shell exec/backticks). Configurable via PHPStan parameters, with optional checks like preventing use imports from Tests in non-test code.
Install via Composer (dev dependency):
composer require --dev ekino/phpstan-banned-code
Enable via Extension Installer (recommended):
composer require --dev phpstan/extension-installer
This auto-includes the extension in your PHPStan config.
Manual Configuration (if not using installer):
Add to your phpstan.neon:
includes:
- vendor/ekino/phpstan-banned-code/extension.neon
Run PHPStan with default rules to catch var_dump, dd(), exit(), etc.:
./vendor/bin/phpstan analyse app
Example output:
[ERROR] app/Http/Controllers/ProductController.php:12:10 - Banned function call: var_dump()
Workflow:
banned_code.nodes in phpstan.neon to block debug functions:
parameters:
banned_code:
nodes:
- type: Expr_FuncCall
functions:
- dd
- dump
- var_dump
- print_r
- name: Run PHPStan with banned-code rules
run: ./vendor/bin/phpstan analyse --level=max --error-format=github
Pattern: Ban shell execution and eval:
parameters:
banned_code:
nodes:
- type: Expr_FuncCall
functions:
- exec
- shell_exec
- system
- passthru
- eval
- type: Expr_ShellExec # Backticks: ``
Laravel-Specific: Extend to block Artisan::call() in non-cli contexts:
- type: Expr_MethodCall
functions:
- Artisan::call
Use Case: Remove PHP 7.x deprecated functions (e.g., mysql_*):
parameters:
banned_code:
nodes:
- type: Expr_FuncCall
functions:
- mysql_connect
- mysql_query
- create_function
- call_user_func_array
Tip: Use --generate-baseline to exclude legacy files temporarily.
Pattern: Block use Tests\* in non-test files:
parameters:
banned_code:
use_from_tests: true
Laravel Example: Prevent test helpers in controllers:
- type: Stmt_Use
functions:
- Tests\Helpers\TestHelper
Example: Ban Log::debug() in production:
parameters:
banned_code:
nodes:
- type: Expr_MethodCall
functions:
- Log::debug
# Optional: Restrict to specific classes
classes:
- App\Services\Logger
False Positives in Generators:
yield or yield from as "banned" if misconfigured.type in config.Closure Scope Issues:
use statements may trigger use_from_tests rules.use_from_tests or whitelist closures:
parameters:
banned_code:
use_from_tests: false
Non-Ignorable Errors:
non_ignorable: false if you need to baseline exceptions.Performance Overhead:
--parallel or exclude directories:
phpstan analyse --exclude vendor,storage
Verbose Output:
Use --debug to see which nodes are being analyzed:
./vendor/bin/phpstan analyse --debug
Isolate Rules:
Test one rule at a time by commenting out sections in phpstan.neon.
Baseline Management:
--generate-baseline to exclude known violations:
phpstan analyse --generate-baseline
Custom Node Types:
Expr_StaticCall for ::method()).Add Custom Functions:
Extend the functions list in Expr_FuncCall for project-specific bans:
- type: Expr_FuncCall
functions:
- App\Helpers\Debug::log
Whitelist Directories:
Use PHPStan’s paths to exclude directories (e.g., tests):
paths:
- app
- src
Dynamic Configuration:
Load banned functions from a .env file or config:
// config/phpstan.php
return [
'banned_functions' => explode(',', env('BANNED_FUNCTIONS', 'dd,dump,var_dump')),
];
Then reference in phpstan.neon:
parameters:
banned_code:
nodes:
- type: Expr_FuncCall
functions: %banned_functions%
Facade Calls:
Ban Laravel facades (e.g., Log::debug) by targeting Expr_StaticCall:
- type: Expr_StaticCall
functions:
- Log::debug
- Cache::put
Blade Debugging:
Detect {{ dd($var) }} by analyzing Blade templates (requires custom rule extension).
Service Container:
Block app()->bind() or app()->singleton() in non-bootstrap files:
- type: Expr_MethodCall
functions:
- Illuminate\Container\Container::bind
php-cs-fixer to auto-fix echo/print statements, then ban them with this package.# .git/hooks/pre-commit
./vendor/bin/phpstan analyse --memory-limit=512M
How can I help you explore Laravel packages today?