Env Calls Outside Config Analyzer
| Analyzer ID | Category | Severity | Time To Fix |
|---|---|---|---|
env-call-outside-config | ⚡ Performance | High | 30 minutes |
What This Checks
Detects direct env() calls outside config files, which breaks config caching and causes unpredictable behavior.
Why It Matters
- Performance: Config caching doesn't work with env() calls
- Reliability: env() returns null when config is cached
- Best Practice: Violates Laravel's configuration patterns
When configuration is cached, env() always returns null outside config files. This causes features to break mysteriously in production.
How to Fix
Quick Fix (5 minutes)
Replace env() calls with config() calls:
Before:
php
// ❌ app/Services/PaymentService.php
$apiKey = env('STRIPE_KEY');After:
php
// ✅ app/Services/PaymentService.php
$apiKey = config('services.stripe.key');
// config/services.php
return [
'stripe' => [
'key' => env('STRIPE_KEY'),
],
];Proper Fix (30 minutes)
Migrate All env() Calls:
- Find all env() calls:
bash
grep -r "env(" app/ --exclude-dir=vendor- Create config entries:
php
// config/myapp.php
return [
'api_key' => env('MYAPP_API_KEY'),
'enabled' => env('MYAPP_ENABLED', false),
];- Update code:
php
// ❌ Before
if (env('FEATURE_ENABLED')) {
// ...
}
// ✅ After
if (config('myapp.enabled')) {
// ...
}References
Related Analyzers
- Configuration Caching Analyzer - Ensures config is cached in production