Skip to content

Commit 3ef51cc

Browse files
committed
minor symfony#60761 Introduce twig-cs-fixer (94noni)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- Introduce `twig-cs-fixer` | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no (improve Twig file coding standard consistency) | New feature? | no | Deprecations? | no | Issues | Fix symfony#54799 | License | MIT --- ## This PR adds config file for the tool `twig-cs-fixer` ([via](https://github.com/VincentLanglet/Twig-CS-Fixer)) As per the original issue, this tool is, in my opinion, relevant here to standardize symfony source Twig file, It follows Twig recommandation, and it's since the issue creation mentioned in the official Twig doc. How to use: - install `twig-cs-fixer` via https://github.com/VincentLanglet/Twig-CS-Fixer - run `{twig-cs-fixer} --config=.twig-cs-fixer.dist.php lint --fix` split in 2 commits; first being the real code, second being the tool run on codebase ## TODOs: - decide if its wanted/needed or not - act default rule set for this PR - define CI --- Friendly ping `@VincentLanglet` `@smnandre` as issue commenters Commits ------- fa3cc0e Introduce `twig-cs-fixer`
2 parents 2dcb076 + fa3cc0e commit 3ef51cc

File tree

62 files changed

+283
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+283
-215
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Twig CS Fixer
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.twig'
7+
pull_request:
8+
paths:
9+
- '**.twig'
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13+
cancel-in-progress: true
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
twig-cs-fixer:
20+
name: Twig CS Fixer
21+
runs-on: ubuntu-24.04
22+
23+
env:
24+
php-version: '8.4'
25+
steps:
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ env.php-version }}
30+
ini-values: "memory_limit=-1"
31+
coverage: none
32+
33+
- name: Checkout target branch
34+
uses: actions/checkout@v4
35+
36+
- name: Install dependencies
37+
run: |
38+
COMPOSER_HOME="$(composer config home)"
39+
([ -d "$COMPOSER_HOME" ] || mkdir "$COMPOSER_HOME") && cp .github/composer-config.json "$COMPOSER_HOME/config.json"
40+
export COMPOSER_ROOT_VERSION=$(grep ' VERSION = ' src/Symfony/Component/HttpKernel/Kernel.php | grep -P -o '[0-9]+\.[0-9]+').x-dev
41+
composer require --dev vincentlanglet/twig-cs-fixer:^3.9.0
42+
43+
- name: Run Twig CS Fixer Lint
44+
run: vendor/bin/twig-cs-fixer lint --config=.twig-cs-fixer.dist.php --report=github

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ composer.lock
33
phpunit.xml
44
.php-cs-fixer.cache
55
.php-cs-fixer.php
6+
.twig-cs-fixer.cache
7+
.twig-cs-fixer.php
68
.phpunit.cache
79
.phpunit.result.cache
810
composer.phar

.twig-cs-fixer.dist.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
$ruleset = new TwigCsFixer\Ruleset\Ruleset();
13+
$ruleset->addStandard(new TwigCsFixer\Standard\TwigCsFixer());
14+
15+
$finder = new TwigCsFixer\File\Finder();
16+
$finder->in('src/Symfony/Bridge/');
17+
$finder->in('src/Symfony/Bundle/');
18+
$finder->in('src/Symfony/Component/');
19+
$finder->exclude('Fixtures');
20+
21+
$config = new TwigCsFixer\Config\Config();
22+
$config->setCacheFile('.twig-cs-fixer.cache');
23+
$config->setRuleset($ruleset);
24+
$config->setFinder($finder);
25+
26+
return $config;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{% extends "@email/zurb_2/notification/body.html.twig" %}
1+
{% extends '@email/zurb_2/notification/body.html.twig' %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{% extends "@email/zurb_2/notification/body.txt.twig" %}
1+
{% extends '@email/zurb_2/notification/body.txt.twig' %}

src/Symfony/Bridge/Twig/Resources/views/Email/zurb_2/notification/body.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<head>
44
<style>
55
{% block style %}
6-
{{ source("@email/zurb_2/main.css") }}
7-
{{ source("@email/zurb_2/notification/local.css") }}
6+
{{ source('@email/zurb_2/main.css') }}
7+
{{ source('@email/zurb_2/notification/local.css') }}
88
{% endblock %}
99
</style>
1010
</head>
1111
<body>
1212
<spacer size="32"></spacer>
1313
<wrapper class="body">
14-
<container class="body_{{ ("urgent" == importance ? "alert" : ("high" == importance ? "warning" : "default")) }}">
14+
<container class="body_{{ ('urgent' == importance ? 'alert' : ('high' == importance ? 'warning' : 'default')) }}">
1515
<spacer size="16"></spacer>
1616
<row>
1717
<columns large="12" small="12">

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_horizontal_layout.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% use "bootstrap_3_layout.html.twig" %}
1+
{% use 'bootstrap_3_layout.html.twig' %}
22

