Skip to content

Commit d64ce9e

Browse files
🐛 Handle unary case
1 parent d16f099 commit d64ce9e

13 files changed

+219
-146
lines changed

TwigCS/Tests/Fixtures/DisallowCommentedCodeTest.twig

Lines changed: 0 additions & 7 deletions
This file was deleted.

TwigCS/Tests/Fixtures/OperatorSpacingTest.fixed.twig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ Untouch +-/*%==:
1818

1919
{{ [1, 2, 3] }}
2020
{{ {'foo': 'bar'} }}
21+
{{ 1 ?: 2 }}
22+
{{ 1 ?? 2 }}
23+
24+
{{ 1 + -2 }}
25+
{{ 1 + (-2 + 3) }}
26+
{{ a[-2] }}
27+
{{ { 'foo': -2 } }}
28+
{{ foo.name }}
29+
{{ 1..10 }}
30+
{{ -5..-2 }}
31+
{{ [1, -2] }}

TwigCS/Tests/Fixtures/OperatorSpacingTest.twig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ Untouch +-/*%==:
1818

1919
{{ [1, 2, 3] }}
2020
{{ {'foo': 'bar'} }}
21+
{{ 1?:2 }}
22+
{{ 1??2 }}
23+
24+
{{ 1 + -2 }}
25+
{{ 1 + (-2 + 3) }}
26+
{{ a[-2] }}
27+
{{ { 'foo': -2 } }}
28+
{{ foo.name }}
29+
{{ 1..10 }}
30+
{{ -5..-2 }}
31+
{{ [1, -2] }}

TwigCS/Tests/Ruleset/Generic/DisallowCommentedCodeTest.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

TwigCS/Tests/Ruleset/Generic/OperatorSpacingTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ 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],
5054
]);
5155
}
5256
}

TwigCS/src/Ruleset/Generic/DelimiterSpacingSniff.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,42 @@
1313
class DelimiterSpacingSniff extends AbstractSpacingSniff
1414
{
1515
/**
16-
* @param Token $token
16+
* @param int $tokenPosition
17+
* @param Token[] $tokens
1718
*
18-
* @return bool
19+
* @return int|null
1920
*/
20-
protected function shouldHaveSpaceBefore(Token $token): bool
21+
protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?int
2122
{
22-
return $this->isTokenMatching($token, Token::VAR_END_TYPE)
23+
$token = $tokens[$tokenPosition];
24+
25+
if ($this->isTokenMatching($token, Token::VAR_END_TYPE)
2326
|| $this->isTokenMatching($token, Token::BLOCK_END_TYPE)
24-
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE);
27+
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE)
28+
) {
29+
return 1;
30+
}
31+
32+
return null;
2533
}
2634

2735
/**
28-
* @param Token $token
36+
* @param int $tokenPosition
37+
* @param Token[] $tokens
2938
*
30-
* @return bool
39+
* @return int|null
3140
*/
32-
protected function shouldHaveSpaceAfter(Token $token): bool
41+
protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
3342
{
34-
return $this->isTokenMatching($token, Token::VAR_START_TYPE)
43+
$token = $tokens[$tokenPosition];
44+
45+
if ($this->isTokenMatching($token, Token::VAR_START_TYPE)
3546
|| $this->isTokenMatching($token, Token::BLOCK_START_TYPE)
36-
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE);
47+
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE)
48+
) {
49+
return 1;
50+
}
51+
52+
return null;
3753
}
3854
}

TwigCS/src/Ruleset/Generic/DisallowCommentedCodeSniff.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,74 @@
1313
class OperatorSpacingSniff extends AbstractSpacingSniff
1414
{
1515
/**
16-
* @param Token $token
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+
25+
$isMinus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '-');
26+
$isPlus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '+');
27+
28+
if ($isMinus || $isPlus) {
29+
return $this->isUnary($tokenPosition, $tokens) ? null : 1;
30+
}
31+
32+
return $this->isTokenMatching($token, Token::OPERATOR_TYPE) && '..' !== $token->getValue() ? 1 : null;
33+
}
34+
35+
/**
36+
* @param int $tokenPosition
37+
* @param Token[] $tokens
1738
*
1839
* @return bool
1940
*/
20-
protected function shouldHaveSpaceBefore(Token $token): bool
41+
protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
2142
{
22-
return $this->isTokenMatching($token, Token::OPERATOR_TYPE);
43+
$token = $tokens[$tokenPosition];
44+
45+
$isMinus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '-');
46+
$isPlus = $this->isTokenMatching($token, Token::OPERATOR_TYPE, '+');
47+
48+
if ($isMinus || $isPlus) {
49+
return $this->isUnary($tokenPosition, $tokens) ? 0 : 1;
50+
}
51+
52+
return $this->isTokenMatching($token, Token::OPERATOR_TYPE) && '..' !== $token->getValue() ? 1 : null;
2353
}
2454

