DirectoryScanner::getFinder()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Cli\FileSystem;
6
7
use Symfony\Component\Finder\Finder;
8
9
class DirectoryScanner
10
{
11 4
    public function getFinder(string $path, ?array $validExtensions = null): Finder
12
    {
13 4
        $finder = (new Finder())->files()
14 4
            ->in($path)
15 4
            ->ignoreUnreadableDirs();
16 4
        if ($validExtensions !== null && count($validExtensions) > 0) {
17 4
            $finder->name(sprintf(
18 4
                '/\.(%s)$/i',
19 4
                implode('|', $validExtensions)
20
            ));
21
        }
22
23 4
        return $finder;
24
    }
25
26
    /**
27
     * @psalm-suppress PossiblyInvalidArgument
28
     *
29 4
     * @return array<\SplFileInfo>
30
     */
31 4
    public function findFiles(string $path, ?array $validExtensions = null, bool $recursive = true): array
32
    {
33 4
        $finder = $this->getFinder($path, $validExtensions);
34 3
35
        if (!$recursive) {
36
            $finder->depth('== 0');
37 4
        }
38
39
        return iterator_to_array($finder->getIterator());
40
    }
41
}
42