Skip to main content

Twig CS Fixer

https://github.com/VincentLanglet/Twig-CS-Fixer

The missing checkstyle for twig!

Twig CS Fixer aims to be what phpcs is to php. It checks your codebase for violations on coding standards.

Vortex comes with pre-configured Twig-cs-fixer ruleset for Drupal projects.

Usage

vendor/bin/twig-cs-fixer

or

ahoy lint-fe

Configuration

See configuration reference.

All global configuration takes place in the .twig-cs-fixer.php file.

Targets include custom modules and themes.

Adding or removing targets:

$ruleset = new TwigCsFixer\Ruleset\Ruleset();
$ruleset->addStandard(new TwigCsFixer\Standard\Twig());

$finder = new TwigCsFixer\File\Finder();
$finder->in(__DIR__ . '/web/modules/custom');
$finder->in(__DIR__ . '/web/themes/custom');

$config = new TwigCsFixer\Config\Config();
$config->setRuleset($ruleset);
$config->setFinder($finder);

return $config;

Ignoring

Ignoring rules globally takes place in the .twig-cs-fixer.php file:

$finder->exclude('myCustomDirectory');

All errors have an identifier with the syntax: A.B:C:D with

  • A: The rule short name (mainly made from the class name)
  • B: The error identifier (like the error level or a specific name)
  • C: The line the error occurs
  • D: The position of the token in the line the error occurs

The four parts are optional, all those format are working

  • A
  • A.B
  • A.B:C
  • A.B:C:D
  • A:C
  • A:C:D
  • A::D

If you need to know the errors identifier you have/want to ignore, you can run the linter command with the --debug option.

To ignore all Twig CS Fixer rules within a file, place in the file header:

{# twig-cs-fixer-disable #}

To ignore a specific rule within a file, place in the file header:

{# twig-cs-fixer-disable A.B:C:D #}

Twig CS Fixer does not support ignoring of the code blocks.

To ignore only the current line:

{# twig-cs-fixer-disable-next-line A.B:C:D #}

To ignore only the next line:

{# twig-cs-fixer-disable-line A.B:C:D #}

Ignoring fail in CI

This tool runs in CI by default and fails the build if there are any violations.

Set VORTEX_CI_TWIG_CS_FIXER_IGNORE_FAILURE environment variable to 1 to ignore failures. The tool will still run and report violations, if any.