DirectoryScanner   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFinder() 0 13 3
A findFiles() 0 9 2
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