LocateASTFromFiles::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 5
c 3
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
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