Passed
Push — master ( d46d5f...674e23 )
by Luis
44s queued 12s
created

UseStatementsBuilder::fromGroupedUse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 10
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
    public function build(Class_|Interface_|Trait_ $definition): UseStatements
24
    {
25
        $uses = [];
26
27
        $previous = $definition->getAttribute('previous');
28
        while ($previous instanceof Use_ || $previous instanceof GroupUse) {
29
            if ($previous instanceof Use_) {
30
                $uses[] = array_map(fn (UseUse $use): UseStatement => $this->fromUseStatement($use), $previous->uses);
31
            } else {
32
                $prefix = (string) $previous->prefix;
33
                $uses[] = array_map(
34
                    fn (UseUse $use): UseStatement => $this->fromGroupedUse($use, $prefix),
35
                    $previous->uses
36
                );
37
            }
38
            $previous = $previous->getAttribute('previous');
39
        }
40
41
        return new UseStatements(array_merge(...$uses));
42
    }
43
44
    private function fromUseStatement(UseUse $use): UseStatement
45
    {
46
        $alias = null;
47
        if ($use->alias !== null) {
48
            $alias = new Name((string) $use->alias);
49
        }
50
        return new UseStatement(new Name((string) $use->name), $alias);
51
    }
52
53
    private function fromGroupedUse(UseUse $use, string $prefix): UseStatement
54
    {
55
        $alias = null;
56
        if ($use->alias !== null) {
57
            $alias = new Name((string) $use->alias);
58
        }
59
        return new UseStatement(new Name((string) ParsedName::concat($prefix, $use->name)), $alias);
60
    }
61
}
62