Skip to content

Commit bd6a6e1

Browse files
Merge pull request #110 from VincentLanglet/bugTwig
Bug
2 parents 2178312 + 77d4673 commit bd6a6e1

32 files changed

+786
-251
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyCustom\Helpers;
6+
7+
/**
8+
* Class AbstractHelper
9+
*/
10+
abstract class AbstractHelper
11+
{
12+
private function __construct()
13+
{
14+
}
15+
}

SymfonyCustom/Helpers/FixerHelper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
use PHP_CodeSniffer\Files\File;
88

99
/**
10-
* class FixerHelper
11-
*
12-
* @internal
10+
* Class FixerHelper
1311
*/
14-
class FixerHelper
12+
class FixerHelper extends AbstractHelper
1513
{
1614
/**
1715
* @param File $phpcsFile

SymfonyCustom/Helpers/SniffHelper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
use PHP_CodeSniffer\Util\Tokens;
99

1010
/**
11-
* class SniffHelper
12-
*
13-
* @internal
11+
* Class SniffHelper
1412
*/
15-
class SniffHelper
13+
class SniffHelper extends AbstractHelper
1614
{
1715
public const TAGS = [
1816
'@api',

TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ private function isUnary(int $tokenPosition, array $tokens): bool
8181
return true;
8282
}
8383

84+
if ($this->isTokenMatching($previousToken, Token::BLOCK_TAG_TYPE)) {
85+
// {% if -2 ... %}
86+
return true;
87+
}
88+
8489
return false;
8590
}
8691
}

TwigCS/src/Runner/Fixer.php

Lines changed: 36 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -172,90 +172,12 @@ public function fixFile(string $file): bool
172172
return true;
173173
}
174174

175-
/**
176-
* @param string $filePath File path to diff the file against.
177-
*
178-
* @return string
179-
*/
180-
public function generateDiff(string $filePath): string
181-
{
182-
$cwd = getcwd().DIRECTORY_SEPARATOR;
183-
if (strpos($filePath, $cwd) === 0) {
184-
$filename = substr($filePath, strlen($cwd));
185-
} else {
186-
$filename = $filePath;
187-
}
188-
189-
$contents = $this->getContents();
190-
191-
$tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer');
192-
$fixedFile = fopen($tempName, 'w');
193-
fwrite($fixedFile, $contents);
194-
195-
// We must use something like shell_exec() because whitespace at the end
196-
// of lines is critical to diff files.
197-
$filename = escapeshellarg($filename);
198-
$cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\"";
199-
200-
$diff = shell_exec($cmd);
201-
202-
fclose($fixedFile);
203-
if (is_file($tempName) === true) {
204-
unlink($tempName);
205-
}
206-
207-
$diffLines = null !== $diff ? explode(PHP_EOL, $diff) : [];
208-
if (count($diffLines) === 1) {
209-
// Seems to be required for cygwin.
210-
$diffLines = explode("\n", $diff);
211-
}
212-
213-
$diff = [];
214-
foreach ($diffLines as $line) {
215-
if (isset($line[0]) === true) {
216-
switch ($line[0]) {
217-
case '-':
218-
$diff[] = "\033[31m$line\033[0m";
219-
break;
220-
case '+':
221-
$diff[] = "\033[32m$line\033[0m";
222-
break;
223-
default:
224-
$diff[] = $line;
225-
}
226-
}
227-
}
228-
229-
$diff = implode(PHP_EOL, $diff);
230-
231-
return $diff;
232-
}
233-
234175
/**
235176
* @return string
236177
*/
237178
public function getContents(): string
238179
{
239-
$contents = implode($this->tokens);
240-
241-
return $contents;
242-
}
243-
244-
/**
245-
* This function takes changesets into account so should be used
246-
* instead of directly accessing the token array.
247-
*
248-
* @param int $tokenPosition
249-
*
250-
* @return string
251-
*/
252-
public function getTokenContent(int $tokenPosition): string
253-
{
254-
if ($this->inChangeset && isset($this->changeset[$tokenPosition])) {
255-
return $this->changeset[$tokenPosition];
256-
}
257-
258-
return $this->tokens[$tokenPosition];
180+
return implode($this->tokens);
259181
}
260182

