Skip to content

Commit 88eb947

Browse files
author
Jean-François Lépine
committed
Merge pull request #25 from David-Guillot/feat.excludeSubdirectories
Feat.exclude subdirectories
2 parents 1a6ab37 + a5c374a commit 88eb947

File tree

5 files changed

+72
-19
lines changed

5 files changed

+72
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Vagrantfile
66
provision.sh
77
.vagrant
88
Makefile
9+
/nbproject

build/metrics.phar

1.46 KB
Binary file not shown.

src/Hal/Application/Command/RunMetricsCommand.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,31 @@ protected function configure()
4242
->setName('metrics')
4343
->setDescription('Run analysis')
4444
->addArgument(
45-
'path', InputArgument::REQUIRED, 'Path to explore'
45+
'path', InputArgument::REQUIRED, 'Path to explore'
4646
)
4747
->addOption(
48-
'report-html',null, InputOption::VALUE_REQUIRED, 'Path to save report in HTML format. Example: /tmp/report.html'
48+
'report-html',null, InputOption::VALUE_REQUIRED, 'Path to save report in HTML format. Example: /tmp/report.html'
4949
)
5050
->addOption(
51-
'report-xml', null, InputOption::VALUE_REQUIRED, 'Path to save summary report in XML format. Example: /tmp/report.xml'
51+
'report-xml', null, InputOption::VALUE_REQUIRED, 'Path to save summary report in XML format. Example: /tmp/report.xml'
5252
)
5353
->addOption(
5454
'violations-xml', null, InputOption::VALUE_REQUIRED, 'Path to save violations in XML format. Example: /tmp/report.xml'
5555
)
5656
->addOption(
57-
'report-csv', null, InputOption::VALUE_REQUIRED, 'Path to save summary report in CSV format. Example: /tmp/report.csv'
57+
'report-csv', null, InputOption::VALUE_REQUIRED, 'Path to save summary report in CSV format. Example: /tmp/report.csv'
5858
)
5959
->addOption(
60-
'level', null, InputOption::VALUE_REQUIRED, 'Depth of summary report', 0
60+
'level', null, InputOption::VALUE_REQUIRED, 'Depth of summary report', 0
6161
)
6262
->addOption(
63-
'extensions', null, InputOption::VALUE_REQUIRED, 'Regex of extensions to include', 'php'
63+
'extensions', null, InputOption::VALUE_REQUIRED, 'Regex of extensions to include', 'php'
6464
)
6565
->addOption(
66-
'without-oop', null, InputOption::VALUE_NONE, 'If provided, tool will not extract any informations about OOP model (faster)'
66+
'excludedDirs', null, InputOption::VALUE_REQUIRED, 'Regex of subdirectories to exclude', 'Tests|Features'
67+
)
68+
->addOption(
69+
'without-oop', null, InputOption::VALUE_NONE, 'If provided, tool will not extract any informations about OOP model (faster)'
6770
)
6871
;
6972
}
@@ -80,7 +83,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
8083
$level = $input->getOption('level');
8184

8285
// files
83-
$finder = new Finder($input->getOption('extensions'));
86+
$finder = new Finder(
87+
$input->getOption('extensions'),
88+
$input->getOption('excludedDirs')
89+
);
8490

8591
// rules
8692
$rules = new \Hal\Application\Rule\RuleSet();

src/Hal/Component/File/Finder.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
namespace Hal\Component\File;
1111

12+
use RecursiveDirectoryIterator;
13+
use RecursiveIteratorIterator;
14+
use RegexIterator;
15+
1216
/**
1317
* File finder
1418
*
@@ -18,18 +22,27 @@ class Finder
1822
{
1923

2024
/**
21-
* Extensions to match
25+
* Extensions to match (regex)
2226
*
2327
* @var string
2428
*/
2529
private $extensions;
2630

