Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unused TWIG layouts in a Symfony project

Tags:

twig

symfony

tl;dr

Is there in the Symfony ecosystem an existing tool specifically meant to find unused Twig layouts, i.e. all layout files which are not included / embedded in other layouts nor rendered by any controller in the project?

Details

After a huge frontend refactoring task on a big project, it may take a while and/or some trial-and-error sessions to find which layout files are not used anymore.

I was wondering if there is an existing tool / command that shows a list of (possibly) unused layout files. Of course layout names can be dynamically created so the list would still need to be carefully checked, but still it seems like that could save some time.

Thank you in advance for any answer.

like image 530
Francesco Abeni Avatar asked Jul 04 '18 11:07

Francesco Abeni


2 Answers

Based on the docs (https://symfony.com/doc/current/index.html) the answer is no there is no tool/feature to find unused templates in symfony even if there may be other methods to find them.

like image 174
Jim Panse Avatar answered Nov 16 '22 01:11

Jim Panse


In my quest, I finally made a script to find twig templates not referenced in templates/ or src/ files:

APP_PATH=./$1
TWIG_PATH=${APP_PATH}/templates

# find all files in TWIG_PATH, remove TWIG_PATH from the string
for i in $( find $TWIG_PATH  -type f -not -path '*/\.*' -exec echo {}  \; | cut -b $((${#TWIG_PATH}+2))-  ); do
    # search for the path in src and templates
    if ! grep "$i" ${APP_PATH}/src ${APP_PATH}/templates -r -hc -m1 | grep -qv 0 
    then 
        echo $i;
    fi
done
like image 21
Matrhack Avatar answered Nov 16 '22 00:11

Matrhack