|
2 | 2 |
|
3 | 3 | namespace TwigCS\Ruleset\Generic; |
4 | 4 |
|
5 | | -use \Exception; |
6 | | -use TwigCS\Sniff\AbstractSniff; |
| 5 | +use TwigCS\Sniff\AbstractSpacingSniff; |
7 | 6 | use TwigCS\Token\Token; |
8 | 7 |
|
9 | 8 | /** |
10 | | - * Ensure there is one space before and after a delimiter {{, {%, {#, }}, %} and #} |
| 9 | + * Ensure there is one space before {{, {%, {#, and after }}, %} and #} |
11 | 10 | */ |
12 | | -class DelimiterSpacingSniff extends AbstractSniff |
| 11 | +class DelimiterSpacingSniff extends AbstractSpacingSniff |
13 | 12 | { |
14 | 13 | /** |
15 | | - * @param int $tokenPosition |
16 | | - * @param Token[] $tokens |
| 14 | + * @param Token $token |
17 | 15 | * |
18 | | - * @return Token |
19 | | - * |
20 | | - * @throws Exception |
| 16 | + * @return bool |
21 | 17 | */ |
22 | | - public function process(int $tokenPosition, array $tokens) |
| 18 | + protected function shouldHaveSpaceBefore(Token $token) |
23 | 19 | { |
24 | | - $token = $tokens[$tokenPosition]; |
25 | | - |
26 | | - if ($this->isTokenMatching($token, Token::VAR_START_TYPE) |
27 | | - || $this->isTokenMatching($token, Token::BLOCK_START_TYPE) |
28 | | - || $this->isTokenMatching($token, Token::COMMENT_START_TYPE) |
29 | | - ) { |
30 | | - $this->processStart($tokenPosition, $tokens); |
31 | | - } |
32 | | - |
33 | | - if ($this->isTokenMatching($token, Token::VAR_END_TYPE) |
| 20 | + return $this->isTokenMatching($token, Token::VAR_END_TYPE) |
34 | 21 | || $this->isTokenMatching($token, Token::BLOCK_END_TYPE) |
35 | | - || $this->isTokenMatching($token, Token::COMMENT_END_TYPE) |
36 | | - ) { |
37 | | - $this->processEnd($tokenPosition, $tokens); |
38 | | - } |
39 | | - |
40 | | - return $token; |
41 | | - } |
42 | | - |
43 | | - /** |
44 | | - * @param int $tokenPosition |
45 | | - * @param Token[] $tokens |
46 | | - * |
47 | | - * @throws Exception |
48 | | - */ |
49 | | - public function processStart(int $tokenPosition, array $tokens) |
50 | | - { |
51 | | - $token = $tokens[$tokenPosition]; |
52 | | - |
53 | | - // Ignore new line |
54 | | - $next = $this->findNext(Token::WHITESPACE_TYPE, $tokens, $tokenPosition + 1, true); |
55 | | - if ($this->isTokenMatching($tokens[$next], Token::EOL_TYPE)) { |
56 | | - return; |
57 | | - } |
58 | | - |
59 | | - if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::WHITESPACE_TYPE)) { |
60 | | - $count = strlen($tokens[$tokenPosition + 1]->getValue()); |
61 | | - } else { |
62 | | - $count = 0; |
63 | | - } |
64 | | - |
65 | | - if (1 !== $count) { |
66 | | - $fix = $this->addFixableMessage( |
67 | | - $this::MESSAGE_TYPE_ERROR, |
68 | | - sprintf('Expecting 1 whitespace after "%s"; found %d', $token->getValue(), $count), |
69 | | - $token |
70 | | - ); |
71 | | - |
72 | | - if ($fix) { |
73 | | - if (0 === $count) { |
74 | | - $this->fixer->addContent($tokenPosition, ' '); |
75 | | - } else { |
76 | | - $this->fixer->replaceToken($tokenPosition + 1, ' '); |
77 | | - } |
78 | | - } |
79 | | - } |
| 22 | + || $this->isTokenMatching($token, Token::COMMENT_END_TYPE); |
80 | 23 | } |
81 | 24 |
|
82 | 25 | /** |
83 | | - * @param int $tokenPosition |
84 | | - * @param Token[] $tokens |
| 26 | + * @param Token $token |
85 | 27 | * |
86 | | - * @throws Exception |
| 28 | + * @return bool |
87 | 29 | */ |
88 | | - public function processEnd(int $tokenPosition, array $tokens) |
| 30 | + protected function shouldHaveSpaceAfter(Token $token) |
89 | 31 | { |
90 | | - $token = $tokens[$tokenPosition]; |
91 | | - |
92 | | - // Ignore new line |
93 | | - $previous = $this->findPrevious(Token::WHITESPACE_TYPE, $tokens, $tokenPosition - 1, true); |
94 | | - if ($this->isTokenMatching($tokens[$previous], Token::EOL_TYPE)) { |
95 | | - return; |
96 | | - } |
97 | | - |
98 | | - if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::WHITESPACE_TYPE)) { |
99 | | - $count = strlen($tokens[$tokenPosition - 1]->getValue()); |
100 | | - } else { |
101 | | - $count = 0; |
102 | | - } |
103 | | - |
104 | | - if (1 !== $count) { |
105 | | - $fix = $this->addFixableMessage( |
106 | | - $this::MESSAGE_TYPE_ERROR, |
107 | | - sprintf('Expecting 1 whitespace before "%s"; found %d', $token->getValue(), $count), |
108 | | - $token |
109 | | - ); |
110 | | - |
111 | | - if ($fix) { |
112 | | - if (0 === $count) { |
113 | | - $this->fixer->addContentBefore($tokenPosition, ' '); |
114 | | - } else { |
115 | | - $this->fixer->replaceToken($tokenPosition - 1, ' '); |
116 | | - } |
117 | | - } |
118 | | - } |
| 32 | + return $this->isTokenMatching($token, Token::VAR_START_TYPE) |
| 33 | + || $this->isTokenMatching($token, Token::BLOCK_START_TYPE) |
| 34 | + || $this->isTokenMatching($token, Token::COMMENT_START_TYPE); |
119 | 35 | } |
120 | 36 | } |
0 commit comments