-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
I'd love to use static code analysis and/or linting for our logic-heavy templates to prevent silly mistakes from breaking production. (Asking for a friend 😉)
Compiling templates catches syntax errors, at least. But there are other types of bugs which aren't detected this way.
For example, suppose that a macro uses an undeclared variable (i.e., not a macro argument or declared using {% set %}). This is almost certainly a mistake. strict_variables will throw an Exception, of course, but only if the code is executed. Without strict_variables, Twig will use null, but that can easily lead to an Exception when passing it as a non-nullable argument.
I searched for separate linters, but only found tools that either focus on HTML (curlylint and djLint) or are deprecated (phpstan-twig-rules and twig-lint).
I see two ways of increasing code quality:
- Separate linter CLI, either included in the main
twig/twigpackage or separately - An extension which adds a token parser (or node visitor?) and throws exceptions at compile-time