Passed
Push — master ( 1ba30a...8ac47c )
by Luis
01:05 queued 12s
created

PhpCodeParser::fromConfiguration()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 47
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 37
nc 16
nop 1
dl 0
loc 47
ccs 36
cts 36
cp 1
crap 5
rs 9.0168
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Parser\Code;
7
8
use phpDocumentor\Reflection\DocBlockFactory;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\DocBlockFactory 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...
9
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...
10
use PhpParser\NodeTraverser;
0 ignored issues
show
Bug introduced by
The type PhpParser\NodeTraverser 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\NodeVisitor\NameResolver;
0 ignored issues
show
Bug introduced by
The type PhpParser\NodeVisitor\NameResolver 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\NodeVisitor\NodeConnectingVisitor;
0 ignored issues
show
Bug introduced by
The type PhpParser\NodeVisitor\NodeConnectingVisitor 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 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...
14
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...
15
use PhUml\Code\Codebase;
16
use PhUml\Parser\Code\Builders\AttributeAnalyzer;
17
use PhUml\Parser\Code\Builders\ClassDefinitionBuilder;
18
use PhUml\Parser\Code\Builders\EnumDefinitionBuilder;
19
use PhUml\Parser\Code\Builders\Filters\PrivateVisibilityFilter;
20
use PhUml\Parser\Code\Builders\Filters\ProtectedVisibilityFilter;
21
use PhUml\Parser\Code\Builders\InterfaceDefinitionBuilder;
22
use PhUml\Parser\Code\Builders\Members\NoConstantsBuilder;
23
use PhUml\Parser\Code\Builders\Members\NoMethodsBuilder;
24
use PhUml\Parser\Code\Builders\Members\NoPropertiesBuilder;
25
use PhUml\Parser\Code\Builders\Members\ParametersBuilder;
26
use PhUml\Parser\Code\Builders\Members\ParsedConstantsBuilder;
27
use PhUml\Parser\Code\Builders\Members\ParsedMethodsBuilder;
28
use PhUml\Parser\Code\Builders\Members\ParsedPropertiesBuilder;
29
use PhUml\Parser\Code\Builders\Members\TypeBuilder;
0 ignored issues
show
Bug introduced by
The type PhUml\Parser\Code\Builders\Members\TypeBuilder 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...
30
use PhUml\Parser\Code\Builders\Members\VisibilityBuilder;
31
use PhUml\Parser\Code\Builders\Members\VisibilityFilters;
32
use PhUml\Parser\Code\Builders\MembersBuilder;
33
use PhUml\Parser\Code\Builders\ParameterTagFilterFactory;
34
use PhUml\Parser\Code\Builders\TagTypeFactory;
0 ignored issues
show
Bug introduced by
The type PhUml\Parser\Code\Builders\TagTypeFactory 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...
35
use PhUml\Parser\Code\Builders\TraitDefinitionBuilder;
36
use PhUml\Parser\Code\Builders\UseStatementsBuilder;
37
use PhUml\Parser\Code\Visitors\ClassVisitor;
38
use PhUml\Parser\Code\Visitors\EnumVisitor;
39
use PhUml\Parser\Code\Visitors\InterfaceVisitor;
40
use PhUml\Parser\Code\Visitors\TraitVisitor;
41
use PhUml\Parser\CodeParserConfiguration;
42
use PhUml\Parser\SourceCode;
43
44
/**
45
 * It traverses the AST of all the files and interfaces found by the `CodeFinder` and builds a
46
 * `Codebase` object
47
 *
48
 * In order to create the collection of definitions it uses the following visitors
49
 *
50
 * - The `ClassVisitor` which builds `ClassDefinition`s
51
 * - The `InterfaceVisitor` which builds `InterfaceDefinition`s
52
 * - The `TraitVisitor` which builds `TraitDefinition`s
53
 */
54
final class PhpCodeParser
55
{
56 38
    public static function fromConfiguration(CodeParserConfiguration $configuration): PhpCodeParser
57
    {
58 38
        if ($configuration->hideAttributes()) {
59 6
            $constantsBuilder = new NoConstantsBuilder();
60 6
            $propertiesBuilder = new NoPropertiesBuilder();
61
        }
62 38
        if ($configuration->hideMethods()) {
63 6
            $methodsBuilder = new NoMethodsBuilder();
64
        }
65 38
        $filters = [];
66 38
        if ($configuration->hidePrivate()) {
67 5
            $filters[] = new PrivateVisibilityFilter();
68
        }
69 38
        if ($configuration->hideProtected()) {
70 5
            $filters[] = new ProtectedVisibilityFilter();
71
        }
72 38
        $visibilityBuilder = new VisibilityBuilder();
73 38
        $typeBuilder = new TypeBuilder(new TypeResolver(new TagTypeFactory(DocBlockFactory::createInstance())));
74 38
        $parameterFilter = new ParameterTagFilterFactory();
75 38
        $methodsBuilder ??= new ParsedMethodsBuilder(
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...
76 32
            new ParametersBuilder($typeBuilder, $parameterFilter),
77
            $typeBuilder,
78
            $visibilityBuilder,
79
        );
80 38
        $constantsBuilder ??= new ParsedConstantsBuilder($visibilityBuilder);
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...
81 38
        $propertiesBuilder ??= new ParsedPropertiesBuilder($visibilityBuilder, $typeBuilder, $parameterFilter);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $propertiesBuilder does not seem to be defined for all execution paths leading up to this point.
Loading history...
82 38
        $filters = new VisibilityFilters($filters);
83 38
        $membersBuilder = new MembersBuilder($constantsBuilder, $propertiesBuilder, $methodsBuilder, $filters);
84 38
        $useStatementsBuilder = new UseStatementsBuilder();
85 38
        $classBuilder = new ClassDefinitionBuilder($membersBuilder, $useStatementsBuilder, new AttributeAnalyzer());
86 38
        $interfaceBuilder = new InterfaceDefinitionBuilder($membersBuilder, $useStatementsBuilder);
87 38
        $traitBuilder = new TraitDefinitionBuilder($membersBuilder, $useStatementsBuilder);
88 38
        $enumBuilder = new EnumDefinitionBuilder($membersBuilder, $useStatementsBuilder);
89
90 38
        $codebase = new Codebase();
91
92 38
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
93 38
        $traverser = new NodeTraverser();
94 38
        $traverser->addVisitor(new NameResolver());
95 38
        $traverser->addVisitor(new NodeConnectingVisitor());
96 38
        $traverser->addVisitor(new ClassVisitor($classBuilder, $codebase));
97 38
        $traverser->addVisitor(new InterfaceVisitor($interfaceBuilder, $codebase));
98 38
        $traverser->addVisitor(new TraitVisitor($traitBuilder, $codebase));
99 38
        $traverser->addVisitor(new EnumVisitor($enumBuilder, $codebase));
100 38
        $traverser = new PhpTraverser($traverser, $codebase);
101
102 38
        return new self($parser, $traverser);
103
    }
104
105 38
    private function __construct(
106
        private readonly Parser $parser,
107
        private readonly PhpTraverser $traverser,
108
    ) {
109
    }
110
111 32
    public function parse(SourceCode $sourceCode): Codebase
112
    {
113 32
        foreach ($sourceCode->fileContents() as $code) {
114
            /** @var Stmt[] $nodes Since the parser is configured to throw in case of errors */
115 30
            $nodes = $this->parser->parse($code);
116 30
            $this->traverser->traverse($nodes);
117
        }
118 32
        return $this->traverser->codebase();
119
    }
120
}
121