2731
/**
28-
* @param string $extensions regex
32+
* Subdirectories to exclude (regex)
33+
*
34+
* @var string
35+
*/
36+
private $excludedDirs;
37+
38+
/**
39+
* @param string $extensions regex of file extensions to include
40+
* @param string $excludedDirs regex of directories to exclude
2941
*/
30-
function __construct($extensions = 'php')
42+
function __construct($extensions = 'php', $excludedDirs = '')
3143
{
3244
$this->extensions = (string) $extensions;
45+
$this->excludedDirs = (string) $excludedDirs;
3346
}
3447

3548
/**
@@ -38,19 +51,32 @@ function __construct($extensions = 'php')
3851
* @param string $path
3952
* @return array
4053
*/
41-
public function find($path) {
54+
public function find($path)
55+
{
4256
$files = array();
4357
if(is_dir($path)) {
4458
$path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
45-
$directory = new \RecursiveDirectoryIterator($path);
46-
$iterator = new \RecursiveIteratorIterator($directory);
47-
$regex = new \RegexIterator($iterator, '/^.+\.('. $this->extensions .')$/i', \RecursiveRegexIterator::GET_MATCH);
48-
foreach($regex as $file) {
59+
$directory = new RecursiveDirectoryIterator($path);
60+
$iterator = new RecursiveIteratorIterator($directory);
61+
62+
$filterRegex = sprintf(
63+
'`^%s%s$`',
64+
!empty($this->excludedDirs) ? '((?!'.$this->excludedDirs.').)+' : '.+',
65+
'\.(' . $this->extensions . ')'
66+
);
67+
68+
$filteredIterator = new RegexIterator(
69+
$iterator,
70+
$filterRegex,
71+
\RecursiveRegexIterator::GET_MATCH
72+
);
73+
74+
foreach($filteredIterator as $file) {
4975
$files[] = $file[0];
5076
}
5177
} elseif(is_file($path)) {
5278
$files = array($path);
5379
}
5480
return $files;
5581
}
56-
}
82+
}

tests/Hal/Component/File/FinderTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ class FinderTest extends \PHPUnit_Framework_TestCase {
1010

1111
private $toExplore;
1212

13-
public function setup() {
13+
protected function setup() {
1414
$this->toExplore = sys_get_temp_dir().'/metrics-tmp-finder';
1515
if(!file_exists($this->toExplore)) {
1616
mkdir($this->toExplore);
17+
mkdir($this->toExplore . '/toExclude');
1718
file_put_contents($this->toExplore.'/tmp.php', "<?php echo 'ok';");
1819
file_put_contents($this->toExplore.'/tmp2.php', "<?php echo 'ok';");
1920
file_put_contents($this->toExplore.'/tmp3.php', "<?php echo 'ok';");
2021
file_put_contents($this->toExplore.'/tmp4.txt', "<?php echo 'ok';");
22+
file_put_contents($this->toExplore.'/toExclude/tmp.php', "<?php echo 'test';");
2123
}
2224
}
2325

26+
protected function tearDown()
27+
{
28+
unlink($this->toExplore.'/tmp.php');
29+
unlink($this->toExplore.'/tmp2.php');
30+
unlink($this->toExplore.'/tmp3.php');
31+
unlink($this->toExplore.'/tmp4.txt');
32+
unlink($this->toExplore.'/toExclude/tmp.php');
33+
rmdir($this->toExplore.'/toExclude');
34+
rmdir($this->toExplore);
35+
}
36+
2437
public function testICanFindFilesByExtension() {
2538
$finder = new Finder('txt');
2639
$results = $finder->find($this->toExplore);
@@ -36,6 +49,13 @@ public function testICanGiveFilepathInsteadOfDirectory() {
3649
public function testIFindPhpFilesByDefault() {
3750
$finder = new Finder();
3851
$results = $finder->find($this->toExplore);
52+
$this->assertEquals(4, sizeof($results));
53+
}
54+
55+
public function testIDontFindFilesFromExcludedDirs()
56+
{
57+
$finder = new Finder('php', 'toExclude');
58+
$results = $finder->find($this->toExplore);
3959
$this->assertEquals(3, sizeof($results));
4060
}
41-
}
61+
}

0 commit comments

Comments
 (0)