Skip to content

Unused Global Middleware Analyzer ​

Analyzer IDCategorySeverityTime To Fix
unused-global-middleware⚡ PerformanceLow10 minutes

What This Checks ​

Detects global middleware that runs on every request but provides no benefit due to missing configuration.

Specifically checks for:

MiddlewareFlagged whenLaravel versions
TrustProxiesNo proxies configured ($proxies property or proxies() method empty, no trustedproxy.proxies config)9–10
HandleCorsNo paths configured in config/cors.php9+

Laravel 11+

TrustProxies is a framework-level default in Laravel 11+ and is not flagged. Only HandleCors is checked.

TrustHosts is never flagged: it validates the Host header on its own and does not depend on TrustProxies.

If the container cannot build the registered TrustProxies middleware, the analyzer warns that it could not be inspected instead of passing.

Why It Matters ​

  • Performance: Unnecessary middleware adds latency to every request
  • Efficiency: Dead code wastes CPU cycles on every single HTTP request
  • Maintenance: Clutters the middleware stack with components that do nothing

How to Fix ​

Quick Fix (5 minutes) ​

Configure the flagged middleware so it runs with a purpose:

HandleCors — add CORS paths:

php
// config/cors.php
return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],
    // ...
];

TrustProxies — configure your proxy (Laravel 9–10):

php
// app/Http/Middleware/TrustProxies.php
protected $proxies = '*'; // or specific IPs: ['192.168.1.1']

Proper Fix (10 minutes) ​

If you don't use the middleware's feature, remove it entirely rather than adding placeholder configuration.

HandleCors — remove if you have no CORS requirements:

php
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
    $middleware->remove(\Illuminate\Http\Middleware\HandleCors::class);
})
php
// app/Http/Kernel.php
protected $middleware = [
    // Comment out or remove:
    // \Fruitcake\Cors\HandleCors::class,
];

TrustProxies — remove if you are not behind a proxy (Laravel 9–10):

php
// app/Http/Kernel.php
protected $middleware = [
    // Comment out or remove:
    // \App\Http\Middleware\TrustProxies::class,
];

References ​