Skip to content

📦 Stop using Finder #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions TwigCS/src/Command/TwigCSCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,13 @@ protected function configure(): void
->setName('lint')
->setDescription('Lints a template and outputs encountered errors')
->setDefinition([
new InputOption(
'exclude',
'e',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Excludes, based on regex, paths of files and folders from parsing',
['vendor/']
),
new InputOption(
'level',
'l',
InputOption::VALUE_OPTIONAL,
'Allowed values are notice, warning or error',
'notice'
),
new InputOption(
'working-dir',
'd',
InputOption::VALUE_OPTIONAL,
'Run as if this was started in <working-dir> instead of the current working directory',
getcwd()
),
new InputOption(
'fix',
'f',
Expand All @@ -78,16 +64,10 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$paths = $input->getArgument('paths');
$exclude = $input->getOption('exclude');
$level = $input->getOption('level');
$currentDir = $input->getOption('working-dir');
$fix = $input->getOption('fix');

$config = new Config([
'paths' => $paths,
'exclude' => $exclude,
'workingDirectory' => $currentDir,
]);
$config = new Config($paths);

// Get the rules to apply.
$ruleset = new Ruleset();
Expand Down
72 changes: 19 additions & 53 deletions TwigCS/src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace TwigCS\Config;

use Exception;
use Symfony\Component\Finder\Finder;

/**
* TwigCS configuration data.
Expand All @@ -15,74 +14,41 @@ class Config
/**
* @var array
*/
public static $defaultConfig = [
'exclude' => [],
'pattern' => '*.twig',
'paths' => [],
'workingDirectory' => '',
];
protected $paths = [];

/**
* @var array
* @param array $paths
*/
protected $config = [];

/**
* @param array $config
*/
public function __construct(array $config = [])
public function __construct(array $paths = [])
{
$this->config = array_merge($this::$defaultConfig, $config);
$this->paths = $paths;
}

/**
* @return Finder
* @return array
*
* @throws Exception
*/
public function findFiles(): Finder
public function findFiles(): array
{
$paths = $this->get('paths');
$exclude = $this->get('exclude');
$workingDir = $this->get('workingDirectory');

// Build the finder.
$files = Finder::create()
->in($workingDir)
->name($this->config['pattern'])
->files();

// Include all matching paths.
foreach ($paths as $path) {
// Trim absolute path
if (substr($path, 0, strlen($workingDir)) === $workingDir) {
$path = ltrim(substr($path, strlen($workingDir)), '/');
$files = [];
foreach ($this->paths as $path) {
if (is_dir($path)) {
$flags = \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS;
$directoryIterator = new \RecursiveDirectoryIterator($path, $flags);
} else {
$directoryIterator = new \RecursiveArrayIterator([$path]);
}

$files->path($path);
}
$filter = new TwigFileFilter($directoryIterator);
$iterator = new \RecursiveIteratorIterator($filter);

// Exclude all matching paths.
if ($exclude) {
$files->exclude($exclude);
/** @var \SplFileInfo $file */
foreach ($iterator as $file) {
$files[] = $file->getRealPath();
}
}

return $files;
}

/**
* @param string $key
*
* @return mixed
*
* @throws Exception
*/
public function get(string $key)
{
if (!isset($this->config[$key])) {
throw new Exception(sprintf('Configuration key "%s" does not exist', $key));
}

return $this->config[$key];
}
}
28 changes: 28 additions & 0 deletions TwigCS/src/Config/TwigFileFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace TwigCS\Config;

/**
* Class TwigFileFilter
*/
class TwigFileFilter extends \RecursiveFilterIterator
{
/**
* @param \RecursiveIterator $iterator
*/
public function __construct(\RecursiveIterator $iterator)
{
parent::__construct($iterator);
}

/**
* @return bool
*/
public function accept()
{
/** @var \SplFileInfo $file */
$file = $this->current();

return $file->isDir() || 'twig' === $file->getExtension();
}
}
11 changes: 6 additions & 5 deletions TwigCS/src/Ruleset/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Exception;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use TwigCS\Sniff\SniffInterface;

/**
Expand Down Expand Up @@ -48,14 +47,16 @@ public function addSniff(SniffInterface $sniff): Ruleset
*/
public function addStandard(string $standardName = 'Generic'): Ruleset
{
try {
$finder = Finder::create()->in(__DIR__.'/'.$standardName)->files();
} catch (Exception $e) {
if (!is_dir(__DIR__.'/'.$standardName)) {
throw new Exception(sprintf('The standard "%s" is not found.', $standardName));
}

$flags = \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS;
$directoryIterator = new \RecursiveDirectoryIterator(__DIR__.'/'.$standardName, $flags);
$iterator = new \RecursiveIteratorIterator($directoryIterator);

/** @var SplFileInfo $file */
foreach ($finder as $file) {
foreach ($iterator as $file) {
$class = __NAMESPACE__.'\\'.$standardName.'\\'.$file->getBasename('.php');

if (class_exists($class)) {
Expand Down
8 changes: 4 additions & 4 deletions TwigCS/src/Runner/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function __construct(Environment $env, Tokenizer $tokenizer)
}

/**
* @param iterable $files
* @param Ruleset $ruleset
* @param bool $fix
* @param array $files
* @param Ruleset $ruleset
* @param bool $fix
*
* @return Report
*
* @throws Exception
*/
public function run(iterable $files, Ruleset $ruleset, bool $fix = false): Report
public function run(array $files, Ruleset $ruleset, bool $fix = false): Report
{
$report = new Report();

Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"php": ">=7.2",
"squizlabs/php_codesniffer": "3.5.*",
"symfony/console": "^3.4 || ^4.3 || ^5.0",
"symfony/finder": "^3.4 || ^4.3 || ^5.0",
"symfony/twig-bridge": "^3.4 || ^4.3 || ^5.0",
"twig/twig": "^2.7 || ^3.0"
},
Expand Down
51 changes: 1 addition & 50 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.