261183
/**
@@ -367,24 +289,6 @@ public function replaceToken(int $tokenPosition, string $content): bool
367289
return true;
368290
}
369291

370-
/**
371-
* @param int $tokenPosition
372-
*
373-
* @return bool
374-
*/
375-
public function revertToken(int $tokenPosition): bool
376-
{
377-
if (!isset($this->fixedTokens[$tokenPosition])) {
378-
return false;
379-
}
380-
381-
$this->tokens[$tokenPosition] = $this->fixedTokens[$tokenPosition];
382-
unset($this->fixedTokens[$tokenPosition]);
383-
$this->numFixes--;
384-
385-
return true;
386-
}
387-
388292
/**
389293
* @param int $tokenPosition
390294
*
@@ -434,4 +338,39 @@ public function addContentBefore(int $tokenPosition, string $content): bool
434338

435339
return $this->replaceToken($tokenPosition, $content.$current);
436340
}
341+
342+
/**
343+
* This function takes changesets into account so should be used
344+
* instead of directly accessing the token array.
345+
*
346+
* @param int $tokenPosition
347+
*
348+
* @return string
349+
*/
350+
protected function getTokenContent(int $tokenPosition): string
351+
{
352+
if ($this->inChangeset && isset($this->changeset[$tokenPosition])) {
353+
return $this->changeset[$tokenPosition];
354+
}
355+
356+
return $this->tokens[$tokenPosition];
357+
}
358+
359+
/**
360+
* @param int $tokenPosition
361+
*
362+
* @return bool
363+
*/
364+
protected function revertToken(int $tokenPosition): bool
365+
{
366+
if (!isset($this->fixedTokens[$tokenPosition])) {
367+
return false;
368+
}
369+
370+
$this->tokens[$tokenPosition] = $this->fixedTokens[$tokenPosition];
371+
unset($this->fixedTokens[$tokenPosition]);
372+
$this->numFixes--;
373+
374+
return true;
375+
}
437376
}

