Skip to content

Commit db7a7f1

Browse files
authored
Merge pull request #6 from ThomasLandauer/patch-2
README overhaul
2 parents cafec69 + f8a029b commit db7a7f1

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

README.md

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,12 @@ Twig Quality Inspections is an extension to the [Twig templating](https://github
1010
which adds static analysis (i.e., compile-time) inspections and runtime assertions to increase templates' quality.
1111
See 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

1615
The 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
2820
Just 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

4335
Here, `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

4839
However, `user.getRoleLabel(usr.role)` will cause an uncaught `TypeError` if that method's parameter is not nullable,
4940
since 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

7156
namespace App\Twig;
@@ -74,11 +59,38 @@ use AlisQI\TwigQI\Extension;
7459
use 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
8294
Either way, all inspection results will [show up](docs/error-on-page.png) in the Web Debug Toolbar's logs.
8395
8496
![Error shown in Web Debug Toolbar](docs/error-in-wdt.png)
@@ -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\TwigQI\Extension;
106-
use AlisQI\TwigQI\Inspection\InvalidConstant;
107-
use AlisQI\TwigQI\Inspection\InvalidEnumCase;
108-
109-
new Extension($logger, [
110-
InvalidConstant::class,
111-
InvalidEnumCase::class,
112-
]);
113-
```
114114

115115
### Logging
116116
The 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
207212
Big thanks to [Ruud Kamphuis](https://github.com/ruudk) for [TwigStan](https://github.com/twigstan/twigstan),
208213
and for helping on this very project.

0 commit comments

Comments
 (0)