A tool to automatically fix Twig Coding Standards issues

Overview

Twig CS Fixer

PHP Latest Stable Version License Actions Status Coverage Type Coverage Infection MSI

Installation

This standard can be installed with the Composer dependency manager.

Add the coding standard as a dependency of your project

composer require --dev vincentlanglet/twig-cs-fixer

Then, use it!

bin/twig-cs-fixer lint /path/to/code
bin/twig-cs-fixer lint --fix /path/to/code

Twig Coding Standard Rules

From the official one.

Delimiter spacing

Put one (and only one) space after the start of a delimiter ({{, {%, and {#) and before the end of a delimiter (}}, %}, and #}).

When using the whitespace control character, do not put any spaces between it and the delimiter.

Operator spacing

Put one (and only one) space before and after the following operators: comparison operators (==, !=, <, >, >=, <=), math operators (+, -, /, *, %, //, **), logic operators (not, and, or), ~, is, in, and the ternary operator (?:).

Do not put any spaces before and after the operator ...

Punctuation spacing

Put one (and only one) space after the : sign in hashes and , in arrays and hashes.

Do not put any spaces after an opening parenthesis and before a closing parenthesis in expressions.

Do not put any spaces before and after the following operators: |, ., [].

Do not put any spaces before and after the parenthesis used for filter and function calls.

Do not put any spaces before and after the opening and the closing of arrays and hashes.

Custom configuration

By default, the generic standard is enabled with the twig coding standard rules and the following sniffs:

  • BlankEOFSniff: Ensure that files ends with one blank line.
  • EmptyLinesSniff: Checks that there are not 2 empty lines following each other.
  • TrailingCommaSingleLineSniff: Ensure that single-line arrays, objects and arguments list does not have a trailing comma.
  • TrailingSpaceSniff: Ensure that files has no trailing spaces.

If you want to use a custom standard and/or add/disable a sniff, you can provide your own configuration with a .twig-cs-fixer.php file which returns a TwigCsFixer\Config\Config class. For instance,



$ruleset = new TwigCsFixer\Ruleset\Ruleset();
$ruleset->addStandard(new TwigCsFixer\Standard\Generic());
$ruleset->removeSniff(TwigCsFixer\Sniff\EmptyLinesSniff::class);

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

return $config;

If your config is not located in your current directory, you can pass his path when running the command.

bin/twig-cs-fixer lint --config=dir/.twig-cs-fixer.php /path/to/code
Comments
  • [Feature] Ignore a line/rule

    [Feature] Ignore a line/rule

    Can you add documentation to the README that explains how one can ignore a line(s)s of code or disable a ruleset all together during a check? I'm happy to contribute that if you can point me to some examples or even provide them here.

    Expected behavior

    Ability ignore certain lines/rules.

    Actual behavior

    I'm unable to do so, or haven't found the documentation.

    opened by adamzimmermann 7
  • Comparison to friendsoftwig/twigcs

    Comparison to friendsoftwig/twigcs

    Can you help me understand how this relates to https://github.com/friendsoftwig/twigcs with functionality and when I would want to use one vs the other?

    The one big thing I've noticed so far is that this project seems to offer automated code style fixes while https://github.com/friendsoftwig/twigcs does not.

    This project also seems to enforce the Symfony standard while https://github.com/friendsoftwig/twigcs enforces the "Official one from Twig".

    At the moment the only available standard is the official one from twig.

    • https://github.com/friendsoftwig/twigcs#custom-coding-standard

    I need to test them both out more this week, but wanted to file an issue to hopefully get a discussion started and document my finding too when I know more.

    Any help or guidance is much appreciated!

    opened by adamzimmermann 7
  • Extendings Tags:  File is invalid: Unknown

    Extendings Tags: File is invalid: Unknown "cache" tag.

    Hey there, is it possible to add tags? or exclude single files?

    I try to fix this line:

    {% cache 'cache_key' ttl(300) %}
        say hello cache
    {% endcache %}
    

    And iĀ“m getting: File is invalid: Unknown "cache" tag.

    opened by timvonboard 5
  • Add --exclude Flag and use Symfony Finder

    Add --exclude Flag and use Symfony Finder

    Description

    This is not working currently, but I wanted to get it up here to see if I'm at least roughly in line with the approach you were thinking.

    This PR is attempting to partially revert these changes:

    • https://github.com/VincentLanglet/symfony-custom-coding-standard/pull/114/files

    One thing I'm not 100% clear on is why we want to keep TwigCsFixer\File\Finder.php or add configurability around that. Why would we not just always use the Symfony Finder? Or perhaps I'm misunderstanding your intent?

    Motivation

    • #32

    Remaining Tasks

    • [x] Decide if we need to support both Finders
    • [x] Get it to actually work.
    • [ ] Update tests if applicable?
    • [ ] Update the README with documentation about the new option.
    opened by adamzimmermann 5
  • Exclude the node_modules directory by default

    Exclude the node_modules directory by default

    Rule(s) related to or rule(s) missing

    N/A

    Expected behavior

    I'm finally getting around to fully integrating the exclude/custom finder functionality. Thanks again for your help with that.

    • https://github.com/VincentLanglet/Twig-CS-Fixer/pull/35#issuecomment-911929586

    I created a custom .twig-cs-fixer.php per the instructions in the README and everything is working. However I realized that my change might be what we want by default.

    I simply added ->exclude('node_modules'); and I'm wondering if that should be the default.

    • https://github.com/ChromaticHQ/chq-robo/pull/36

    If so, I'm happy to submit a PR for that.

    Actual behavior

    Exclude node_modules from linting by default.

    opened by adamzimmermann 4
  • Question on some twig cs fix

    Question on some twig cs fix

    Hi, first thanks for this lib, I discovered it recently and began to use it on some of my pro/perso projects :)

    Rule(s) related to or rule(s) missing

    I have a question regarding a fix on spaces for some twig: see after

    Expected behavior

    it removes extra spaces

    {% extends '@Admin/base.html.twig' %}
    {% block main_content %}
    

    Actual behavior

    it does not remove extra spaces

    {%    extends    '@Admin/...'     %}
    {%    block main_content       %}
    

    I was expecting the same twig cs fix as for example:

    {{ my_var}} => {{ my_var }}


    Is it a normal behavior or should I configure something? I am using the v0.3 with the default config (and twig/twig v2)

    Thank you!

    opened by noniagriconomie 4
  • Use vendor/bin/twig-cs-fixer in README

    Use vendor/bin/twig-cs-fixer in README

    I replaced bin/twig-cs-fixer with vendor/bin/twig-cs-fixer as that is today more common. I also added empty spaces around code blocks as that is required for some markdown viewers sadly not remember which one it was where that problem popped up.

    PS: really great project šŸ‘

    opened by alexander-schranz 3
  • fail to lint string slice notation

    fail to lint string slice notation

    Let's say we have sth like this in the code: {% set var = 'test'[0:1] ~ 'test'[:3] %}

    After firing the lint --fix we have: {% set var = 'test'[0: 1] ~ 'test'[: 3] %}

    image

    opened by sirchris 3
  • [Request] Automatically format or beautify HTML too?

    [Request] Automatically format or beautify HTML too?

    Hello and thanks for this very useful tool! Is there a way to format the HTML along with the twig tags or I should use another tool for this? Which one would you recommend?

    Thanks!

    opened by lukepass 3
  • adds a caching system based on PHP-CS-Fixer's

    adds a caching system based on PHP-CS-Fixer's

    Adds a caching system heavily based on PHP-CS-Fixer's

    Create a file named .twig-cs-fixer.php at the root of your project and add the following content:

    <?php
    
    use TwigCsFixer\Config\Config;
    
    $config = new Config();
    $config->setCacheFile('/tmp/cache-file');
    
    return $config;
    
    opened by JoolsMcFly 2
  • version 0.5.1 : The

    version 0.5.1 : The "lint" directory does not exist

    i update all my tools as part of my ci.

    And It used to work well (version 0.5), But now it errors with latest version (0.5.1) :

    twig-cs-fixer lint --fix /xxx/views
    Error: The "lint" directory does not exist.
    

    i reverted version, using this, and it worked again :

    composer global require vincentlanglet/twig-cs-fixer:0.5
    

    has the parameters changed in the new version ? the unit tests passed ?

    opened by rafipiccolo 2
  • Space in single line objects

    Space in single line objects

    Rule(s) related to or rule(s) missing

    I'm searching for a way which I can force a space around object in single lines.

    Expected behavior

    +{{ include('app/atoms/icon.html.twig', { icon: 'time' }, with_context = false) }
    -{{ include('app/atoms/icon.html.twig', {icon: 'time'}, with_context = false) }}
    

    Actual behavior

    -{{ include('app/atoms/icon.html.twig', { icon: 'time' }, with_context = false) }
    +{{ include('app/atoms/icon.html.twig', {icon: 'time'}, with_context = false) }}
    
    opened by alexander-schranz 11
Releases(0.8.0)
Owner
Vincent Langlet
Vincent Langlet
A tool to automatically fix PHP Coding Standards issues

PHP Coding Standards Fixer The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP codi

null 11.6k Jan 1, 2023
Plugin to diagnose/fix ClassicPress SSL issues.

ClassicPress SSL Fix This plugin provides a way to work around the issue "cURL error 60: SSL certificate problem: certificate has expired" in ClassicP

ClassicPress Research 2 Oct 10, 2021
Composer installer for PHP_CodeSniffer coding standards

PHP_CodeSniffer Standards Composer Installer Plugin This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards (r

Dealerdirect 462 Dec 22, 2022
This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards

PHP_CodeSniffer Standards Composer Installer Plugin This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards (r

PHPCSStandards 393 Feb 25, 2022
PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

Squiz Labs 9.9k Jan 5, 2023
Coding-standard - Magento PHP CodeSniffer Coding Standard

ECG Magento Code Sniffer Coding Standard ECG Magento Code Sniffer Coding Standard is a set of rules and sniffs for PHP_CodeSniffer tool. It allows aut

Magento ECG 309 Jan 3, 2023
Documentation on clean coding and demonstration of studied clean coding principals with PHP.

practice-php-clean-code Documentation on clean coding and demonstration of studied clean coding principals with PHP. The document contained in this re

Ferdous Islam 1 Feb 21, 2022
Standards Alignment Tool

Standards Alignment Tool (SALT) [] Overview This is a prototype for testing the IMS Global Learning ConsortiumĀ® CASEā„¢ Specification and proving its us

OpenSALT 39 Dec 14, 2022
The tool converts different error reporting standards for deep compatibility with popular CI systems (TeamCity, IntelliJ IDEA, GitHub Actions, etc).

JBZoo / CI-Report-Converter Why? Installing Using as GitHub Action Example GitHub Action workflow Available Directions Help description in terminal Co

JBZoo Toolbox 17 Jun 16, 2022
Debug with Ray to fix problems faster

Debug with Ray to fix problems faster This package can be installed in any PHP application to send messages to the Ray app. The desktop app: can be us

Spatie 458 Dec 27, 2022
Container Open Inventory ID send bug fix PocketMine-MP plugin

ContainerOpenFixed Container Open Inventory ID send bug fix PocketMine-MP plugin Example use skh6075\containeropenfixed\IWindowType; use pocketmine\ne

avas 2 May 20, 2022
PHPCheckstyle is an open-source tool that helps PHP programmers adhere to certain coding conventions.

PHPCheckstyle Overview PHPCheckstyle is an open-source tool that helps PHP programmers adhere to certain coding conventions. The tools checks the inpu

PHPCheckstyle 157 Dec 5, 2022
Issue tracking application extending GitHub's issues and pull requests for the Joomla! project.

Requirements The issue tracker application requires a server running: PHP 7.2 or 7.3 PHP's ext/curl and ext/intl should also be installed MySQL 5.5.3

Joomla! 68 Oct 27, 2022
A Kimai 2 plugin, which send duration of cards to GitLab spend issues of timesheet.

GitLabBundle A Kimai 2 plugin, which send duration of cards to GitLab spend issues of timesheet. Installation First clone it to your Kimai installatio

LibreCode coop 9 Nov 14, 2022
Analyzer of PHP code to search issues with deprecated functionality in newer interpreter versions.

PhpDeprecationDetector PhpDeprecationDetector - analyzer of PHP code to search usages of deprecated functionality in newer interpreter versions - depr

Sergey 312 Dec 26, 2022
List of Magento extensions with known security issues.

Magento Vulnerability Database List of Magento 1 and 2 integrations with known security issues. Objective: easily identify insecure 3rd party software

Sansec 184 Dec 7, 2022
Fixes WordPress 5.9 global CSS styles specificity issues

Fixes WordPress 5.9 global CSS styles specificity issues

Oliver Juhas 3 Feb 22, 2022
Analyzer of PHP code to search issues with deprecated functionality in newer interpreter versions.

PhpDeprecationDetector PhpDeprecationDetector - analyzer of PHP code to search usages of deprecated functionality in newer interpreter versions - depr

Sergey 312 Dec 26, 2022
A collection of standards as PHP Enums: ISO3166, ISO4217, ISO639...

Standards A collection of standards as PHP Enums Setup Make sure you are running PHP 8.1 or higher to use this package To start right away, run the fo

null 295 Dec 20, 2022