TwigCS/src/Runner/Linter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function run(array $files, Ruleset $ruleset, bool $fix = false): Report
8686
*
8787
* @throws Exception
8888
*/
89-
public function fix(iterable $files, Ruleset $ruleset): void
89+
protected function fix(iterable $files, Ruleset $ruleset): void
9090
{
9191
$fixer = new Fixer($ruleset, $this->tokenizer);
9292

@@ -113,7 +113,7 @@ public function fix(iterable $files, Ruleset $ruleset): void
113113
*
114114
* @return bool
115115
*/
116-
public function processTemplate(string $file, Ruleset $ruleset, Report $report): bool
116+
protected function processTemplate(string $file, Ruleset $ruleset, Report $report): bool
117117
{
118118
$twigSource = new Source(file_get_contents($file), $file);
119119

TwigCS/src/Sniff/AbstractSniff.php

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,30 @@ public function disable(): void
4747
$this->fixer = null;
4848
}
4949

50+
/**
51+
* @param array $stream
52+
*/
53+
public function processFile(array $stream): void
54+
{
55+
foreach ($stream as $index => $token) {
56+
$this->process($index, $stream);
57+
}
58+
}
59+
60+
/**
61+
* @param int $tokenPosition
62+
* @param Token[] $stream
63+
*/
64+
abstract protected function process(int $tokenPosition, array $stream): void;
65+
5066
/**
5167
* @param Token $token
5268
* @param int|array $type
5369
* @param string|array $value
5470
*
5571
* @return bool
5672
*/
57-
public function isTokenMatching(Token $token, $type, $value = []): bool
73+
protected function isTokenMatching(Token $token, $type, $value = []): bool
5874
{
5975
if (!is_array($type)) {
6076
$type = [$type];
@@ -74,7 +90,7 @@ public function isTokenMatching(Token $token, $type, $value = []): bool
7490
*
7591
* @return int|false
7692
*/
77-
public function findNext($type, array $tokens, int $start, bool $exclude = false)
93+
protected function findNext($type, array $tokens, int $start, bool $exclude = false)
7894
{
7995
$i = 0;
8096

@@ -97,7 +113,7 @@ public function findNext($type, array $tokens, int $start, bool $exclude = false
97113
*
98114
* @return int|false
99115
*/
100-
public function findPrevious($type, array $tokens, int $start, bool $exclude = false)
116+
protected function findPrevious($type, array $tokens, int $start, bool $exclude = false)
101117
{
102118
$i = 0;
103119

@@ -118,7 +134,7 @@ public function findPrevious($type, array $tokens, int $start, bool $exclude = f
118134
*
119135
* @throws Exception
120136
*/
121-
public function addWarning(string $message, Token $token): void
137+
protected function addWarning(string $message, Token $token): void
122138
{
123139
$this->addMessage(Report::MESSAGE_TYPE_WARNING, $message, $token);
124140
}
@@ -129,7 +145,7 @@ public function addWarning(string $message, Token $token): void
129145
*
130146
* @throws Exception
131147
*/
132-
public function addError(string $message, Token $token): void
148+
protected function addError(string $message, Token $token): void
133149
{
134150
$this->addMessage(Report::MESSAGE_TYPE_ERROR, $message, $token);
135151
}
@@ -142,7 +158,7 @@ public function addError(string $message, Token $token): void
142158
*
143159
* @throws Exception
144160
*/
145-
public function addFixableWarning(string $message, Token $token): bool
161+
protected function addFixableWarning(string $message, Token $token): bool
146162
{
147163
return $this->addFixableMessage(Report::MESSAGE_TYPE_WARNING, $message, $token);
148164
}
@@ -155,41 +171,11 @@ public function addFixableWarning(string $message, Token $token): bool
155171
*
156172
* @throws Exception
157173
*/
158-
public function addFixableError(string $message, Token $token): bool
174+
protected function addFixableError(string $message, Token $token): bool
159175
{
160176
return $this->addFixableMessage(Report::MESSAGE_TYPE_ERROR, $message, $token);
161177
}
162178

163-
/**
164-
* @param Token $token
165-
*
166-
* @return string|null
167-
*/
168-
public function stringifyValue(Token $token): ?string
169-
{
170-
if ($token->getType() === Token::STRING_TYPE) {
171-
return $token->getValue();
172-
}
173-
174-
return '\''.$token->getValue().'\'';
175-
}
176-
177-
/**
178-
* @param array $stream
179-
*/
180-
public function processFile(array $stream): void
181-
{
182-
foreach ($stream as $index => $token) {
183-
$this->process($index, $stream);
184-
}
185-
}
186-
187-
/**
188-
* @param int $tokenPosition
189-
* @param Token[] $stream
190-
*/
191-
abstract protected function process(int $tokenPosition, array $stream): void;
192-
193179
/**
194180
* @param int $messageType
195181
* @param string $message

TwigCS/src/Sniff/AbstractSpacingSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract protected function shouldHaveSpaceBefore(int $tokenPosition, array $tok
5555
*
5656
* @throws Exception
5757
*/
58-
protected function checkSpaceAfter(int $tokenPosition, array $tokens, int $expected): void
58+
private function checkSpaceAfter(int $tokenPosition, array $tokens, int $expected): void
5959
{
6060
$token = $tokens[$tokenPosition];
6161

@@ -94,7 +94,7 @@ protected function checkSpaceAfter(int $tokenPosition, array $tokens, int $expec
9494
*
9595
* @throws Exception
9696
*/
97-
protected function checkSpaceBefore(int $tokenPosition, array $tokens, int $expected): void
97+
private function checkSpaceBefore(int $tokenPosition, array $tokens, int $expected): void
9898
{
9999
$token = $tokens[$tokenPosition];
100100

TwigCS/src/Token/Token.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ class Token
2525
public const INTERPOLATION_END_TYPE = 11;
2626
public const ARROW_TYPE = 12;
2727
// New constants
28-
public const WHITESPACE_TYPE = 13;
29-
public const TAB_TYPE = 14;
30-
public const EOL_TYPE = 15;
31-
public const COMMENT_START_TYPE = 16;
32-
public const COMMENT_TEXT_TYPE = 17;
33-
public const COMMENT_WHITESPACE_TYPE = 18;
34-
public const COMMENT_TAB_TYPE = 19;
35-
public const COMMENT_EOL_TYPE = 20;
36-
public const COMMENT_END_TYPE = 21;
28+
public const DQ_STRING_START_TYPE = 13;
29+
public const DQ_STRING_END_TYPE = 14;
30+
public const BLOCK_TAG_TYPE = 15;
31+
public const WHITESPACE_TYPE = 16;
32+
public const TAB_TYPE = 17;
33+
public const EOL_TYPE = 18;
34+
public const COMMENT_START_TYPE = 19;
35+
public const COMMENT_TEXT_TYPE = 20;
36+
public const COMMENT_WHITESPACE_TYPE = 21;
37+
public const COMMENT_TAB_TYPE = 22;
38+
public const COMMENT_EOL_TYPE = 23;
39+
public const COMMENT_END_TYPE = 24;
3740

3841
public const EMPTY_TOKENS = [
3942
self::WHITESPACE_TYPE => self::WHITESPACE_TYPE,

0 commit comments

Comments
 (0)