Skip to content

Commit edb3738

Browse files
📦 More cleaning
1 parent f8e18ef commit edb3738

13 files changed

+55
-76
lines changed

TwigCS/Command/TwigCSCommand.php

Lines changed: 12 additions & 15 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',
38-
['vendor/']
37+
[]
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());
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ class Config
1616
* @var array
1717
*/
1818
public static $defaultConfig = [
19-
'exclude' => [],
19+
'exclude' => ['/vendor'],
2020
'pattern' => '*.twig',
2121
'paths' => [],
22-
'stub' => [],
2322
'workingDirectory' => '',
2423
];
2524

TwigCS/Environment/StubbedEnvironment.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
1010
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
1111
use Twig\Environment;
12-
use Twig\Loader\LoaderInterface;
12+
use Twig\Loader\ArrayLoader;
1313
use Twig\TwigFilter;
1414
use Twig\TwigFunction;
1515
use Twig\TwigTest;
@@ -34,13 +34,9 @@ class StubbedEnvironment extends Environment
3434
*/
3535
private $stubTests;
3636

37-
/**
38-
* @param LoaderInterface|null $loader
39-
* @param array $options
40-
*/
41-
public function __construct(LoaderInterface $loader = null, $options = [])
37+
public function __construct()
4238
{
43-
parent::__construct($loader, $options);
39+
parent::__construct(new ArrayLoader());
4440

4541
$this->addTokenParser(new DumpTokenParser());
4642
$this->addTokenParser(new FormThemeTokenParser());

TwigCS/Sniff/Standard/BlankEOFSniff.php renamed to TwigCS/Ruleset/Generic/BlankEOFSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace TwigCS\Sniff\Standard;
3+
namespace TwigCS\Ruleset\Generic;
44

55
use \Exception;
66
use TwigCS\Sniff\AbstractSniff;

TwigCS/Sniff/Standard/DelimiterSpacingSniff.php renamed to TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace TwigCS\Sniff\Standard;
3+
namespace TwigCS\Ruleset\Generic;
44

55
use \Exception;
66
use TwigCS\Sniff\AbstractSniff;

TwigCS/Sniff/Standard/DisallowCommentedCodeSniff.php renamed to TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace TwigCS\Sniff\Standard;
3+
namespace TwigCS\Ruleset\Generic;
44

55
use \Exception;
66
use TwigCS\Sniff\AbstractSniff;

TwigCS/Ruleset/Ruleset.php

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

33
namespace TwigCS\Ruleset;
44

5+
use \Exception;
6+
use \SplFileInfo;
7+
use Symfony\Component\Finder\Finder;
58
use TwigCS\Sniff\SniffInterface;
69

710
/**
@@ -40,14 +43,29 @@ public function addSniff(SniffInterface $sniff)
4043
}
4144

4245
/**
43-
* @param string $sniffClass
46+
* Create a new set of rule.
4447
*
45-
* @return $this
48+
* @param string $standardName
49+
*
50+
* @return Ruleset
51+
*
52+
* @throws Exception
4653
*/
47-
public function removeSniff($sniffClass)
54+
public function addStandard($standardName = 'Generic')
4855
{
49-
if (isset($this->sniffs[$sniffClass])) {
50-
unset($this->sniffs[$sniffClass]);
56+
try {
57+
$finder = Finder::create()->in(__DIR__.'/'.$standardName)->files();
58+
} catch (Exception $e) {
59+
throw new Exception(sprintf('The standard "%s" is not found.', $standardName));
60+
}
61+
62+
/** @var SplFileInfo $file */
63+
foreach ($finder as $file) {
64+
$class = __NAMESPACE__.'\\'.$standardName.'\\'.$file->getBasename('.php');
65+
66+
if (class_exists($class)) {
67+
$this->addSniff(new $class());
68+
}
5169
}
5270

5371
return $this;

TwigCS/Ruleset/RulesetFactory.php

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

TwigCS/Tests/AbstractSniffTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
use \Exception;
66
use \ReflectionClass;
7-
use PHPUnit\Framework\MockObject\MockObject;
87
use PHPUnit\Framework\TestCase;
9-
use Twig\Loader\LoaderInterface;
108
use TwigCS\Environment\StubbedEnvironment;
119
use TwigCS\Linter;
1210
use TwigCS\Report\SniffViolation;
@@ -31,12 +29,15 @@ abstract class AbstractSniffTest extends TestCase
3129

3230
public function setUp()
3331
{
34-
/** @var LoaderInterface|MockObject $twigLoaderInterface */
35-
$twigLoaderInterface = $this->getMockBuilder(LoaderInterface::class)->getMock();
36-
$this->env = new StubbedEnvironment($twigLoaderInterface);
32+
$this->env = new StubbedEnvironment();
3733
$this->lint = new Linter($this->env, new Tokenizer($this->env));
3834
}
3935

36+
/**
37+
* Should call $this->checkGenericSniff(new Sniff(), [...]);
38+
*/
39+
abstract public function testSniff();
40+
4041
/**
4142
* @param SniffInterface $sniff
4243
* @param array $expects

TwigCS/Tests/Sniff/BlankEOFTest.php renamed to TwigCS/Tests/Ruleset/Generic/BlankEOFTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace TwigCS\Tests\Sniff;
3+
namespace TwigCS\Tests\Ruleset\Generic;
44

5-
use TwigCS\Sniff\Standard\BlankEOFSniff;
5+
use TwigCS\Ruleset\Generic\BlankEOFSniff;
66
use TwigCS\Tests\AbstractSniffTest;
77

88
/**

TwigCS/Tests/Sniff/DelimiterSpacingTest.php renamed to TwigCS/Tests/Ruleset/Generic/DelimiterSpacingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace TwigCS\Tests\Sniff;
3+
namespace TwigCS\Tests\Ruleset\Generic;
44

5-
use TwigCS\Sniff\Standard\DelimiterSpacingSniff;
5+
use TwigCS\Ruleset\Generic\DelimiterSpacingSniff;
66
use TwigCS\Tests\AbstractSniffTest;
77

88
/**

TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php renamed to TwigCS/Tests/Ruleset/Generic/DisallowCommentedCodeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace TwigCS\Tests\Sniff;
3+
namespace TwigCS\Tests\Ruleset\Generic;
44

5-
use TwigCS\Sniff\Standard\DisallowCommentedCodeSniff;
5+
use TwigCS\Ruleset\Generic\DisallowCommentedCodeSniff;
66
use TwigCS\Tests\AbstractSniffTest;
77

88
/**

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)