Skip to content

Commit 328de9e

Browse files
Merge pull request #59 from VincentLanglet/fixTwigCs
🐛 Bugfix
2 parents 0740604 + 8ab4ec5 commit 328de9e

21 files changed

+303
-233
lines changed

TwigCS/Command/TwigCSCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TwigCS\Command;
44

5+
use \Exception;
56
use Symfony\Component\Console\Command\Command;
67
use Symfony\Component\Console\Input\InputArgument;
78
use Symfony\Component\Console\Input\InputInterface;
@@ -65,7 +66,7 @@ protected function configure()
6566
*
6667
* @return int
6768
*
68-
* @throws \Exception
69+
* @throws Exception
6970
*/
7071
protected function execute(InputInterface $input, OutputInterface $output)
7172
{

TwigCS/Config/Config.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TwigCS\Config;
44

5+
use \Exception;
56
use Symfony\Component\Finder\Finder;
67

78
/**
@@ -43,6 +44,8 @@ public function __construct()
4344
* Find all files to process, based on a file or directory and exclude patterns.
4445
*
4546
* @return array
47+
*
48+
* @throws Exception
4649
*/
4750
public function findFiles()
4851
{
@@ -74,11 +77,13 @@ public function findFiles()
7477
* @param string $key
7578
*
7679
* @return mixed
80+
*
81+
* @throws Exception
7782
*/
7883
public function get($key)
7984
{
8085
if (!isset($this->config[$key])) {
81-
throw new \Exception(sprintf('Configuration key "%s" does not exist', $key));
86+
throw new Exception(sprintf('Configuration key "%s" does not exist', $key));
8287
}
8388

8489
return $this->config[$key];

TwigCS/Environment/StubbedEnvironment.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace TwigCS\Environment;
44

5+
use \Closure;
6+
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
7+
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
8+
use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
9+
use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
10+
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
11+
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
512
use Twig\Environment;
613
use Twig\Loader\LoaderInterface;
714
use Twig\TwigFilter;
@@ -31,7 +38,7 @@ class StubbedEnvironment extends Environment
3138
private $stubTests;
3239

3340
/**
34-
* @var \Closure
41+
* @var Closure
3542
*/
3643
private $stubCallable;
3744

@@ -43,6 +50,13 @@ public function __construct(LoaderInterface $loader = null, $options = [])
4350
{
4451
parent::__construct($loader, $options);
4552

53+
$this->addTokenParser(new DumpTokenParser());
54+
$this->addTokenParser(new FormThemeTokenParser());
55+
$this->addTokenParser(new StopwatchTokenParser(false));
56+
$this->addTokenParser(new TransChoiceTokenParser());
57+
$this->addTokenParser(new TransDefaultDomainTokenParser());
58+
$this->addTokenParser(new TransTokenParser());
59+
4660
$this->stubCallable = function () {
4761
/* This will be used as stub filter, function or test */
4862
};

TwigCS/Linter.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TwigCS;
44

5+
use \Exception;
6+
use \Traversable;
57
use Twig\Environment;
68
use Twig\Error\Error;
79
use Twig\Source;
@@ -12,7 +14,7 @@
1214
use TwigCS\Sniff\PostParserSniffInterface;
1315
use TwigCS\Sniff\PreParserSniffInterface;
1416
use TwigCS\Sniff\SniffInterface;
15-
use TwigCS\Token\TokenizerInterface;
17+
use TwigCS\Token\Tokenizer;
1618

1719
/**
1820
* Linter is the main class and will process twig files against a set of rules.
@@ -30,17 +32,18 @@ class Linter
3032
protected $sniffsExtension;
3133

3234
/**
33-
* @var TokenizerInterface
35+
* @var Tokenizer
3436
*/
3537
protected $tokenizer;
3638

3739
/**
38-
* @param Environment $env
39-
* @param TokenizerInterface $tokenizer
40+
* @param Environment $env
41+
* @param Tokenizer $tokenizer
4042
*/
41-
public function __construct(Environment $env, TokenizerInterface $tokenizer)
43+
public function __construct(Environment $env, Tokenizer $tokenizer)
4244
{
4345
$this->env = $env;
46+
4447
$this->sniffsExtension = $this->env->getExtension('TwigCS\Extension\SniffsExtension');
4548
$this->tokenizer = $tokenizer;
4649
}
@@ -52,15 +55,17 @@ public function __construct(Environment $env, TokenizerInterface $tokenizer)
5255
* @param Ruleset $ruleset Set of rules to check.
5356
*
5457
* @return Report an object with all violations and stats.
58+
*
59+
* @throws Exception
5560
*/
5661
public function run($files, Ruleset $ruleset)
5762
{
58-
if (!is_array($files) && !$files instanceof \Traversable) {
63+
if (!is_array($files) && !$files instanceof Traversable) {
5964
$files = [$files];
6065
}
6166

6267
if (empty($files)) {
63-
throw new \Exception('No files to process, provide at least one file to be linted');
68+
throw new Exception('No files to process, provide at least one file to be linted');
6469
}
6570

6671
// setUp
@@ -70,8 +75,8 @@ public function run($files, Ruleset $ruleset)
7075
$sniffViolation = new SniffViolation(
7176
SniffInterface::MESSAGE_TYPE_NOTICE,
7277
$msg,
73-
'',
74-
''
78+
null,
79+
null
7580
);
7681

7782
$report->addMessage($sniffViolation);
@@ -139,11 +144,11 @@ public function processTemplate($file, $ruleset, $report)
139144
// Tokenizer.
140145
try {
141146
$stream = $this->tokenizer->tokenize($twigSource);
142-
} catch (\Exception $e) {
147+
} catch (Exception $e) {
143148
$sniffViolation = new SniffViolation(
144149
SniffInterface::MESSAGE_TYPE_ERROR,
145-
sprintf('Unable to tokenize file "%s"', (string) $file),
146-
'',
150+
sprintf('Unable to tokenize file'),
151+
null,
147152
(string) $file
148153
);
149154

TwigCS/Report/SniffViolation.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ public function getLevelAsString()
9191
case SniffInterface::MESSAGE_TYPE_WARNING:
9292
return 'WARNING';
9393
case SniffInterface::MESSAGE_TYPE_ERROR:
94+
default:
9495
return 'ERROR';
9596
}
96-
97-
throw new \Exception(sprintf('Unknown level "%s"', $this->level));
9897
}
9998

10099
/**
@@ -112,10 +111,9 @@ public static function getLevelAsInt($level)
112111
case 'WARNING':
113112
return SniffInterface::MESSAGE_TYPE_WARNING;
114113
case 'ERROR':
114+
default:
115115
return SniffInterface::MESSAGE_TYPE_ERROR;
116116
}
117-
118-
throw new \Exception(sprintf('Unknown level "%s"', $level));
119117
}
120118

121119
/**

TwigCS/Report/TextFormatter.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,40 @@ public function display(Report $report, $level = null)
4646
'level' => $level,
4747
]);
4848

49-
$this->io->text((count($fileMessages) > 0 ? '<fg=red>KO</fg=red>' : '<info>OK</info>').' '.$file);
49+
if (count($fileMessages) > 0) {
50+
$this->io->text('<fg=red>KO</fg=red> '.$file);
51+
}
5052

5153
$rows = [];
5254
foreach ($fileMessages as $message) {
5355
$lines = $this->getContext(file_get_contents($file), $message->getLine(), $this::ERROR_CONTEXT_LIMIT);
5456

5557
$formattedText = [];
58+
if (!$message->getLine()) {
59+
$formattedText[] = $this->formatErrorMessage($message);
60+
}
61+
5662
foreach ($lines as $no => $code) {
5763
$formattedText[] = sprintf($this::ERROR_LINE_FORMAT, $no, wordwrap($code, $this::ERROR_LINE_WIDTH));
5864

5965
if ($no === $message->getLine()) {
60-
$formattedText[] = sprintf(
61-
'<fg=red>'.$this::ERROR_LINE_FORMAT.'</fg=red>',
62-
$this::ERROR_CURSOR_CHAR,
63-
wordwrap($message->getMessage(), $this::ERROR_LINE_WIDTH)
64-
);
66+
$formattedText[] = $this->formatErrorMessage($message);
6567
}
6668
}
6769

70+
if (count($rows) > 0) {
71+
$rows[] = new TableSeparator();
72+
}
73+
6874
$rows[] = [
69-
new TableCell('<comment>'.$message->getLevelAsString().'</comment>', ['rowspan' => 2]),
75+
new TableCell('<comment>'.$message->getLevelAsString().'</comment>'),
7076
implode("\n", $formattedText),
7177
];
72-
$rows[] = new TableSeparator();
7378
}
7479

75-
$this->io->table([], $rows);
80+
if (count($rows) > 0) {
81+
$this->io->table([], $rows);
82+
}
7683
}
7784

7885
$summaryString = sprintf(
@@ -131,4 +138,18 @@ protected function getContext($template, $line, $context)
131138

132139
return $result;
133140
}
141+
142+
/**
143+
* @param SniffViolation $message
144+
*
145+
* @return string
146+
*/
147+
protected function formatErrorMessage(SniffViolation $message)
148+
{
149+
return sprintf(
150+
'<fg=red>'.$this::ERROR_LINE_FORMAT.'</fg=red>',
151+
$this::ERROR_CURSOR_CHAR,
152+
wordwrap($message->getMessage(), $this::ERROR_LINE_WIDTH)
153+
);
154+
}
134155
}

TwigCS/Ruleset/Ruleset.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TwigCS\Ruleset;
44

5+
use \Exception;
56
use TwigCS\Sniff\PostParserSniffInterface;
67
use TwigCS\Sniff\PreParserSniffInterface;
78
use TwigCS\Sniff\SniffInterface;
@@ -69,6 +70,8 @@ public function addPostParserSniff(PostParserSniffInterface $sniff)
6970
* @param SniffInterface $sniff
7071
*
7172
* @return $this
73+
*
74+
* @throws Exception
7275
*/
7376
public function addSniff(SniffInterface $sniff)
7477
{
@@ -88,7 +91,7 @@ public function addSniff(SniffInterface $sniff)
8891
return $this;
8992
}
9093

91-
throw new \Exception(sprintf(
94+
throw new Exception(sprintf(
9295
'Unknown type of sniff "%s", expected one of: "%s"',
9396
$sniff->getType(),
9497
implode(', ', [SniffInterface::TYPE_PRE_PARSER, SniffInterface::TYPE_POST_PARSER])

TwigCS/Ruleset/RulesetFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TwigCS\Ruleset;
44

5+
use \Exception;
6+
use \SplFileInfo;
57
use Symfony\Component\Finder\Finder;
68

79
/**
@@ -13,14 +15,16 @@ class RulesetFactory
1315
* Create a new set of rule.
1416
*
1517
* @return Ruleset
18+
*
19+
* @throws Exception
1620
*/
1721
public function createStandardRuleset()
1822
{
1923
$ruleset = new Ruleset();
2024

2125
$finder = Finder::create()->in(__DIR__.'/../Sniff/Standard')->files();
2226

23-
/** @var \SplFileInfo $file */
27+
/** @var SplFileInfo $file */
2428
foreach ($finder as $file) {
2529
$class = 'TwigCS\Sniff\Standard\\'.explode('.', $file->getFilename())[0];
2630
$ruleset->addSniff(new $class());

TwigCS/Sniff/AbstractPostParserSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TwigCS\Sniff;
44

5+
use \Exception;
56
use Twig\Node\Expression\Binary\ConcatBinary;
67
use Twig\Node\Expression\ConstantExpression;
78
use Twig\Node\Expression\FilterExpression;
@@ -38,6 +39,8 @@ public function getType()
3839
* @param Node $node
3940
*
4041
* @return self
42+
*
43+
* @throws Exception
4144
*/
4245
public function addMessage($messageType, $message, Node $node)
4346
{
@@ -56,7 +59,7 @@ public function addMessage($messageType, $message, Node $node)
5659
/**
5760
* @param Node $node
5861
*
59-
* @return string
62+
* @return int|null
6063
*/
6164
public function getTemplateLine(Node $node)
6265
{
@@ -68,7 +71,7 @@ public function getTemplateLine(Node $node)
6871
return $node->getLine();
6972
}
7073

71-
return '';
74+
return null;
7275
}
7376

7477
/**

TwigCS/Sniff/AbstractPreParserSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TwigCS\Sniff;
44

5+
use \Exception;
56
use TwigCS\Report\SniffViolation;
67
use TwigCS\Token\Token;
78

@@ -46,6 +47,8 @@ public function isTokenMatching(Token $token, $type, $value = null)
4647
* @param Token $token
4748
*
4849
* @return self
50+
*
51+
* @throws Exception
4952
*/
5053
public function addMessage($messageType, $message, Token $token)
5154
{

TwigCS/Sniff/AbstractSniff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TwigCS\Sniff;
44

5+
use \Exception;
56
use TwigCS\Report\Report;
67

78
/**
@@ -51,11 +52,13 @@ public function disable()
5152

5253
/**
5354
* @return Report
55+
*
56+
* @throws Exception
5457
*/
5558
public function getReport()
5659
{
5760
if (null === $this->report) {
58-
throw new \Exception('Sniff is disabled!');
61+
throw new Exception('Sniff is disabled!');
5962
}
6063

6164
return $this->report;

0 commit comments

Comments
 (0)