September 30th, 2018   // Twigcs, it's the missing Phpcs for Twig!

Lately I've been working a lot on Twigcs, an open source project I initiated while working at Allociné and kept working on ever after. So I felt like it's time to speak about it here.

What is it?

The idea came as a direct inspiration from Phpcs. I was looking for a tool that would do the same for twig templates but never found one. So I created mine.

It is a CLI tool based on Symfony's console component that will lint your twig templates. Various things are checked like spacing, naming conventions or unused macros / variables. Whenever a mistake is detected, it is added to a report that is also compliant with continuous integration systems (using junit format).

The tool comes with a default rule set based on SensioLabs's official guidelines but can easily be overridden by your own rules.

What does it look like?

# twigcs .
./vendor/twig/twig/test/Twig/Tests/Loader/Fixtures/inheritance/array_inheritance_nonexistent_parent.html.twig
l.1 c.36 : ERROR There should be 1 space(s) after ",".
l.1 c.12 : ERROR There should be no space before "[".
./vendor/twig/twig/test/Twig/Tests/Loader/Fixtures/inheritance/array_inheritance_empty_parent.html.twig
l.1 c.15 : ERROR There should be 1 space(s) after ",".
l.1 c.12 : ERROR There should be no space before "[".
./vendor/twig/twig/test/Twig/Tests/Loader/Fixtures/inheritance/array_inheritance_valid_parent.html.twig
l.1 c.31 : ERROR There should be 1 space(s) after ",".
l.1 c.12 : ERROR There should be no space before "[".
./vendor/twig/twig/test/Twig/Tests/Loader/Fixtures/inheritance/array_inheritance_null_parent.html.twig
l.1 c.17 : ERROR There should be 1 space(s) after ",".
l.1 c.12 : ERROR There should be no space before "[".
./source/layout.html.twig
l.17 c.16 : ERROR There should be 1 space(s) before closing a block.
9 violation(s) found

How to get it?

On Github as a phar archive or with composer!

Are there any integrations?

Yes! Cyril, a former co-worker, developed a VSCode extension.