UseStatementsBuilder::build()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.8666
c 1
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\Builders;
7
8
use PhpParser\Node\Name as ParsedName;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Name 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\Class_;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\Class_ 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\Node\Stmt\Enum_;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\Enum_ 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\Node\Stmt\GroupUse;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\GroupUse 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\Node\Stmt\Interface_;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\Interface_ 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\Node\Stmt\Trait_;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\Trait_ 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\Node\Stmt\Use_;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\Use_ 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 PhpParser\Node\Stmt\UseUse;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\UseUse 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...
16
use PhUml\Code\Name;
17
use PhUml\Code\UseStatement;
18
use PhUml\Code\UseStatements;
19
20
final class UseStatementsBuilder
21
{
22 43
    public function build(Class_|Interface_|Trait_|Enum_ $definition): UseStatements
23
    {
24 43
        $uses = [];
25
26 43
        $previous = $definition->getAttribute('previous');
27 43
        while ($previous instanceof Use_ || $previous instanceof GroupUse) {
28 24
            if ($previous instanceof Use_) {
29 24
                $uses[] = array_map(fn (UseUse $use): UseStatement => $this->fromUseStatement($use), $previous->uses);
30
            } else {
31 3
                $prefix = (string) $previous->prefix;
32 3
                $uses[] = array_map(
33 3
                    fn (UseUse $use): UseStatement => $this->fromGroupedUse($use, $prefix),
34 3
                    $previous->uses
35
                );
36
            }
37 24
            $previous = $previous->getAttribute('previous');
38
        }
39
40 43
        return new UseStatements(array_merge(...$uses));
41
    }
42
43 24
    private function fromUseStatement(UseUse $use): UseStatement
44
    {
45 24
        $alias = null;
46 24
        if ($use->alias !== null) {
47 2
            $alias = new Name((string) $use->alias);
48
        }
49 24
        return new UseStatement(new Name((string) $use->name), $alias);
50
    }
51
52 3
    private function fromGroupedUse(UseUse $use, string $prefix): UseStatement
53
    {
54 3
        $alias = null;
55 3
        if ($use->alias !== null) {
56 1
            $alias = new Name((string) $use->alias);
57
        }
58 3
        return new UseStatement(new Name((string) ParsedName::concat($prefix, $use->name)), $alias);
59
    }
60
}
61