### Configure Twig-CS-Fixer rules Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/rules.md Examples of instantiating configurable rules by passing parameters to their constructors. ```php new TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule( ['}' => 1], ['{' => 1], ); new TwigCsFixer\Rules\Whitespace\IndentRule(3); ``` -------------------------------- ### Install Twig CS Fixer via Composer Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/README.md Add the package as a development dependency to your project. ```bash composer require --dev vincentlanglet/twig-cs-fixer ``` -------------------------------- ### Specify Configuration Path Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Use the --config flag to point to a specific configuration file when running the linter. ```bash vendor/bin/twig-cs-fixer lint --config=dir/.twig-cs-fixer.php /path/to/code ``` -------------------------------- ### Configure File Finder Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Customize which files or directories are included or excluded from the linting process. ```php in('templates'); $finder->exclude('myCustomDirectory'); $config = new TwigCsFixer\Config\Config(); $config->setFinder($finder); return $config; ``` -------------------------------- ### Update Directory path helper Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/UPGRADE.md Replaced Directory instance method with FileHelper static method. ```diff - (new Directory($dir))->getRelativePathTo($file); + FileHelper::getRelativePath($file, $dir); ``` -------------------------------- ### Update AbstractRule methods Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/UPGRADE.md Refactored token matching and navigation methods in AbstractRule. ```diff - $this->isTokenMatching($token, $type, $value) + $token->isMatching($type, $value) ``` ```diff - $this->findNext($type, $tokens, $start) + $tokens->findNext($type, $start) - $this->findNext($type, $tokens, $start, true) + $tokens->findNext($type, $start, null, true) ``` ```diff - $this->findPrevious($type, $tokens, $start) + $tokens->findPrevious($type, $start) - $this->findPrevious($type, $tokens, $start, true) + $tokens->findPrevious($type, $start, 0, true) ``` ```diff - protected function process(int $tokenPosition, array $tokens): void; + protected function process(int $tokenIndex, Tokens $tokens): void; ``` -------------------------------- ### Download Twig CS Fixer PHAR Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/README.md Fetch the stable version of the tool as a Phar archive. ```bash wget -c https://github.com/VincentLanglet/Twig-CS-Fixer/releases/download/VERSION/twig-cs-fixer.phar ``` -------------------------------- ### Disable Cache via Command Line Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Run the linter without using the cache. ```bash vendor/bin/twig-cs-fixer lint --no-cache ``` -------------------------------- ### Include Autoloader in Configuration Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/README.md Add the autoloader to your configuration file to support custom rules or node-based rules. ```php require_once __DIR__.'/vendor/autoload.php'; ``` -------------------------------- ### Add Custom Reporter Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Register a custom reporter class to handle linter output formats. ```php addCustomReporter(new App\Twig\CustomReporter()); return $config; ``` -------------------------------- ### Lint and Fix Twig Templates Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/README.md Run the linter to check for issues or the fixer to automatically resolve them. ```bash vendor/bin/twig-cs-fixer lint /path/to/code vendor/bin/twig-cs-fixer fix /path/to/code ``` -------------------------------- ### Configure Cache Location Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Set a custom path for the cache file or disable caching entirely. ```php setCacheFile('/tmp/.twig-cs-fixer.cache'); return $config; ``` ```php setCacheFile(null); return $config; ``` -------------------------------- ### Configure Ruleset Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Define a custom ruleset by adding, removing, or overriding rules within a Config instance. ```php addStandard(new TwigCsFixer\Standard\TwigCsFixer()); // And then add/remove/override some rules $ruleset->addRule(new TwigCsFixer\Rules\File\FileExtensionRule()); $ruleset->removeRule(TwigCsFixer\Rules\Whitespace\EmptyLinesRule::class); $ruleset->overrideRule(new TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule( ['}' => 1], ['{' => 1], )); $config = new TwigCsFixer\Config\Config(); $config->setRuleset($ruleset); return $config; ``` -------------------------------- ### Update AbstractSpacingRule methods Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/UPGRADE.md Updated spacing rule methods to use Tokens object instead of array. ```diff - protected function getSpaceAfter(int $tokenPosition, array $tokens): ?int; + protected function getSpaceAfter(int $tokenIndex, Tokens $tokens): ?int; ``` ```diff - protected function getSpaceBefore(int $tokenPosition, array $tokens): ?int; + protected function getSpaceBefore(int $tokenIndex, Tokens $tokens): ?int; ``` -------------------------------- ### Register Token Parsers and Extensions Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Add custom Twig extensions, token parsers, or node visitors to the configuration. ```php addTwigExtension(new App\Twig\CustomTwigExtension()); $config->addTokenParser(new App\Twig\CustomTokenParser()); $config->addNodeVisitor(new App\Twig\CustomNodeVisitor()); return $config; ``` -------------------------------- ### Implement a custom rule class Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/custom.md Extend AbstractRule or AbstractFixableRule to define custom token validation and fixing logic. Use addFixableError to initiate a change set for automatic fixes. ```php final class MyCustomRule extends \TwigCsFixer\Rules\AbstractRule { protected function process(int $tokenIndex, \TwigCsFixer\Token\Tokens $tokens): void { $token = $tokens->get($tokenIndex); if (!$token->isMatching(...)) { // Skip if the token is not matching some conditions. return; } $nextToken = $tokens->findNext(...); // Look for next token based on conditions. $previousToken = $tokens->findPrevious(...); // Look for previous token based on conditions. if (...) { // Skip if the token is valid. return; } // Use $this->addError(...) if the error is not fixable. $fixer = $this->addFixableError('Custom message.', $token); if (null === $fixer) { // Fixer is null when you're linting the file. return; } $fixer->beginChangeSet(); // Use some of the following functions base of the wanted behavior: // $fixer->replaceToken(...); // $fixer->addNewline(...); // $fixer->addNewlineBefore(...); // $fixer->addContent(...); // $fixer->addContentBefore(...); $fixer->endChangeSet(); } } ``` -------------------------------- ### Update Token position retrieval Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/UPGRADE.md Replaced getPosition with getLinePosition. ```diff - $token->getPosition(); + $token->getLinePosition(); ``` -------------------------------- ### Update DelimiterSpacingRule constructor Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/UPGRADE.md The constructor signature for DelimiterSpacingRule has been updated to include override parameters. ```php __construct(bool $skipIfNewLine = true) ``` ```php __construct(array $beforeOverride = [], array $afterOverride = [], bool $skipIfNewLine = true) ``` -------------------------------- ### Implement a custom node-based rule Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/custom.md Extend AbstractNodeRule to define custom logic for Twig nodes. Note that these rules are not fixable and only support line-level error reporting. ```php final class MyCustomRule extends \TwigCsFixer\Rules\Node\AbstractNodeRule { public function enterNode(\Twig\Node\Node $node, \Twig\Environment $env): Node { // Do some logic } } ``` -------------------------------- ### Update TokenizerInterface Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/UPGRADE.md The tokenize method now returns a Tokens object. ```diff - /** - * @return array{list, list} - */ - public function tokenize(Source $source): array; + public function tokenize(Source $source): Tokens; ``` -------------------------------- ### Allow Non-Fixable Rules Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/configuration.md Enable rules that are not automatically fixable by the linter. ```php allowNonFixableRules(); return $config; ``` -------------------------------- ### Update RuleInterface and FixableRuleInterface Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/UPGRADE.md Updated linting and fixing interfaces to use Tokens object. ```diff - public function lintFile(array $tokens, Report $report, array $ignoredViolations = []): void; + public function lintFile(Tokens $tokens, Report $report): void; ``` ```diff - public function fixFile(array $tokens, FixerInterface $fixer, array $ignoredViolations = []): void; + public function fixFile(Tokens $tokens, FixerInterface $fixer): void; ``` -------------------------------- ### Disable specific errors using comments Source: https://github.com/vincentlanglet/twig-cs-fixer/blob/main/docs/ignoring-errors.md Syntax for disabling rules or specific error identifiers using file-level, line-level, or next-line comments. ```twig {# twig-cs-fixer-disable A.B:C:D #} => Apply to the whole file {# twig-cs-fixer-disable-line A.B:C:D #} => Apply to the line of the comment {# twig-cs-fixer-disable-next-line A.B:C:D #} => Apply to the next line of the comment ``` ```twig {# twig-cs-fixer-disable #} => Disable every rule for the whole file {# twig-cs-fixer-disable-line #} => Disable every rule for the current line {# twig-cs-fixer-disable-next-line #} => Disable every rule for the next line {# twig-cs-fixer-disable A #} => Disable the rule A for the whole file {# twig-cs-fixer-disable A:42 #} => Disable the rule A for the line 42 of the file {# twig-cs-fixer-disable-line A.B #} => Disable the error B of the rule A for the current line {# twig-cs-fixer-disable-next-line A::42 #} => Disable the rule A for the next line but only for the token 42 ``` ```twig {# twig-cs-fixer-disable A B C #} => Disable A and B and C for the whole file {# twig-cs-fixer-disable-line A.B,C.D #} => Disable A.B and C.D for the current line ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.