Passed
Push — master ( f58add...4aa999 )
by Luis
54s queued 13s
created

PhpCodeParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.4
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Parser\Code;
9
10
use PhpParser\Node\Stmt;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PhpParser\Parser;
0 ignored issues
show
Bug introduced by
The type PhpParser\Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use PhpParser\ParserFactory;
0 ignored issues
show
Bug introduced by
The type PhpParser\ParserFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use PhUml\Code\Codebase;
14
use PhUml\Parser\Code\Builders\ClassDefinitionBuilder;
15
use PhUml\Parser\Code\Builders\InterfaceDefinitionBuilder;
16
use PhUml\Parser\Code\Builders\TraitDefinitionBuilder;
17
use PhUml\Parser\CodeFinder;
18
19
/**
20
 * It traverses the AST of all the files and interfaces found by the `CodeFinder` and builds a
21
 * `Codebase` object
22
 *
23
 * In order to create the collection of definitions it uses the following visitors
24
 *
25
 * - The `ClassVisitor` which builds `ClassDefinition`s
26
 * - The `InterfaceVisitor` which builds `InterfaceDefinition`s
27
 * - The `TraitVisitor` which builds `TraitDefinition`s
28
 */
29
final class PhpCodeParser
30
{
31
    private Parser $parser;
32
33
    private PhpTraverser $traverser;
34
35
    public function __construct(
36
        ClassDefinitionBuilder $classBuilder = null,
37
        InterfaceDefinitionBuilder $interfaceBuilder = null,
38
        TraitDefinitionBuilder $traitBuilder = null
39
    ) {
40
        $this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
41
        $this->traverser = new PhpTraverser(
42
            $classBuilder ?? new ClassDefinitionBuilder(),
43
            $interfaceBuilder ?? new InterfaceDefinitionBuilder(),
44
            $traitBuilder ?? new TraitDefinitionBuilder()
45
        );
46
    }
47
48
    public function parse(CodeFinder $finder): Codebase
49
    {
50
        foreach ($finder->files() as $code) {
51
            /** @var Stmt[] $nodes Since the parser is run in throw errors mode */
52
            $nodes = $this->parser->parse($code);
53
            $this->traverser->traverse($nodes);
54
        }
55
        return $this->traverser->codebase();
56
    }
57
}
58