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

FilesystemFacade   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 17
c 0
b 0
f 0
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilesystemInput() 0 34 2
A __construct() 0 3 1
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