Skip to content

Commit daf2edb

Browse files
✨ Add PunctuationSpacingSniff
1 parent bd6a6e1 commit daf2edb

File tree

6 files changed

+118
-25
lines changed

6 files changed

+118
-25
lines changed

TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use TwigCS\Token\Token;
99

1010
/**
11-
* Ensure there is one space before and after an operator
11+
* Ensure there is one space before and after an operator except for '..'
1212
*/
1313
class OperatorSpacingSniff extends AbstractSpacingSniff
1414
{
@@ -21,35 +21,43 @@ class OperatorSpacingSniff extends AbstractSpacingSniff
2121
protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?int
2222
{
2323
$token = $tokens[$tokenPosition];
24+
if (!$this->isTokenMatching($token, Token::OPERATOR_TYPE)) {
25+
return null;
26+
}
2427

25-
$isMinus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '-');
26-
$isPlus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '+');
27-
28-
if ($isMinus || $isPlus) {
28+
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, ['-', '+'])) {
2929
return $this->isUnary($tokenPosition, $tokens) ? null : 1;
3030
}
3131

32-
return $this->isTokenMatching($token, Token::OPERATOR_TYPE) && '..' !== $token->getValue() ? 1 : null;
32+
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, '..')) {
33+
return 0;
34+
}
35+
36+
return 1;
3337
}
3438

3539
/**
3640
* @param int $tokenPosition
3741
* @param Token[] $tokens
3842
*
39-
* @return bool
43+
* @return int|null
4044
*/
4145
protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
4246
{
4347
$token = $tokens[$tokenPosition];
48+
if (!$this->isTokenMatching($token, Token::OPERATOR_TYPE)) {
49+
return null;
50+
}
4451

45-
$isMinus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '-');
46-
$isPlus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '+');
47-
48-
if ($isMinus || $isPlus) {
52+
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, ['-', '+'])) {
4953
return $this->isUnary($tokenPosition, $tokens) ? 0 : 1;
5054
}
5155

52-
return $this->isTokenMatching($token, Token::OPERATOR_TYPE) && '..' !== $token->getValue() ? 1 : null;
56+
if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, '..')) {
57+
return 0;
58+
}
59+
60+
return 1;
5361
}
5462

5563
/**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCS\Ruleset\Generic;
6+
7+
use TwigCS\Sniff\AbstractSpacingSniff;
8+
use TwigCS\Token\Token;
9+
10+
/**
11+
* Ensure there is no space before and after a punctuation except for ':' and ','
12+
*/
13+
class PunctuationSpacingSniff extends AbstractSpacingSniff
14+
{
15+
/**
16+
* @param int $tokenPosition
17+
* @param Token[] $tokens
18+
*
19+
* @return int|null
20+
*/
21+
protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?int
22+
{
23+
$token = $tokens[$tokenPosition];
24+
if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [')', ']', '}', ':', '.', ',', '|'])) {
25+
return 0;
26+
}
27+
28+
return null;
29+
}
30+
31+
/**
32+
* @param int $tokenPosition
33+
* @param Token[] $tokens
34+
*
35+
* @return int|null
36+
*/
37+
protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
38+
{
39+
$token = $tokens[$tokenPosition];
40+
if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [':', ','])) {
41+
return 1;
42+
}
43+
44+
if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, ['(', '[', '{', '.', '|'])) {
45+
return 0;
46+
}
47+
48+
return null;
49+
}
50+
}

TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.fixed.twig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
Untouch +-/*%==:
1818

19-
{{ [1, 2, 3] }}
20-
{{ {'foo': 'bar'} }}
2119
{{ 1 ?: 2 }}
2220
{{ 1 ?? 2 }}
2321

TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public function testSniff(): void
4747
[14 => 7],
4848
[15 => 7],
4949
[15 => 7],
50-
[21 => 5],
51-
[21 => 5],
52-
[22 => 5],
53-
[22 => 5],
54-
[24 => 6],
55-
[35 => 10],
56-
[35 => 10],
50+
[19 => 5],
51+
[19 => 5],
52+
[20 => 5],
53+
[20 => 5],
54+
[22 => 6],
55+
[33 => 10],
56+
[33 => 10],
5757
]);
5858
}
5959
}

TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.twig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
Untouch +-/*%==:
1818

19-
{{ [1, 2, 3] }}
20-
{{ {'foo': 'bar'} }}
2119
{{ 1?:2 }}
2220
{{ 1??2 }}
2321

docs/twig.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
# Twig Coding Standard Rules
22

3-
In construction...
3+
From the [official one](http://twig.sensiolabs.org/doc/coding_standards.html).
44

5-
See [official one](http://twig.sensiolabs.org/doc/coding_standards.html).
5+
### Delimiter spacing
6+
7+
Put one (and only one) space after the start of a delimiter (`{{`, `{%`, and `{#`)
8+
and before the end of a delimiter (`}}`, `%}`, and `#}`).
9+
10+
When using the whitespace control character, do not put any spaces between it and the delimiter
11+
12+
### Operator spacing
13+
14+
Put one (and only one) space before and after the following operators:
15+
comparison operators (`==`, `!=`, `<`, `>`, `>=`, `<=`), math operators (`+`, `-`, `/`, `*`, `%`, `//`, `**`),
16+
logic operators (`not`, `and`, `or`), `~`, `is`, `in`, and the ternary operator (`?:`)
17+
18+
Do not put any spaces before and after the operator `..`.
19+
20+
### Punctuation spacing
21+
22+
Put one (and only one) space after the `:` sign in hashes and `,` in arrays and hashes
23+
24+
Do not put any spaces after an opening parenthesis and before a closing parenthesis in expressions
25+
26+
Do not put any spaces before and after the following operators: `|`, `.`, `[]`
27+
28+
Do not put any spaces before and after the opening and the closing of arrays and hashes
29+
30+
### Todo
31+
32+
Do not put any spaces before and after string delimiters
33+
34+
### Todo
35+
36+
Do not put any spaces before and after the parenthesis used for filter and function calls
37+
38+
### Todo
39+
40+
Use lower cased and underscored variable names
41+
42+
### Todo
43+
44+
Indent your code inside tags (use the same indentation as the one used for the target language of the rendered template)

0 commit comments

Comments
 (0)