Passed
Push — 5.0 ( d46d5f )
by Luis
39s queued 12s
created

PhpCodeParser::fromConfiguration()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 30
c 0
b 0
f 0
nc 16
nop 1
dl 0
loc 39
rs 9.1288
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\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\Filters\PrivateVisibilityFilter;
16
use PhUml\Parser\Code\Builders\Filters\ProtectedVisibilityFilter;
17
use PhUml\Parser\Code\Builders\InterfaceDefinitionBuilder;
18
use PhUml\Parser\Code\Builders\Members\FilteredAttributesBuilder;
19
use PhUml\Parser\Code\Builders\Members\FilteredConstantsBuilder;
20
use PhUml\Parser\Code\Builders\Members\FilteredMethodsBuilder;
21
use PhUml\Parser\Code\Builders\Members\NoAttributesBuilder;
22
use PhUml\Parser\Code\Builders\Members\NoConstantsBuilder;
23
use PhUml\Parser\Code\Builders\Members\NoMethodsBuilder;
24
use PhUml\Parser\Code\Builders\Members\ParametersBuilder;
25
use PhUml\Parser\Code\Builders\Members\TypeBuilder;
26
use PhUml\Parser\Code\Builders\Members\VisibilityBuilder;
27
use PhUml\Parser\Code\Builders\Members\VisibilityFilters;
28
use PhUml\Parser\Code\Builders\MembersBuilder;
29
use PhUml\Parser\Code\Builders\TraitDefinitionBuilder;
30
use PhUml\Parser\CodeParserConfiguration;
31
use PhUml\Parser\SourceCode;
32
33
/**
34
 * It traverses the AST of all the files and interfaces found by the `CodeFinder` and builds a
35
 * `Codebase` object
36
 *
37
 * In order to create the collection of definitions it uses the following visitors
38
 *
39
 * - The `ClassVisitor` which builds `ClassDefinition`s
40
 * - The `InterfaceVisitor` which builds `InterfaceDefinition`s
41
 * - The `TraitVisitor` which builds `TraitDefinition`s
42
 */
43
final class PhpCodeParser
44
{
45
    public static function fromConfiguration(CodeParserConfiguration $configuration): PhpCodeParser
46
    {
47
        if ($configuration->hideAttributes()) {
48
            $constantsBuilder = new NoConstantsBuilder();
49
            $attributesBuilder = new NoAttributesBuilder();
50
        }
51
        if ($configuration->hideMethods()) {
52
            $methodsBuilder = new NoMethodsBuilder();
53
        }
54
        $filters = [];
55
        if ($configuration->hidePrivate()) {
56
            $filters[] = new PrivateVisibilityFilter();
57
        }
58
        if ($configuration->hideProtected()) {
59
            $filters[] = new ProtectedVisibilityFilter();
60
        }
61
        $visibilityBuilder = new VisibilityBuilder();
62
        $typeBuilder = new TypeBuilder();
63
        $filters = new VisibilityFilters($filters);
64
        $methodsBuilder ??= new FilteredMethodsBuilder(
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $methodsBuilder does not seem to be defined for all execution paths leading up to this point.
Loading history...
65
            new ParametersBuilder($typeBuilder),
66
            $typeBuilder,
67
            $visibilityBuilder,
68
            $filters
69
        );
70
        $constantsBuilder ??= new FilteredConstantsBuilder($visibilityBuilder, $filters);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $constantsBuilder does not seem to be defined for all execution paths leading up to this point.
Loading history...
71
        $attributesBuilder ??= new FilteredAttributesBuilder(
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attributesBuilder does not seem to be defined for all execution paths leading up to this point.
Loading history...
72
            $visibilityBuilder,
73
            $typeBuilder,
74
            $filters
75
        );
76
        $membersBuilder = new MembersBuilder($constantsBuilder, $attributesBuilder, $methodsBuilder);
77
78
        return new self(
79
            (new ParserFactory())->create(ParserFactory::PREFER_PHP7),
80
            new PhpTraverser(
81
                new ClassDefinitionBuilder($membersBuilder),
82
                new InterfaceDefinitionBuilder($membersBuilder),
83
                new TraitDefinitionBuilder($membersBuilder)
84
            )
85
        );
86
    }
87
88
    private function __construct(
89
        private Parser $parser,
90
        private PhpTraverser $traverser,
91
    ) {
92
    }
93
94
    public function parse(SourceCode $sourceCode): Codebase
95
    {
96
        foreach ($sourceCode->fileContents() as $code) {
97
            /** @var Stmt[] $nodes Since the parser is run in throw errors mode */
98
            $nodes = $this->parser->parse($code);
99
            $this->traverser->traverse($nodes);
100
        }
101
        return $this->traverser->codebase();
102
    }
103
}
104