Skip to content

Commit 56f95cb

Browse files
committed
[#45] Replace File_Iterator with Symfony Finder
[#23] Add command line input option to support includes/excludes
1 parent 567562b commit 56f95cb

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"herrera-io/phar-update": "~2.0",
3232
"nikic/php-parser": "dev-features/php-semver-checker",
3333
"symfony/console": "2.7.*@dev",
34-
"phpunit/php-file-iterator": "~1.3"
34+
"tomzx/finder": "~0.1@dev"
3535
},
3636
"require-dev": {
3737
"mockery/mockery": "~0.9",

src/PHPSemVerChecker/Console/Command/CompareCommand.php

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

33
namespace PHPSemVerChecker\Console\Command;
44

5-
use File_Iterator_Facade;
65
use PHPSemVerChecker\Analyzer\Analyzer;
76
use PHPSemVerChecker\Configuration\Configuration;
87
use PHPSemVerChecker\Configuration\LevelMapping;
98
use PHPSemVerChecker\Filter\SourceFilter;
9+
use PHPSemVerChecker\Finder\Finder;
1010
use PHPSemVerChecker\Reporter\JsonReporter;
1111
use PHPSemVerChecker\Reporter\Reporter;
1212
use PHPSemVerChecker\Scanner\Scanner;
@@ -24,10 +24,14 @@ protected function configure()
2424
->setName('compare')
2525
->setDescription('Compare a set of files to determine what semantic versioning change needs to be done')
2626
->setDefinition([
27-
new InputArgument('source-before', InputArgument::REQUIRED, 'A directory to check'),
28-
new InputArgument('source-after', InputArgument::REQUIRED, 'A directory to check against'),
27+
new InputArgument('source-before', InputArgument::REQUIRED, 'A base directory to check (ex my-test)'),
28+
new InputArgument('source-after', InputArgument::REQUIRED, 'A base directory to check against (ex my-test)'),
29+
new InputOption('include-before', null, InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
30+
new InputOption('include-after', null, InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
31+
new InputOption('exclude-before', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
32+
new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
2933
new InputOption('full-path', null, InputOption::VALUE_NONE, 'Display the full path to the file instead of the relative path'),
30-
new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker'),
34+
new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker'),
3135
new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file')
3236
]);
3337
}
@@ -42,15 +46,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
4246
// Set overrides
4347
LevelMapping::setOverrides($configuration->getLevelMapping());
4448

45-
$fileIterator = new File_Iterator_Facade;
49+
$finder = new Finder();
4650
$scannerBefore = new Scanner();
4751
$scannerAfter = new Scanner();
4852

4953
$sourceBefore = $input->getArgument('source-before');
50-
$sourceBefore = $fileIterator->getFilesAsArray($sourceBefore, '.php');
54+
$includeBefore = $input->getOption('include-before');
55+
$excludeBefore = $input->getOption('exclude-before');
5156

5257
$sourceAfter = $input->getArgument('source-after');
53-
$sourceAfter = $fileIterator->getFilesAsArray($sourceAfter, '.php');
58+
$includeAfter = $input->getOption('include-after');
59+
$excludeAfter = $input->getOption('exclude-after');
60+
61+
$sourceBefore = $finder->findFromString($sourceBefore, $includeBefore, $excludeBefore);
62+
$sourceAfter = $finder->findFromString($sourceAfter, $includeAfter, $excludeAfter);
5463

5564
$sourceFilter = new SourceFilter();
5665
$identicalCount = $sourceFilter->filter($sourceBefore, $sourceAfter);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Finder;
4+
5+
use Finder\Adapter\SymfonyFinder as BaseFinder;
6+
7+
class Finder
8+
{
9+
/**
10+
* @param string $path
11+
* @param array $includes
12+
* @param array $excludes
13+
* @return array
14+
*/
15+
public function find($path, array $includes, array $excludes = [])
16+
{
17+
$finder = new BaseFinder();
18+
$finder->ignoreDotFiles(true)
19+
->files()
20+
->name('*.php')
21+
->in($path);
22+
23+
foreach ($includes as $include) {
24+
$finder->path($include);
25+
}
26+
27+
foreach ($excludes as $exclude) {
28+
$finder->notPath($exclude);
29+
}
30+
31+
$files = iterator_to_array($finder->getIterator());
32+
33+
return $files;
34+
}
35+
36+
/**
37+
* @param string $path
38+
* @param string $includes
39+
* @param string $excludes
40+
* @return array
41+
*/
42+
public function findFromString($path, $includes, $excludes)
43+
{
44+
if ($includes === '*') {
45+
$includes = [];
46+
} else {
47+
$includes = preg_split('@(?:\s*,\s*|^\s*|\s*$)@', $includes, NULL, PREG_SPLIT_NO_EMPTY);
48+
}
49+
50+
if ($excludes === '*') {
51+
$excludes = [];
52+
} else {
53+
$excludes = preg_split('@(?:\s*,\s*|^\s*|\s*$)@', $excludes, NULL, PREG_SPLIT_NO_EMPTY);
54+
}
55+
56+
return $this->find($path, $includes, $excludes);
57+
}
58+
}

0 commit comments

Comments
 (0)