Skip to content

Commit f501e32

Browse files
📦 Stop using Finder
1 parent 306a626 commit f501e32

File tree

7 files changed

+59
-134
lines changed

7 files changed

+59
-134
lines changed

TwigCS/src/Command/TwigCSCommand.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,13 @@ protected function configure(): void
3131
->setName('lint')
3232
->setDescription('Lints a template and outputs encountered errors')
3333
->setDefinition([
34-
new InputOption(
35-
'exclude',
36-
'e',
37-
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
38-
'Excludes, based on regex, paths of files and folders from parsing',
39-
['vendor/']
40-
),
4134
new InputOption(
4235
'level',
4336
'l',
4437
InputOption::VALUE_OPTIONAL,
4538
'Allowed values are notice, warning or error',
4639
'notice'
4740
),
48-
new InputOption(
49-
'working-dir',
50-
'd',
51-
InputOption::VALUE_OPTIONAL,
52-
'Run as if this was started in <working-dir> instead of the current working directory',
53-
getcwd()
54-
),
5541
new InputOption(
5642
'fix',
5743
'f',
@@ -78,16 +64,10 @@ protected function configure(): void
7864
protected function execute(InputInterface $input, OutputInterface $output): int
7965
{
8066
$paths = $input->getArgument('paths');
81-
$exclude = $input->getOption('exclude');
8267
$level = $input->getOption('level');
83-
$currentDir = $input->getOption('working-dir');
8468
$fix = $input->getOption('fix');
8569

86-
$config = new Config([
87-
'paths' => $paths,
88-
'exclude' => $exclude,
89-
'workingDirectory' => $currentDir,
90-
]);
70+
$config = new Config($paths);
9171

9272
// Get the rules to apply.
9373
$ruleset = new Ruleset();

TwigCS/src/Config/Config.php

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TwigCS\Config;
66

77
use Exception;
8-
use Symfony\Component\Finder\Finder;
98

109
/**
1110
* TwigCS configuration data.
@@ -15,74 +14,41 @@ class Config
1514
/**
1615
* @var array
1716
*/
18-
public static $defaultConfig = [
19-
'exclude' => [],
20-
'pattern' => '*.twig',
21-
'paths' => [],
22-
'workingDirectory' => '',
23-
];
17+
protected $paths = [];
2418

2519
/**
26-
* @var array
20+
* @param array $paths
2721
*/
28-
protected $config = [];
29-
30-
/**
31-
* @param array $config
32-
*/
33-
public function __construct(array $config = [])
22+
public function __construct(array $paths = [])
3423
{
35-
$this->config = array_merge($this::$defaultConfig, $config);
24+
$this->paths = $paths;
3625
}
3726

3827
/**
39-
* @return Finder
28+
* @return array
4029
*
4130
* @throws Exception
4231
*/
43-
public function findFiles(): Finder
32+
public function findFiles(): array
4433
{
45-
$paths = $this->get('paths');
46-
$exclude = $this->get('exclude');
47-
$workingDir = $this->get('workingDirectory');
48-
49-
// Build the finder.
50-
$files = Finder::create()
51-
->in($workingDir)
52-
->name($this->config['pattern'])
53-
->files();
54-
55-
// Include all matching paths.
56-
foreach ($paths as $path) {
57-
// Trim absolute path
58-
if (substr($path, 0, strlen($workingDir)) === $workingDir) {
59-
$path = ltrim(substr($path, strlen($workingDir)), '/');
34+
$files = [];
35+
foreach ($this->paths as $path) {
36+
if (is_dir($path)) {
37+
$flags = \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS;
38+
$directoryIterator = new \RecursiveDirectoryIterator($path, $flags);
39+
} else {
40+
$directoryIterator = new \RecursiveArrayIterator([$path]);
6041
}
6142

62-
$files->path($path);
63-
}
43+
$filter = new TwigFileFilter($directoryIterator);
44+
$iterator = new \RecursiveIteratorIterator($filter);
6445

65-
// Exclude all matching paths.
66-
if ($exclude) {
67-
$files->exclude($exclude);
46+
/** @var \SplFileInfo $file */
47+
foreach ($iterator as $file) {
48+
$files[] = $file->getRealPath();
49+
}
6850
}
6951

7052
return $files;
7153
}
72-
73-
/**
74-
* @param string $key
75-
*
76-
* @return mixed
77-
*
78-
* @throws Exception
79-
*/
80-
public function get(string $key)
81-
{
82-
if (!isset($this->config[$key])) {
83-
throw new Exception(sprintf('Configuration key "%s" does not exist', $key));
84-
}
85-
86-
return $this->config[$key];
87-
}
8854
}

TwigCS/src/Config/TwigFileFilter.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace TwigCS\Config;
4+
5+
/**
6+
* Class TwigFileFilter
7+
*/
8+
class TwigFileFilter extends \RecursiveFilterIterator
9+
{
10+
/**
11+
* @param \RecursiveIterator $iterator
12+
*/
13+
public function __construct(\RecursiveIterator $iterator)
14+
{
15+
parent::__construct($iterator);
16+
}
17+
18+
/**
19+
* @return bool
20+
*/
21+
public function accept()
22+
{
23+
/** @var \SplFileInfo $file */
24+
$file = $this->current();
25+
26+
return $file->isDir() || 'twig' === $file->getExtension();
27+
}
28+
}

TwigCS/src/Ruleset/Ruleset.php

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

77
use Exception;
88
use SplFileInfo;
9-
use Symfony\Component\Finder\Finder;
109
use TwigCS\Sniff\SniffInterface;
1110

1211
/**
@@ -48,14 +47,16 @@ public function addSniff(SniffInterface $sniff): Ruleset
4847
*/
4948
public function addStandard(string $standardName = 'Generic'): Ruleset
5049
{
51-
try {
52-
$finder = Finder::create()->in(__DIR__.'/'.$standardName)->files();
53-
} catch (Exception $e) {
50+
if (!is_dir(__DIR__.'/'.$standardName)) {
5451
throw new Exception(sprintf('The standard "%s" is not found.', $standardName));
5552
}
5653

54+
$flags = \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS;
55+
$directoryIterator = new \RecursiveDirectoryIterator(__DIR__.'/'.$standardName, $flags);
56+
$iterator = new \RecursiveIteratorIterator($directoryIterator);
57+
5758
/** @var SplFileInfo $file */
58-
foreach ($finder as $file) {
59+
foreach ($iterator as $file) {
5960
$class = __NAMESPACE__.'\\'.$standardName.'\\'.$file->getBasename('.php');
6061

6162
if (class_exists($class)) {

TwigCS/src/Runner/Linter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function __construct(Environment $env, Tokenizer $tokenizer)
4040
}
4141

4242
/**
43-
* @param iterable $files
44-
* @param Ruleset $ruleset
45-
* @param bool $fix
43+
* @param array $files
44+
* @param Ruleset $ruleset
45+
* @param bool $fix
4646
*
4747
* @return Report
4848
*
4949
* @throws Exception
5050
*/
51-
public function run(iterable $files, Ruleset $ruleset, bool $fix = false): Report
51+
public function run(array $files, Ruleset $ruleset, bool $fix = false): Report
5252
{
5353
$report = new Report();
5454

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"php": ">=7.2",
2121
"squizlabs/php_codesniffer": "3.5.*",
2222
"symfony/console": "^3.4 || ^4.3 || ^5.0",
23-
"symfony/finder": "^3.4 || ^4.3 || ^5.0",
2423
"symfony/twig-bridge": "^3.4 || ^4.3 || ^5.0",
2524
"twig/twig": "^2.7 || ^3.0"
2625
},

composer.lock

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

0 commit comments

Comments
 (0)