Passed
Push — master ( aade94...d46d5f )
by Luis
50s queued 12s
created

CodeParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.0
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;
9
10
use PhUml\Code\Codebase;
11
use PhUml\Parser\Code\PhpCodeParser;
12
use PhUml\Parser\Code\RelationshipsResolvers;
13
14
/**
15
 * It takes the files found by the `CodeFinder` and turns them into a `Codebase`
16
 *
17
 * A `Codebase` is a collection of `Definition`s (classes, interfaces and traits)
18
 *
19
 * It will call the `ExternalDefinitionsResolver` to add generic `Definition`s for classes,
20
 * interfaces and traits that do not belong directly to the current codebase
21
 *
22
 * These external definitions are either built-in or from third party libraries
23
 */
24
final class CodeParser
25
{
26
    public static function fromConfiguration(CodeParserConfiguration $configuration): CodeParser
27
    {
28
        $resolvers = $configuration->extractAssociations()
29
            ? RelationshipsResolvers::withAssociations()
30
            : RelationshipsResolvers::withoutAssociations();
31
32
        return new CodeParser(PhpCodeParser::fromConfiguration($configuration), $resolvers);
33 111
    }
34
35 111
    private function __construct(private PhpCodeParser $parser, private RelationshipsResolvers $resolvers)
36 111
    {
37 111
    }
38
39
    /**
40
     * The parsing process is as follows
41
     *
42
     * 1. Parse the code and populate the `Codebase` with definitions
43
     * 2. Add external definitions (built-in/third party), if needed
44
     */
45 93
    public function parse(SourceCode $sourceCode): Codebase
46
    {
47 93
        $codebase = $this->parser->parse($sourceCode);
48 93
49
        $this->resolvers->addExternalDefinitionsTo($codebase);
50 93
51
        return $codebase;
52
    }
53
}
54