smnandre/twigmetrics

Twig template analyzer for PHP: structure, style, complexity, and maintainability metrics at a glance

Fund package maintenance!
smnandre

Installs: 18

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 0

Forks: 1

Open Issues: 0

Type:project

v1.1.0 2025-08-28 20:05 UTC

This package is auto-updated.

Last update: 2025-09-05 17:22:42 UTC


README

Twig ๐ŸŒฟ Metrics

Analyze your Twig templates and get a clear picture of their most important metrics

Twig Metrics

ย  PHP Version ย  CI ย  Release ย  GitHub Sponsors ย  License

Warning

TwigMetrics is in active development. Use with caution, and expect things to change quickly.

This project is a melting pot of code or ideas I had while working on various Symfony projects. I'm releasing it as a standalone tool in case it can be useful to others.

Depending on the level of interest and feedback, I have plans to expand it into a more reliable and feature-rich tool.

What it does

TwigMetrics scans a directory of .twig templates and produces a full report:

  • Template overview: number of files, directories, lines, characters
  • Code style: average line length, comment ratio, indentation, formatting
  • Callables: functions, filters, variables, macros, blocks, tests
  • Architecture: includes, extends, embeds, imports, inheritance depth
  • Complexity: logical depth, IF/ELSE/LOOP counts, nesting
  • Maintainability: large files, high complexity hotspots, risk scores
  • Visual output: ASCII tables and gauges directly in your terminal
$ bin/twigmetrics analyze templates/


  โ•ญโ”€ Template Files โ”€โ”€โ”€โ”€โ”€โ•ฎ  โ•ญโ”€ Logical Comp... โ”€โ”€โ”€โ”€โ•ฎ  โ•ญโ”€ Twig Callables โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚  โ— โ— โ— โ— โ—‹ โ—‹      C  โ”‚  โ”‚  โ— โ— โ— โ— โ— โ—‹      B  โ”‚  โ”‚  โ— โ— โ— โ— โ— โ—‹      B  โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

  โ•ญโ”€ Code Style โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ  โ•ญโ”€ Architecture โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ  โ•ญโ”€ Maintainability โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚  โ— โ— โ— โ— โ— โ—‹      C  โ”‚  โ”‚  โ— โ— โ— โ— โ— โ—‹      B  โ”‚  โ”‚  โ— โ— โ— โ— โ— โ—      A+ โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

See examples of each section in the Usage section below.

Installation

Global (soon)

Tip

A PHAR release is planned, so youโ€™ll be able to install it globally.

Composer

composer require --dev smnandre/twigmetrics

From source

git clone https://github.com/smnandre/twigmetrics.git
cd twigmetrics
composer install

Usage

To analyze a directory of Twig templates, run:

vendor/bin/twigmetrics path/to/templates

Twig Metrics

Template Files

  โ•ญโ”€ Template Files โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚                                                                            โ”‚
  โ”‚   Total Templates ........... 188      Total Lines .............. 11,213   โ”‚
  โ”‚   Average Lines/File ....... 59.6      Median Lines ................. 48   โ”‚
  โ”‚   Size Coefficient (CV) .... 0.77      Gini Index ................ 0.380   โ”‚
  โ”‚   Directories ................ 19      Characters ............... 503.8k   โ”‚
  โ”‚                                                                            โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Logical Complexity

  โ•ญโ”€ Logical Complexity โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚                                                                            โ”‚
  โ”‚   Avg Complexity ............. 8.3      Max Complexity .............. 79   โ”‚
  โ”‚   Avg Depth .................. 1.2      Max Depth .................... 6   โ”‚
  โ”‚   IFs/Template ............... 1.3      FORs/Template .............. 0.6   โ”‚
  โ”‚                                                                            โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Twig Callables

  โ•ญโ”€ Twig Callables โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚                                                                            โ”‚
  โ”‚   Total Calls ............. 4,632      Unique Functions ............. 23   โ”‚
  โ”‚   Unique Filters ............. 32      Unique Tests .................. 7   โ”‚
  โ”‚   Funcs/Template ............ 2.9      Filters/Template ........... 18.9   โ”‚
  โ”‚                                                                            โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Code Style

  โ•ญโ”€ Code Style โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚                                                                            โ”‚
  โ”‚   Avg Line Length ........... 41.0      Max Line Length ............ 217   โ”‚
  โ”‚   Indent Consistency ...... 100.0%      P95 Length ................. 217   โ”‚
  โ”‚   Consistency Score ........ 92.7%      Style Violations ........... 128   โ”‚
  โ”‚   Comments/Template .......... 0.6      Mixed Indentation ............ 0   โ”‚
  โ”‚                                                                            โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Architecture

  โ•ญโ”€ Architecture โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚                                                                            โ”‚
  โ”‚   Imports/Template ......... 0.00      Extends/Template ........... 0.22   โ”‚
  โ”‚   Avg Inherit Depth ......... 0.2      Includes/Template .......... 0.57   โ”‚
  โ”‚   Embeds/Template .......... 0.04      Blocks/Template ............ 1.13   โ”‚
  โ”‚                                                                            โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Maintainability

  โ•ญโ”€ Maintainability โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚                                                                            โ”‚
  โ”‚   Empty Lines Ratio ....... 10.0%      MI Average ................ 107.2   โ”‚
  โ”‚   MI Median ............... 106.7      Comment Density ............ 1.3%   โ”‚
  โ”‚   High Risk ................... 3      Medium Risk .................. 40   โ”‚
  โ”‚                                                                            โ”‚
  โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Contributing

Feedback, issues, and pull requests are very welcome!

License

Twig Metrics is licensed under the MIT License. See the LICENSE file for details.