Skip to content

Commit

Permalink
Merge pull request #228 from sensiolabs-de/improve-file-exclusion
Browse files Browse the repository at this point in the history
improve file exclusion
  • Loading branch information
Simon Mönch authored Jun 11, 2019
2 parents 564d7cd + fe4cff1 commit 6747725
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/FileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,31 @@
namespace SensioLabs\Deptrac;

use SensioLabs\Deptrac\Configuration\Configuration;
use SplFileInfo;
use Symfony\Component\Finder\Finder;

class FileResolver
{
/**
* @throws \InvalidArgumentException
*
* @return \SplFileInfo[]
* @return SplFileInfo[]
*/
public function resolve(Configuration $configuration): array
{
$files = iterator_to_array(
(new Finder())
->in($configuration->getPaths())
->name('*.php')
->files()
->followLinks()
->ignoreUnreadableDirs(true)
->ignoreVCS(true)
);
$finder = (new Finder())
->in($configuration->getPaths())
->name('*.php')
->files()
->followLinks()
->ignoreUnreadableDirs(true)
->ignoreVCS(true)
->notPath($configuration->getExcludeFiles());

return array_filter($files, function (\SplFileInfo $fileInfo) use ($configuration) {
foreach ($configuration->getExcludeFiles() as $excludeFiles) {
if (preg_match('/'.$excludeFiles.'/i', $fileInfo->getPathname())) {
return false;
}
}
$finder = new PathNameFilterIterator(
$finder->getIterator(),
[],
$configuration->getExcludeFiles()
);

return true;
});
return iterator_to_array($finder);
}
}
21 changes: 21 additions & 0 deletions src/PathNameFilterIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace SensioLabs\Deptrac;

use Symfony\Component\Finder\Iterator\PathFilterIterator;

class PathNameFilterIterator extends PathFilterIterator
{
public function accept(): bool
{
$filename = $this->current()->getPathname();

if ('\\' === \DIRECTORY_SEPARATOR) {
$filename = str_replace('\\', '/', $filename);
}

return $this->isAccepted($filename);
}
}
77 changes: 77 additions & 0 deletions tests/PathNameFilterIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Tests\SensioLabs\Deptrac;

use ArrayIterator;
use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\PathNameFilterIterator;
use SplFileInfo;

class PathNameFilterIteratorTest extends TestCase
{
/**
* @dataProvider getTestFilterData
*/
public function testFilter(\Iterator $inner, array $matchPatterns, array $noMatchPatterns, array $resultArray): void
{
$iterator = new PathNameFilterIterator($inner, $matchPatterns, $noMatchPatterns);

$values = array_map(
static function (\SplFileInfo $fileInfo) {
return str_replace('/', \DIRECTORY_SEPARATOR, $fileInfo->getPathname());
},
iterator_to_array($iterator, false)
);

sort($values);
sort($resultArray);

static::assertEquals($resultArray, array_values($values));
}

public function getTestFilterData(): array
{
$inner = new ArrayIterator();

//PATH: A/B/C/abc.dat
$inner[] = new SplFileInfo(
'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat'
);

//PATH: A/B/ab.dat
$inner[] = new SplFileInfo('A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat');

//PATH: A/a.dat
$inner[] = new SplFileInfo('A'.\DIRECTORY_SEPARATOR.'a.dat');

//PATH: copy/A/B/C/abc.dat.copy
$inner[] = new SplFileInfo(
'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'C'.\DIRECTORY_SEPARATOR.'abc.dat.copy'
);

//PATH: copy/A/B/ab.dat.copy
$inner[] = new SplFileInfo(
'copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'B'.\DIRECTORY_SEPARATOR.'ab.dat.copy'
);

//PATH: copy/A/a.dat.copy
$inner[] = new SplFileInfo('copy'.\DIRECTORY_SEPARATOR.'A'.\DIRECTORY_SEPARATOR.'a.dat.copy');

return [
[$inner, ['/^A/'], [], ['A/B/C/abc.dat', 'A/B/ab.dat', 'A/a.dat']],
[$inner, ['/^A\/B/'], [], ['A/B/C/abc.dat', 'A/B/ab.dat']],
[$inner, ['/^A\/B\/C/'], [], ['A/B/C/abc.dat']],
[$inner, ['/A\/B\/C/'], [], ['A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy']],

[$inner, ['A'], [], ['A/B/C/abc.dat', 'A/B/ab.dat', 'A/a.dat', 'copy/A/B/C/abc.dat.copy', 'copy/A/B/ab.dat.copy', 'copy/A/a.dat.copy']],
[$inner, ['A/B'], [], ['A/B/C/abc.dat', 'A/B/ab.dat', 'copy/A/B/C/abc.dat.copy', 'copy/A/B/ab.dat.copy']],
[$inner, ['A/B/C'], [], ['A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy']],

[$inner, ['copy/A'], [], ['copy/A/B/C/abc.dat.copy', 'copy/A/B/ab.dat.copy', 'copy/A/a.dat.copy']],
[$inner, ['copy/A/B'], [], ['copy/A/B/C/abc.dat.copy', 'copy/A/B/ab.dat.copy']],
[$inner, ['copy/A/B/C'], [], ['copy/A/B/C/abc.dat.copy']],
];
}
}

0 comments on commit 6747725

Please sign in to comment.