Passed
Pull Request — 5.0 (#11)
by Luis
11:53 queued 09:06
created

UseStatementsBuilder::build()   A

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
 * 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\Builders;
9
10
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...
11
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...
12
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...
13
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...
14
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...
15
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...
16
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...
17
use PhUml\Code\Name;
18
use PhUml\Code\UseStatement;
19
use PhUml\Code\UseStatements;
20
21
final class UseStatementsBuilder
22
{
23 41
    public function build(Class_|Interface_|Trait_ $definition): UseStatements
24
    {
25 41
        $uses = [];
26
27 41
        $previous = $definition->getAttribute('previous');
28 41
        while ($previous instanceof Use_ || $previous instanceof GroupUse) {
29 23
            if ($previous instanceof Use_) {
30 23
                $uses[] = array_map(fn (UseUse $use): UseStatement => $this->fromUseStatement($use), $previous->uses);
31
            } else {
32 3
                $prefix = (string) $previous->prefix;
33 3
                $uses[] = array_map(
34 3
                    fn (UseUse $use): UseStatement => $this->fromGroupedUse($use, $prefix),
35 3
                    $previous->uses
36
                );
37
            }
38 23
            $previous = $previous->getAttribute('previous');
39
        }
40
41 41
        return new UseStatements(array_merge(...$uses));
42
    }
43
44 23
    private function fromUseStatement(UseUse $use): UseStatement
45
    {
46 23
        $alias = null;
47 23
        if ($use->alias !== null) {
48 2
            $alias = new Name((string) $use->alias);
49
        }
50 23
        return new UseStatement(new Name((string) $use->name), $alias);
51
    }
52
53 3
    private function fromGroupedUse(UseUse $use, string $prefix): UseStatement
54
    {
55 3
        $alias = null;
56 3
        if ($use->alias !== null) {
57 1
            $alias = new Name((string) $use->alias);
58
        }
59 3
        return new UseStatement(new Name((string) ParsedName::concat($prefix, $use->name)), $alias);
60
    }
61
}
62