@@ -10,20 +10,12 @@ Twig Quality Inspections is an extension to the [Twig templating](https://github
1010which adds static analysis (i.e., compile-time) inspections and runtime assertions to increase templates' quality.
1111See the [ inspections section] ( #Inspections ) below for details.
1212
13- Unlike other projects like [ curlylint] ( https://www.curlylint.org/ ) and [ djLint] ( https://www.djlint.com/docs/linter/ ) ,
14- which focus on HTML, this tool exclusively analyzes the Twig code.
13+
1514
1615The two intended use cases are:
1716* Add the extension to the ` Twig\Environment ` during development
1817* Invoke a CLI command in CI and/or pre-commit hook which compiles all templates with the extension enabled.
1918
20- > [ !NOTE]
21- > TwigQI is stable and should work in most codebases due to its simplicity. I would love to hear about your experience
22- > using it. Please create an issue or a pull request if you've found an issue. 🙏
23-
24- Note that TwigQI doesn't support every single edge case, plus it is a little opinionated. You've been warned! 😉
25- The good news is that you can easily create a bespoke suite by cherry-picking the inspections.
26-
2719# Justification
2820Just in case you need convincing, please consider the following example:
2921
@@ -41,9 +33,8 @@ Just in case you need convincing, please consider the following example:
4133```
4234
4335Here, ` usr.admin ` is obviously a typo. Fortunately, this bug is easily detected with ` strict_types ` enabled,
44- but only if the macro is called with ` showBadge=true ` , which might be uncommon enough to go unnoticed during
45- development. In this example, the ` (admin) ` badge will simply never be printed in production, where ` strict_types `
46- is likely disabled. A bug for sure, but perhaps not a critical one.
36+ but only if the macro is called with ` showBadge=true ` . In this example, the ` (admin) ` badge will simply never be printed in production
37+ (where ` strict_types ` is likely disabled).
4738
4839However, ` user.getRoleLabel(usr.role) ` will cause an uncaught ` TypeError ` if that method's parameter is not nullable,
4940since Twig will call that method with ` null ` . Instead of just having a buggy badge, * the whole page breaks* .
@@ -55,17 +46,11 @@ composer require --dev alisqi/twigqi
5546```
5647
5748## Symfony integration
58- In a Symfony application, you can enable the extension in your ` config\services.yaml `
59- ``` yaml
60- when@dev :
61- services :
62- AlisQI\TwigQI\Extension :
63- autowire : true
64- tags : [ 'twig.extension' ]
65- ` ` `
49+ In a Symfony application, the recommended way is to create a class that extends ` AlisQI\TwigQI\Extension ` and add the ` When ` attribute.
50+ This allows you to configure which inspections to enable.
6651
67- Alternatively, you can extend ` AlisQI\TwigQI\Extension` and add the `When` attribute.
6852``` php
53+ // src/Twig/TwigQIExtension.php
6954<?php
7055
7156namespace App\Twig;
@@ -74,11 +59,38 @@ use AlisQI\TwigQI\Extension;
7459use Symfony\Component\DependencyInjection\Attribute\When;
7560
7661#[When('dev')]
77- class QualityExtension extends Extension {}
62+ final class TwigQIExtension extends AbstractExtension
63+ {
64+ public function getNodeVisitors(): array
65+ {
66+ return [
67+ // Assertions:
68+ new WrapTypesInAssertedTypes(),
69+
70+ // Inspections:
71+ // new BadArgumentCountInMacroCall(), // Kills the Web Debug Toolbar: https://github.com/alisqi/TwigQI/issues/3#issuecomment-2651503912
72+ new InvalidConstant(),
73+ new InvalidDotOperation(),
74+ new InvalidTypes(),
75+ new PositionalMacroArgumentAfterNamed(),
76+ new RequiredMacroArgumentAfterOptional(),
77+ // new UndeclaredVariableInMacro(), // Kills the Web Debug Toolbar: https://github.com/alisqi/TwigQI/issues/3#issuecomment-2651503912
78+ ];
79+ }
80+ }
7881```
79- This allows you to configure which inspections to enable. See details below.
8082
81- # ## Logging
83+ Alternatively, if you want * all* inspections, you can enable the extension in your ` config/services.yaml ` :
84+
85+ ``` yaml
86+ when@dev :
87+ services :
88+ AlisQI\TwigQI\Extension :
89+ autowire : true
90+ tags : [ 'twig.extension' ]
91+ ` ` `
92+
93+ ### Output & Logging
8294Either way, all inspection results will [show up](docs/error-on-page.png) in the Web Debug Toolbar's logs.
8395
8496
@@ -99,18 +111,6 @@ $twig->addExtension(new AlisQI\TwigQI\Extension($logger));
99111` ` `
100112
101113# # Configuration
102- # ## Select inspections
103- You can cherry-pick your inspections (see below) :
104- ` ` ` php
105- use AlisQI\T wigQI\E xtension;
106- use AlisQI\T wigQI\I nspection\I nvalidConstant;
107- use AlisQI\T wigQI\I nspection\I nvalidEnumCase;
108-
109- new Extension($logger, [
110- InvalidConstant::class,
111- InvalidEnumCase::class,
112- ]);
113- ` ` `
114114
115115# ## Logging
116116The extension class requires a `\Psr\Log\LoggerInterface` implementation.
@@ -203,6 +203,11 @@ no explicit default value as required.
203203* ✅ Positional argument after named in call expression
204204* ⌛ Type mismatch in macro call
205205
206+ # Similar Projects
207+
208+ * [curlylint](https://www.curlylint.org/): Focuses on HTML
209+ * [djLint](https://www.djlint.com/docs/linter/): Focuses on HTML
210+
206211# Acknowledgments
207212Big thanks to [Ruud Kamphuis](https://github.com/ruudk) for [TwigStan](https://github.com/twigstan/twigstan),
208213and for helping on this very project.
0 commit comments