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

ExampleProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 21
c 1
b 0
f 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getExamplesForSuite() 0 31 2
A __construct() 0 5 1
A getStdin() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester;
6
7
use hanneskod\readmetester\Config\Suite;
8
use hanneskod\readmetester\Compiler\Pass\FilterPass;
9
use hanneskod\readmetester\Compiler\StdinInput;
10
use hanneskod\readmetester\Example\ExampleStoreInterface;
11
12
final class ExampleProvider implements ExampleProviderInterface
13
{
14
    private ?StdinInput $stdin = null;
15
16
    public function __construct(
17
        private Compiler\CompilerFactoryFactory $compilerFactoryFactory,
18
        private Compiler\Pass\DefaultCompilerPasses $defaultPasses,
19
        private FilesystemFacade $filesystemFacade,
20
    ) {}
21
22
    public function getExamplesForSuite(Suite $suite): ExampleStoreInterface
23
    {
24
        $prependPasses = [];
25
        $appendPasses = [];
26
27
        // Setup filtering
28
29
        if ($filter = $suite->getFilter()) {
30
            $appendPasses[] = new FilterPass(
31
                new Utils\Regexp('/' . preg_quote($filter, '/') . '/i')
32
            );
33
        }
34
35
        // Create inputs
36
37
        $inputs = match($suite->readFromStdin()) {
38
            true => [$this->getStdin()],
39
            false => $this->filesystemFacade->getFilesystemInput(
40
                '.',
41
                $suite->getIncludePaths(),
42
                $suite->getFileExtensions(),
43
                $suite->getExcludePaths()
44
            ),
45
        };
46
47
        // Create compiler and compile
48
49
        return $this->compilerFactoryFactory
50
            ->createCompilerFactory($suite->getInputLanguage())
51
            ->createCompiler([...$prependPasses, ...$this->defaultPasses->getPasses(), ...$appendPasses])
52
            ->compile($inputs);
53
    }
54
55
    private function getStdin(): StdinInput
56
    {
57
        if (!isset($this->stdin)) {
58
            $this->stdin = new StdinInput();
59
        }
60
61
        return $this->stdin;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->stdin could return the type null which is incompatible with the type-hinted return hanneskod\readmetester\Compiler\StdinInput. Consider adding an additional type-check to rule them out.
Loading history...
62
    }
63
}
64