Passed
Pull Request — master (#47)
by
unknown
11:29
created

FilesystemFacade::getFilesystemInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 34
rs 9.7333
cc 2
nc 2
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Symfony\Component\Finder\Finder;
9
10
class FilesystemFacade
11
{
12
    public function __construct(
13
        private EventDispatcherInterface $dispatcher,
14
    ) {}
15
16
    /**
17
     * @param array<string> $paths
18
     * @param array<string> $extensions
19
     * @param array<string> $ignore
20
     * @return iterable<Compiler\InputInterface>
21
     */
22
    public function getFilesystemInput(string $cwd, array $paths, array $extensions, array $ignore): iterable
23
    {
24
        $finder = (new Finder())->files()->in($cwd)->ignoreUnreadableDirs();
25
26
        // Set paths to scan
27
28
        $finder->path(
29
            array_map(
30
                fn($path) => '/^' . preg_quote($path, '/') . '/i',
31
                $paths
32
            )
33
        );
34
35
        // Set file extensions
36
37
        $finder->name(
38
            array_map(
39
                fn($extension) => '/\\.' . preg_quote($extension, '/') . '$/i',
40
                $extensions
41
            )
42
        );
43
44
        // Set paths to ignore
45
46
        $finder->notPath(
47
            array_map(
48
                fn($path) => '/^' . preg_quote($path, '/') . '/i',
49
                $ignore
50
            )
51
        );
52
53
        foreach ($finder as $file) {
54
            yield new Compiler\FileInput($file);
55
            $this->dispatcher->dispatch(new Event\FileIncluded($file));
56
        }
57
    }
58
}
59