33
{% block form_start -%}
44
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-horizontal')|trim}) %}
@@ -25,7 +25,7 @@ col-sm-2
2525
{% block form_row -%}
2626
{%- set widget_attr = {} -%}
2727
{%- if help -%}
28-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
28+
{%- set widget_attr = {attr: {'aria-describedby': id ~ '_help'}} -%}
2929
{%- endif -%}
3030
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group' ~ ((not compound or force_error|default(false)) and not valid ? ' has-error'))|trim})} %}{{ block('attributes') }}{% endwith %}>
3131
{{- form_label(form) -}}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% use "bootstrap_base_layout.html.twig" %}
1+
{% use 'bootstrap_base_layout.html.twig' %}
22

33
{# Widgets #}
44

@@ -35,21 +35,21 @@
3535
{% block checkbox_widget -%}
3636
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
3737
{% if 'checkbox-inline' in parent_label_class %}
38-
{{- form_label(form, null, { widget: parent() }) -}}
38+
{{- form_label(form, null, {widget: parent()}) -}}
3939
{% else -%}
4040
<div class="checkbox">
41-
{{- form_label(form, null, { widget: parent() }) -}}
41+
{{- form_label(form, null, {widget: parent()}) -}}
4242
</div>
4343
{%- endif -%}
4444
{%- endblock checkbox_widget %}
4545

4646
{% block radio_widget -%}
4747
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
4848
{%- if 'radio-inline' in parent_label_class -%}
49-
{{- form_label(form, null, { widget: parent() }) -}}
49+
{{- form_label(form, null, {widget: parent()}) -}}
5050
{%- else -%}
5151
<div class="radio">
52-
{{- form_label(form, null, { widget: parent() }) -}}
52+
{{- form_label(form, null, {widget: parent()}) -}}
5353
</div>
5454
{%- endif -%}
5555
{%- endblock radio_widget %}
@@ -73,13 +73,13 @@
7373
{% endblock %}
7474

7575
{% block checkbox_label -%}
76-
{%- set label_attr = label_attr|merge({'for': id}) -%}
76+
{%- set label_attr = label_attr|merge({for: id}) -%}
7777

7878
{{- block('checkbox_radio_label') -}}
7979
{%- endblock checkbox_label %}
8080

8181
{% block radio_label -%}
82-
{%- set label_attr = label_attr|merge({'for': id}) -%}
82+
{%- set label_attr = label_attr|merge({for: id}) -%}
8383

8484
{{- block('checkbox_radio_label') -}}
8585
{%- endblock radio_label %}
@@ -104,7 +104,7 @@
104104
{% set label = name|humanize %}
105105
{%- endif -%}
106106
{%- endif -%}
107-
<label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
107+
<label{% with {attr: label_attr} %}{{ block('attributes') }}{% endwith %}>
108108
{#- if statement must be kept on the same line, to force the space between widget and label -#}
109109
{{- widget|raw }} {% if label is not same as(false) -%}
110110
{%- if translation_domain is same as(false) -%}
@@ -130,7 +130,7 @@
130130
{% block form_row -%}
131131
{%- set widget_attr = {} -%}
132132
{%- if help -%}
133-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
133+
{%- set widget_attr = {attr: {'aria-describedby': id ~ '_help'}} -%}
134134
{%- endif -%}
135135
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group' ~ ((not compound or force_error|default(false)) and not valid ? ' has-error'))|trim})} %}{{ block('attributes') }}{% endwith %}>
136136
{{- form_label(form) }} {# -#}
@@ -201,7 +201,7 @@
201201
{% block form_help -%}
202202
{%- if help -%}
203203
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-block')|trim}) -%}
204-
<span id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
204+
<span id="{{ id }}_help"{% with {attr: help_attr} %}{{ block('attributes') }}{% endwith %}>
205205
{%- if translation_domain is same as(false) -%}
206206
{%- if help_html is same as(false) -%}
207207
{{- help -}}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_horizontal_layout.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% use "bootstrap_4_layout.html.twig" %}
1+
{% use 'bootstrap_4_layout.html.twig' %}
22

33
{# Labels #}
44

@@ -26,7 +26,7 @@ col-sm-2
2626
{%- else -%}
2727
{%- set widget_attr = {} -%}
2828
{%- if help -%}
29-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
29+
{%- set widget_attr = {attr: {'aria-describedby': id ~ '_help'}} -%}
3030
{%- endif -%}
3131
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group row' ~ ((not compound or force_error|default(false)) and not valid ? ' is-invalid'))|trim})} %}{{ block('attributes') }}{% endwith %}>
3232
{{- form_label(form) -}}
@@ -41,7 +41,7 @@ col-sm-2
4141
{% block fieldset_form_row -%}
4242
{%- set widget_attr = {} -%}
4343
{%- if help -%}
44-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
44+
{%- set widget_attr = {attr: {'aria-describedby': id ~ '_help'}} -%}
4545
{%- endif -%}
4646
<fieldset{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
4747
<div class="row{% if (not compound or force_error|default(false)) and not valid %} is-invalid{% endif %}">

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% use "bootstrap_base_layout.html.twig" %}
1+
{% use 'bootstrap_base_layout.html.twig' %}
22

33
{# Widgets #}
44

@@ -123,10 +123,10 @@
123123
{%- set type = type|default('file') -%}
124124
{%- set input_lang = 'en' -%}
125125
{% if app is defined and app.request is defined %}{%- set input_lang = app.request.locale -%}{%- endif -%}
126-
{%- set attr = {lang: input_lang} | merge(attr) -%}
126+
{%- set attr = {lang: input_lang}|merge(attr) -%}
127127
{{- block('form_widget_simple') -}}
128-
{%- set label_attr = label_attr|merge({ class: (label_attr.class|default('') ~ ' custom-file-label')|trim })|filter((value, key) => key != 'id') -%}
129-
<label for="{{ form.vars.id }}" {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
128+
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' custom-file-label')|trim})|filter((value, key) => key != 'id') -%}
129+
<label for="{{ form.vars.id }}" {% with {attr: label_attr} %}{{ block('attributes') }}{% endwith %}>
130130
{%- if attr.placeholder is defined and attr.placeholder is not none -%}
131131
{{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}}
132132
{%- endif -%}
@@ -173,17 +173,17 @@
173173
{%- if 'checkbox-custom' in parent_label_class -%}
174174
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) -%}
175175
<div class="custom-control custom-checkbox{{ 'checkbox-inline' in parent_label_class ? ' custom-control-inline' }}">
176-
{{- form_label(form, null, { widget: parent() }) -}}
176+
{{- form_label(form, null, {widget: parent()}) -}}
177177
</div>
178178
{%- elseif 'switch-custom' in parent_label_class -%}
179179
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) -%}
180180
<div class="custom-control custom-switch{{ 'switch-inline' in parent_label_class ? ' custom-control-inline' }}">
181-
{{- form_label(form, null, { widget: parent() }) -}}
181+
{{- form_label(form, null, {widget: parent()}) -}}
182182
</div>
183183
{%- else -%}
184184
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) -%}
185185
<div class="form-check{{ 'checkbox-inline' in parent_label_class ? ' form-check-inline' }}">
186-
{{- form_label(form, null, { widget: parent() }) -}}
186+
{{- form_label(form, null, {widget: parent()}) -}}
187187
</div>
188188
{%- endif -%}
189189
{%- endblock checkbox_widget %}
@@ -193,12 +193,12 @@
193193
{%- if 'radio-custom' in parent_label_class -%}
194194
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) -%}
195195
<div class="custom-control custom-radio{{ 'radio-inline' in parent_label_class ? ' custom-control-inline' }}">
196-
{{- form_label(form, null, { widget: parent() }) -}}
196+
{{- form_label(form, null, {widget: parent()}) -}}
197197
</div>
198198
{%- else -%}
199199
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) -%}
200200
<div class="form-check{{ 'radio-inline' in parent_label_class ? ' form-check-inline' }}">
201-
{{- form_label(form, null, { widget: parent() }) -}}
201+
{{- form_label(form, null, {widget: parent()}) -}}
202202
</div>
203203
{%- endif -%}
204204
{%- endblock radio_widget %}
@@ -233,7 +233,7 @@
233233
{% if required -%}
234234
{% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
235235
{%- endif -%}
236-
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
236+
<{{ element|default('label') }}{% if label_attr %}{% with {attr: label_attr} %}{{ block('attributes') }}{% endwith %}{% endif %}>
237237
{{- block('form_label_content') -}}
238238
{% block form_label_errors %}{{- form_errors(form) -}}{% endblock form_label_errors %}</{{ element|default('label') }}>
239239
{%- else -%}
@@ -256,7 +256,7 @@
256256
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' form-check-label')|trim}) -%}
257257
{%- endif %}
258258
{%- if not compound -%}
259-
{% set label_attr = label_attr|merge({'for': id}) %}
259+
{% set label_attr = label_attr|merge({for: id}) %}
260260
{%- endif -%}
261261
{%- if required -%}
262262
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) -%}
@@ -267,7 +267,7 @@
267267
{% endif %}
268268

269269
{{ widget|raw }}
270-
<label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
270+
<label{% with {attr: label_attr} %}{{ block('attributes') }}{% endwith %}>
271271
{%- if label is not same as(false) -%}
272272
{{- block('form_label_content') -}}
273273
{%- endif -%}
@@ -284,7 +284,7 @@
284284
{%- endif -%}
285285
{%- set widget_attr = {} -%}
286286
{%- if help -%}
287-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
287+
{%- set widget_attr = {attr: {'aria-describedby': id ~ '_help'}} -%}
288288
{%- endif -%}
289289
<{{ element|default('div') }}{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
290290
{{- form_label(form) -}}
@@ -312,7 +312,7 @@
312312
{% block form_help -%}
313313
{%- if help -%}
314314
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text text-muted')|trim}) -%}
315-
<small id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
315+
<small id="{{ id }}_help"{% with {attr: help_attr} %}{{ block('attributes') }}{% endwith %}>
316316
{{- block('form_help_content') -}}
317317
</small>
318318
{%- endif -%}

0 commit comments

Comments
 (0)