smnandre/twigmetrics
Composer 安装命令:
composer create-project smnandre/twigmetrics
包简介
Twig template analyzer for PHP: structure, style, complexity, and maintainability metrics at a glance
关键字:
README 文档
README
Twig 🌿 Metrics
Analyze your Twig templates and get a clear picture of their most important metrics
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!
- Issues: github.com/smnandre/twigmetrics/issues
- Pull Requests: github.com/smnandre/twigmetrics/pulls
License
Twig Metrics is licensed under the MIT License. See the LICENSE file for details.
smnandre/twigmetrics 适用场景与选型建议
smnandre/twigmetrics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 712 次下载、GitHub Stars 达 8, 最近一次更新时间为 2025 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「cli」 「template」 「Metrics」 「static-analysis」 「complexity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 smnandre/twigmetrics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 smnandre/twigmetrics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 smnandre/twigmetrics 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Twig extensions for Tracy Debugger
Provides code-quality helpers to your Twig templates.
统计信息
- 总下载量: 712
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 32
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-24