Passed
Push — union-types ( b56600 )
by Luis
14:03
created

CodeParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 7 1
A fromConfiguration() 0 7 2
A __construct() 0 4 1
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;
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
    private PhpCodeParser $parser;
27
28
    private RelationshipsResolvers $resolvers;
29
30
    public static function fromConfiguration(CodeParserConfiguration $configuration): CodeParser
31
    {
32
        $resolvers = $configuration->extractAssociations()
33
            ? RelationshipsResolvers::withAssociations()
34
            : RelationshipsResolvers::withoutAssociations();
35
36
        return new CodeParser(PhpCodeParser::fromConfiguration($configuration), $resolvers);
37
    }
38
39
    private function __construct(PhpCodeParser $parser, RelationshipsResolvers $resolvers)
40
    {
41
        $this->parser = $parser;
42
        $this->resolvers = $resolvers;
43
    }
44
45
    /**
46
     * The parsing process is as follows
47
     *
48
     * 1. Parse the code and populate the `Codebase` with definitions
49
     * 2. Add external definitions (built-in/third party), if needed
50
     */
51
    public function parse(SourceCode $sourceCode): Codebase
52
    {
53
        $codebase = $this->parser->parse($sourceCode);
54
55
        $this->resolvers->addExternalDefinitionsTo($codebase);
56
57
        return $codebase;
58
    }
59
}
60