PHPackages                             alisqi/twigqi - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. alisqi/twigqi

ActiveLibrary[Testing &amp; Quality](/categories/testing)

alisqi/twigqi
=============

Static code analysis for Twig templates

3.1.1(8mo ago)3944.4k↓30.9%3[1 issues](https://github.com/alisqi/TwigQI/issues)MITPHPPHP ^8.2CI passing

Since Oct 25Pushed 3mo ago7 watchersCompare

[ Source](https://github.com/alisqi/TwigQI)[ Packagist](https://packagist.org/packages/alisqi/twigqi)[ RSS](/packages/alisqi-twigqi/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (19)Used By (0)

TwigQI: Static code analysis for Twig templates
===============================================

[](#twigqi-static-code-analysis-for-twig-templates)

[![License](https://camo.githubusercontent.com/5c82692cfde8219a75088d03e3a875e32984f8505ddc8bd9968a8c5318942b4f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f616c697371692f5477696751492e737667)](https://github.com/alisqi/TwigQI/blob/main/LICENSE)[![PHP Version](https://camo.githubusercontent.com/ec21f169d70b69344c67d6f18fa1a24d20476d2f0cd680e8c4a1534c22f34e5f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e322d3838393242462e737667)](https://php.net)[![Stability](https://camo.githubusercontent.com/ab8ef3fe8abaa0d51cd12f22a8a8159f8022515a030b700ab4a35367d281028f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73746162696c6974792d737461626c652d627269676874677265656e)](https://camo.githubusercontent.com/ab8ef3fe8abaa0d51cd12f22a8a8159f8022515a030b700ab4a35367d281028f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73746162696c6974792d737461626c652d627269676874677265656e)[![Build Status](https://github.com/alisqi/TwigQI/actions/workflows/test.yml/badge.svg)](https://github.com/alisqi/TwigQI/actions)[![codecov](https://camo.githubusercontent.com/6ccad3020b9a1d7b0a2572dc616ee1e06f91c2e7496d2d2c0db73c7bb10e1c3b/68747470733a2f2f636f6465636f762e696f2f67682f616c697371692f5477696751492f67726170682f62616467652e7376673f746f6b656e3d47334145335245344b30)](https://codecov.io/gh/alisqi/TwigQI)

Twig Quality Inspections is an extension to the [Twig templating](https://github.com/twigphp/Twig) engine which adds static analysis (i.e., compile-time) inspections and runtime assertions to increase templates' quality. See the [inspections section](#Inspections) below for details.

The two intended use cases are:

- Add the extension to the `Twig\Environment` during development
- Invoke a CLI command in CI and/or pre-commit hook which compiles all templates with the extension enabled.

Justification
=============

[](#justification)

Just in case you need convincing, please consider the following example:

```
{% macro userCard(user, showBadge = false) %}
  {{ user.name }}
  {% if showBadge %}
    {% if usr.admin %} {# Oops #}
      (admin)
    {% else if user.role %}
      ({{ user.getRoleLabel(usr.role) }}) {# Uh oh! #}
    {% endif %}
  {% endif %}
{% endmacro %}
```

Here, `usr.admin` is obviously a typo. Fortunately, this bug is easily detected with `strict_types` enabled, but only if the macro is called with `showBadge=true`. In this example, the `(admin)` badge will simply never be printed in production (where `strict_types` is likely disabled).

However, `user.getRoleLabel(usr.role)` will cause an uncaught `TypeError` if that method's parameter is not nullable, since Twig will call that method with `null`. Instead of just having a buggy badge, *the whole page breaks*.

Installation
============

[](#installation)

First, install using

```
composer require --dev alisqi/twigqi
```

Symfony integration
-------------------

[](#symfony-integration)

In a Symfony application, the recommended way is to create a class that extends `AlisQI\TwigQI\Extension` and add the `When` attribute. This allows you to configure which inspections to enable.

```
// src/Twig/TwigQIExtension.php
