How to Find Dead Symfony Routes

2 min

Mon, Apr 6

Updated Apr, 2020

X comments

Edit Post

This post was updated at April 2020 Route visit table structure has changed and new command show-dead-routes was added.

Almost half a year ago, I spotted a post called Route Usage Package for Laravel. It's nice to have to see what routes are used and how often.



But when dealing with legacy code, knowing dead routes will save you dozens of hours in refactoring.

Why are Dead Routes Important for Your Code?

If you know that 20 % is never used, you can drop it and ease your maintenance. Also, it's a must-have pre-step to code migrations.

Static analysis can help you with dead code that is never used and with public methods that are never called, e.g.:

-$discount = $this->getDiscount(); $productCategory = $this->categoryRepository->findCategoriesByProduct( $product->getCategory() ); $discount = $this->getDiscount();





10-20 % of code in most PHP code bases

is dead and can be deleted.

In this case study from Spaceflow project we worked in summer 2019, we removed ~20 % of the code... and nobody noticed.

Static Analysis of Dead Code is Not Enough

It's quite easy to find dead calls, but when it comes to controller and API endpoints, it's a different game. Controller methods are public and are called by a framework.

Static analysis won't tell us, which controller is used and which not. Also, the controller might call 2-3 other services... and they might call other services... and those could be dead too... welcome fractal of dead code we have to maintain. What now?

We'll have to use similar approach we used for From 0 Doc Types to Full Type Declaration.

Such branch is in your code... somewhere. Would you still water it for next couple years?

Coffee Shop - Static vs. Dynamic Analysis

Imagine you're a coffee franchise owner. Not just 1, but 30 coffee houses. Suddenly, there comes corona, and shops have to be locked down. Luckily, you have an emergency fund to keep them running... well, just 15 of them. Which one you choose to close?

Coffee shop with a property of specific size, location, number of chairs, and toilets - that's static analysis. It won't help us here. What if the shop with the smallest area is making more money than the biggest one?

Let's use dynamic analysis here - you measure data in time and decide based on it. E.g., income for last year from all of them compared to expenses to run them.

2 Steps to add Route Usage to Your Symfony App

Inspired by the Laravel package, I've made a Symfony Route Usage.

1. Install it

composer require migrify/symfony-route-usage

2. Enable Bundle

// config/bundles.php return [ Migrify\SymfonyRouteUsage\SymfonyRouteUsageBundle::class => ['all' => true], ];





Collect data for a couple of weeks (depends on the size of your site) and see for yourself, what routes have been used:

bin/console show-route-usage

↓





What Routes Have Never Been Used?

Just run:

bin/console show-dead-routes

↓





What about the performance? Pehapkari website takes 72 ms to load, of which 10 ms (13 %) is Symfony Route Visit. If that would be too much for your website, add partial logging by overloading the subscriber - e.g., every 100th or 1000th request.





This package is freshly baked. Do you have some ideas how to make it better? Let me know ↓





Happy coding!