Skip to content

Commit 799de09

Browse files
Merge pull request #65 from VincentLanglet/fixer
Add TwigCS Fixer
2 parents e5a8d6a + 59c91b5 commit 799de09

13 files changed

+668
-108
lines changed

TwigCS/Command/TwigCSCommand.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Symfony\Component\Console\Output\OutputInterface;
1111
use TwigCS\Config\Config;
1212
use TwigCS\Environment\StubbedEnvironment;
13-
use TwigCS\Linter;
1413
use TwigCS\Report\TextFormatter;
1514
use TwigCS\Ruleset\Ruleset;
15+
use TwigCS\Runner\Linter;
1616
use TwigCS\Token\Tokenizer;
1717

1818
/**
@@ -50,6 +50,12 @@ protected function configure()
5050
'Run as if this was started in <working-dir> instead of the current working directory',
5151
getcwd()
5252
),
53+
new InputOption(
54+
'fix',
55+
'f',
56+
InputOption::VALUE_NONE,
57+
'Automatically fix all the fixable violations'
58+
),
5359
])
5460
->addArgument(
5561
'paths',
@@ -69,10 +75,11 @@ protected function configure()
6975
*/
7076
protected function execute(InputInterface $input, OutputInterface $output)
7177
{
72-
$paths = $input->getArgument('paths');
73-
$exclude = $input->getOption('exclude');
74-
$level = $input->getOption('level');
78+
$paths = $input->getArgument('paths');
79+
$exclude = $input->getOption('exclude');
80+
$level = $input->getOption('level');
7581
$currentDir = $input->getOption('working-dir');
82+
$fix = $input->getOption('fix');
7683

7784
$config = new Config([
7885
'paths' => $paths,
@@ -87,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8794
// Execute the linter.
8895
$twig = new StubbedEnvironment();
8996
$linter = new Linter($twig, new Tokenizer($twig));
90-
$report = $linter->run($config->findFiles(), $ruleset);
97+
$report = $linter->run($config->findFiles(), $ruleset, $fix);
9198

9299
// Format the output.
93100
$reporter = new TextFormatter($input, $output);

TwigCS/Environment/StubbedEnvironment.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ class StubbedEnvironment extends Environment
2222
/**
2323
* @var TwigFilter[]
2424
*/
25-
private $stubFilters;
25+
protected $stubFilters = [];
2626

2727
/**
2828
* @var TwigFunction[]
2929
*/
30-
private $stubFunctions;
30+
protected $stubFunctions = [];
3131

3232
/**
3333
* @var TwigTest[]
3434
*/
35-
private $stubTests;
35+
protected $stubTests = [];
3636

3737
public function __construct()
3838
{
@@ -44,10 +44,6 @@ public function __construct()
4444
$this->addTokenParser(new TransChoiceTokenParser());
4545
$this->addTokenParser(new TransDefaultDomainTokenParser());
4646
$this->addTokenParser(new TransTokenParser());
47-
48-
$this->stubFilters = [];
49-
$this->stubFunctions = [];
50-
$this->stubTests = [];
5147
}
5248

5349
/**

TwigCS/Ruleset/Generic/BlankEOFSniff.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,45 @@
1212
class BlankEOFSniff extends AbstractSniff
1313
{
1414
/**
15-
* @param Token $token
1615
* @param int $tokenPosition
1716
* @param Token[] $tokens
1817
*
1918
* @return Token
2019
*
2120
* @throws Exception
2221
*/
23-
public function process(Token $token, int $tokenPosition, array $tokens)
22+
public function process(int $tokenPosition, array $tokens)
2423
{
24+
$token = $tokens[$tokenPosition];
25+
2526
if ($this->isTokenMatching($token, Token::EOF_TYPE)) {
2627
$i = 0;
2728
while (isset($tokens[$tokenPosition - ($i + 1)])
2829
&& $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE)
2930
) {
30-
++$i;
31+
$i++;
3132
}
3233

3334
if (1 !== $i) {
3435
// Either 0 or 2+ blank lines.
35-
$this->addMessage(
36+
$fix = $this->addFixableMessage(
3637
$this::MESSAGE_TYPE_ERROR,
3738
sprintf('A file must end with 1 blank line; found %d', $i),
3839
$token
3940
);
41+
42+
if ($fix) {
43+
if (0 === $i) {
44+
$this->fixer->addNewlineBefore($tokenPosition);
45+
} else {
46+
$this->fixer->beginChangeset();
47+
while ($i > 1) {
48+
$this->fixer->replaceToken($tokenPosition - $i, '');
49+
$i--;
50+
}
51+
$this->fixer->endChangeset();
52+
}
53+
}
4054
}
4155
}
4256

TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,44 @@
1212
class DelimiterSpacingSniff extends AbstractSniff
1313
{
1414
/**
15-
* @param Token $token
1615
* @param int $tokenPosition
1716
* @param Token[] $tokens
1817
*
1918
* @return Token
2019
*
2120
* @throws Exception
2221
*/
23-
public function process(Token $token, int $tokenPosition, array $tokens)
22+
public function process(int $tokenPosition, array $tokens)
2423
{
24+
$token = $tokens[$tokenPosition];
25+
2526
if ($this->isTokenMatching($token, Token::VAR_START_TYPE)
2627
|| $this->isTokenMatching($token, Token::BLOCK_START_TYPE)
2728
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE)
2829
) {
29-
$this->processStart($token, $tokenPosition, $tokens);
30+
$this->processStart($tokenPosition, $tokens);
3031
}
3132

3233
if ($this->isTokenMatching($token, Token::VAR_END_TYPE)
3334
|| $this->isTokenMatching($token, Token::BLOCK_END_TYPE)
3435
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE)
3536
) {
36-
$this->processEnd($token, $tokenPosition, $tokens);
37+
$this->processEnd($tokenPosition, $tokens);
3738
}
3839

3940
return $token;
4041
}
4142

4243
/**
43-
* @param Token $token
4444
* @param int $tokenPosition
4545
* @param Token[] $tokens
4646
*
4747
* @throws Exception
4848
*/
49-
public function processStart(Token $token, $tokenPosition, $tokens)
49+
public function processStart(int $tokenPosition, array $tokens)
5050
{
51+
$token = $tokens[$tokenPosition];
52+
5153
// Ignore new line
5254
if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::EOL_TYPE)) {
5355
return;
@@ -60,23 +62,32 @@ public function processStart(Token $token, $tokenPosition, $tokens)
6062
}
6163

6264
if (1 !== $count) {
63-
$this->addMessage(
65+
$fix = $this->addFixableMessage(
6466
$this::MESSAGE_TYPE_ERROR,
6567
sprintf('Expecting 1 whitespace after "%s"; found %d', $token->getValue(), $count),
6668
$token
6769
);
70+
71+
if ($fix) {
72+
if (0 === $count) {
73+
$this->fixer->addContent($tokenPosition, ' ');
74+
} else {
75+
$this->fixer->replaceToken($tokenPosition + 1, ' ');
76+
}
77+
}
6878
}
6979
}
7080

7181
/**
72-
* @param Token $token
7382
* @param int $tokenPosition
7483
* @param Token[] $tokens
7584
*
7685
* @throws Exception
7786
*/
78-
public function processEnd(Token $token, $tokenPosition, $tokens)
87+
public function processEnd(int $tokenPosition, array $tokens)
7988
{
89+
$token = $tokens[$tokenPosition];
90+
8091
// Ignore new line
8192
if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::EOL_TYPE)) {
8293
return;
@@ -89,11 +100,19 @@ public function processEnd(Token $token, $tokenPosition, $tokens)
89100
}
90101

91102
if (1 !== $count) {
92-
$this->addMessage(
103+
$fix = $this->addFixableMessage(
93104
$this::MESSAGE_TYPE_ERROR,
94105
sprintf('Expecting 1 whitespace before "%s"; found %d', $token->getValue(), $count),
95106
$token
96107
);
108+
109+
if ($fix) {
110+
if (0 === $count) {
111+
$this->fixer->addContentBefore($tokenPosition, ' ');
112+
} else {
113+
$this->fixer->replaceToken($tokenPosition - 1, ' ');
114+
}
115+
}
97116
}
98117
}
99118
}

TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
class DisallowCommentedCodeSniff extends AbstractSniff
1515
{
1616
/**
17-
* @param Token $token
1817
* @param int $tokenPosition
1918
* @param Token[] $tokens
2019
*
2120
* @return Token
2221
*
2322
* @throws Exception
2423
*/
25-
public function process(Token $token, int $tokenPosition, array $tokens)
24+
public function process(int $tokenPosition, array $tokens)
2625
{
26+
$token = $tokens[$tokenPosition];
27+
2728
if ($this->isTokenMatching($token, Token::COMMENT_START_TYPE)) {
2829
$i = $tokenPosition;
2930
$found = false;

TwigCS/Ruleset/Ruleset.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ class Ruleset
1515
/**
1616
* @var SniffInterface[]
1717
*/
18-
protected $sniffs;
19-
20-
public function __construct()
21-
{
22-
$this->sniffs = [];
23-
}
18+
protected $sniffs = [];
2419

2520
/**
2621
* @return SniffInterface[]

0 commit comments

Comments
 (0)