LocateASTFromFiles::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 2
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace ComposerRequireChecker\ASTLocator;
4
5
use PhpParser\ErrorHandler;
6
use PhpParser\Node\Stmt;
7
use PhpParser\Parser;
8
use RuntimeException;
9
use Traversable;
10
use function sprintf;
11
12
final class LocateASTFromFiles
13
{
14
    /**
15
     * @var Parser
16
     */
17
    private $parser;
18
19
    /**
20
     * @var ErrorHandler
21
     */
22
    private $errorHandler;
23
24 12
    public function __construct(Parser $parser, ?ErrorHandler $errorHandler)
25
    {
26 12
        $this->parser = $parser;
27 12
        $this->errorHandler = $errorHandler;
28 12
    }
29
30
    /**
31
     * @param Traversable<string> $files
32
     *
33
     * @return Traversable<int, array<Stmt>> a series of AST roots, one for each given file
34
     */
35 12
    public function __invoke(Traversable $files): Traversable
36
    {
37 12
        foreach ($files as $file) {
38 11
            $stmts = $this->parser->parse(file_get_contents($file), $this->errorHandler);
39
40 10
            if ($stmts === null) {
41 1
                throw new RuntimeException(sprintf("Parsing the file [%s] resulted in an error.", $file));
42
            }
43
44 9
            yield $stmts;
45
        }
46 10
    }
47
}
48