Skip to content

Commit e5a8d6a

Browse files
Merge pull request #63 from VincentLanglet/whitespace
✨ Add Whitespace check for delimiter
2 parents 41306de + 43adc22 commit e5a8d6a

34 files changed

+394
-817
lines changed

SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected function processParams(
269269
*
270270
* @return bool True if the return does not return anything
271271
*/
272-
protected function isMatchingReturn($tokens, $returnPos)
272+
protected function isMatchingReturn(array $tokens, $returnPos)
273273
{
274274
do {
275275
$returnPos++;

TwigCS/Command/TwigCSCommand.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Input\InputOption;
1010
use Symfony\Component\Console\Output\OutputInterface;
11-
use Twig\Loader\ArrayLoader;
1211
use TwigCS\Config\Config;
1312
use TwigCS\Environment\StubbedEnvironment;
1413
use TwigCS\Linter;
1514
use TwigCS\Report\TextFormatter;
16-
use TwigCS\Ruleset\RulesetFactory;
15+
use TwigCS\Ruleset\Ruleset;
1716
use TwigCS\Token\Tokenizer;
1817

1918
/**
@@ -33,16 +32,16 @@ protected function configure()
3332
new InputOption(
3433
'exclude',
3534
'e',
36-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
35+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
3736
'Excludes, based on regex, paths of files and folders from parsing',
3837
['vendor/']
3938
),
4039
new InputOption(
4140
'level',
4241
'l',
4342
InputOption::VALUE_OPTIONAL,
44-
'Allowed values are: warning, error',
45-
'warning'
43+
'Allowed values are notice, warning or error',
44+
'notice'
4645
),
4746
new InputOption(
4847
'working-dir',
@@ -81,26 +80,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
8180
'workingDirectory' => $currentDir,
8281
]);
8382

84-
$twig = new StubbedEnvironment(new ArrayLoader(), ['stub_tags' => $config->get('stub')]);
85-
$linter = new Linter($twig, new Tokenizer($twig));
86-
$factory = new RulesetFactory();
87-
$reporter = new TextFormatter($input, $output);
88-
$exitCode = 0;
89-
9083
// Get the rules to apply.
91-
$ruleset = $factory->createStandardRuleset();
84+
$ruleset = new Ruleset();
85+
$ruleset->addStandard();
9286

9387
// Execute the linter.
88+
$twig = new StubbedEnvironment();
89+
$linter = new Linter($twig, new Tokenizer($twig));
9490
$report = $linter->run($config->findFiles(), $ruleset);
9591

9692
// Format the output.
93+
$reporter = new TextFormatter($input, $output);
9794
$reporter->display($report, $level);
9895

9996
// Return a meaningful error code.
10097
if ($report->getTotalErrors()) {
101-
$exitCode = 1;
98+
return 1;
10299
}
103100

104-
return $exitCode;
101+
return 0;
105102
}
106103
}

TwigCS/Config/Config.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Config
1919
'exclude' => [],
2020
'pattern' => '*.twig',
2121
'paths' => [],
22-
'stub' => [],
2322
'workingDirectory' => '',
2423
];
2524

@@ -30,14 +29,12 @@ class Config
3029
*/
3130
protected $config;
3231

33-
public function __construct()
32+
/**
33+
* @param array $config
34+
*/
35+
public function __construct(array $config = [])
3436
{
35-
$args = func_get_args();
36-
37-
$this->config = $this::$defaultConfig;
38-
foreach ($args as $arg) {
39-
$this->config = array_merge($this->config, $arg);
40-
}
37+
$this->config = array_merge($this::$defaultConfig, $config);
4138
}
4239

4340
/**
@@ -68,7 +65,7 @@ public function findFiles()
6865
$files->exclude($exclude);
6966
}
7067

71-
return $files;
68+
return iterator_to_array($files, false);
7269
}
7370

7471
/**
@@ -80,7 +77,7 @@ public function findFiles()
8077
*
8178
* @throws Exception
8279
*/
83-
public function get($key)
80+
public function get(string $key)
8481
{
8582
if (!isset($this->config[$key])) {
8683
throw new Exception(sprintf('Configuration key "%s" does not exist', $key));

TwigCS/Environment/StubbedEnvironment.php

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33
namespace TwigCS\Environment;
44

5-
use \Closure;
65
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
76
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
87
use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
98
use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
109
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
1110
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
1211
use Twig\Environment;
13-
use Twig\Loader\LoaderInterface;
12+
use Twig\Loader\ArrayLoader;
1413
use Twig\TwigFilter;
1514
use Twig\TwigFunction;
1615
use Twig\TwigTest;
17-
use TwigCS\Extension\SniffsExtension;
18-
use TwigCS\Token\TokenParser;
1916

2017
/**
2118
* Provide stubs for all filters, functions, tests and tags that are not defined in twig's core.
@@ -37,18 +34,9 @@ class StubbedEnvironment extends Environment
3734
*/
3835
private $stubTests;
3936

40-
/**
41-
* @var Closure
42-
*/
43-
private $stubCallable;
44-
45-
/**
46-
* @param LoaderInterface|null $loader
47-
* @param array $options
48-
*/
49-
public function __construct(LoaderInterface $loader = null, $options = [])
37+
public function __construct()
5038
{
51-
parent::__construct($loader, $options);
39+
parent::__construct(new ArrayLoader());
5240

5341
$this->addTokenParser(new DumpTokenParser());
5442
$this->addTokenParser(new FormThemeTokenParser());
@@ -57,27 +45,9 @@ public function __construct(LoaderInterface $loader = null, $options = [])
5745
$this->addTokenParser(new TransDefaultDomainTokenParser());
5846
$this->addTokenParser(new TransTokenParser());
5947

60-
$this->stubCallable = function () {
61-
/* This will be used as stub filter, function or test */
62-
};
63-
64-
$this->stubFilters = [];
48+
$this->stubFilters = [];
6549
$this->stubFunctions = [];
66-
67-
if (isset($options['stub_tags'])) {
68-
foreach ($options['stub_tags'] as $tag) {
69-
$this->addTokenParser(new TokenParser($tag));
70-
}
71-
}
72-
7350
$this->stubTests = [];
74-
if (isset($options['stub_tests'])) {
75-
foreach ($options['stub_tests'] as $test) {
76-
$this->stubTests[$test] = new TwigTest('stub', $this->stubCallable);
77-
}
78-
}
79-
80-
$this->addExtension(new SniffsExtension());
8151
}
8252

8353
/**
@@ -88,7 +58,7 @@ public function __construct(LoaderInterface $loader = null, $options = [])
8858
public function getFilter($name)
8959
{
9060
if (!isset($this->stubFilters[$name])) {
91-
$this->stubFilters[$name] = new TwigFilter('stub', $this->stubCallable);
61+
$this->stubFilters[$name] = new TwigFilter('stub');
9262
}
9363

9464
return $this->stubFilters[$name];
@@ -102,7 +72,7 @@ public function getFilter($name)
10272
public function getFunction($name)
10373
{
10474
if (!isset($this->stubFunctions[$name])) {
105-
$this->stubFunctions[$name] = new TwigFunction('stub', $this->stubCallable);
75+
$this->stubFunctions[$name] = new TwigFunction('stub');
10676
}
10777

10878
return $this->stubFunctions[$name];
@@ -111,19 +81,14 @@ public function getFunction($name)
11181
/**
11282
* @param string $name
11383
*
114-
* @return false|TwigTest
84+
* @return TwigTest
11585
*/
11686
public function getTest($name)
11787
{
118-
$test = parent::getTest($name);
119-
if ($test) {
120-
return $test;
121-
}
122-
123-
if (isset($this->stubTests[$name])) {
124-
return $this->stubTests[$name];
88+
if (!isset($this->stubTests[$name])) {
89+
$this->stubTests[$name] = new TwigTest('stub');
12590
}
12691

127-
return false;
92+
return $this->stubTests[$name];
12893
}
12994
}

TwigCS/Extension/SniffsExtension.php

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

0 commit comments

Comments
 (0)