2555
/**
26-
* @param Token $token
56+
* @param int $tokenPosition
57+
* @param array $tokens
2758
*
2859
* @return bool
2960
*/
30-
protected function shouldHaveSpaceAfter(Token $token): bool
61+
private function isUnary(int $tokenPosition, array $tokens): bool
3162
{
32-
return $this->isTokenMatching($token, Token::OPERATOR_TYPE);
63+
$previous = $this->findPrevious(Token::EMPTY_TOKENS, $tokens, $tokenPosition - 1, true);
64+
if (false === $previous) {
65+
return true;
66+
}
67+
68+
$previousToken = $tokens[$previous];
69+
if ($this->isTokenMatching($previousToken, Token::OPERATOR_TYPE)) {
70+
// {{ 1 * -2 }}
71+
return true;
72+
}
73+
74+
if ($this->isTokenMatching($previousToken, Token::VAR_START_TYPE)) {
75+
// {{ -2 }}
76+
return true;
77+
}
78+
79+
if ($this->isTokenMatching($previousToken, Token::PUNCTUATION_TYPE, ['(', '[', ':', ','])) {
80+
// {{ 1 + (-2) }}
81+
return true;
82+
}
83+
84+
return false;
3385
}
3486
}

TwigCS/src/Sniff/AbstractSniff.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,67 @@ public function disable(): void
4848
}
4949

5050
/**
51-
* @param Token $token
52-
* @param int $type
53-
* @param string $value
51+
* @param Token $token
52+
* @param int|array $type
53+
* @param string|array $value
5454
*
5555
* @return bool
5656
*/
57-
public function isTokenMatching(Token $token, int $type, string $value = null): bool
57+
public function isTokenMatching(Token $token, $type, $value = []): bool
5858
{
59-
return $token->getType() === $type && (null === $value || $token->getValue() === $value);
59+
if (!is_array($type)) {
60+
$type = [$type];
61+
}
62+
if (!is_array($value)) {
63+
$value = [$value];
64+
}
65+
66+
return in_array($token->getType(), $type) && ([] === $value || in_array($token->getValue(), $value));
6067
}
6168

6269
/**
63-
* @param int $type
64-
* @param array $tokens
65-
* @param int $start
66-
* @param bool $exclude
70+
* @param int|array $type
71+
* @param array $tokens
72+
* @param int $start
73+
* @param bool $exclude
6774
*
68-
* @return int
75+
* @return int|false
6976
*/
70-
public function findNext(int $type, array $tokens, int $start, bool $exclude = false): int
77+
public function findNext($type, array $tokens, int $start, bool $exclude = false)
7178
{
7279
$i = 0;
7380

7481
while (isset($tokens[$start + $i]) && $exclude === $this->isTokenMatching($tokens[$start + $i], $type)) {
7582
$i++;
7683
}
7784

85+
if (!isset($tokens[$start + $i])) {
86+
return false;
87+
}
88+
7889
return $start + $i;
7990
}
8091

8192
/**
82-
* @param int $type
83-
* @param array $tokens
84-
* @param int $start
85-
* @param bool $exclude
93+
* @param int|array $type
94+
* @param array $tokens
95+
* @param int $start
96+
* @param bool $exclude
8697
*
87-
* @return int
98+
* @return int|false
8899
*/
89-
public function findPrevious(int $type, array $tokens, int $start, bool $exclude = false): int
100+
public function findPrevious($type, array $tokens, int $start, bool $exclude = false)
90101
{
91102
$i = 0;
92103

93104
while (isset($tokens[$start - $i]) && $exclude === $this->isTokenMatching($tokens[$start - $i], $type)) {
94105
$i++;
95106
}
96107

108+
if (!isset($tokens[$start - $i])) {
109+
return false;
110+
}
111+
97112
return $start - $i;
98113
}
99114

0 commit comments

Comments
 (0)