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

TypeBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
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\Members;
9
10
use PhpParser\Comment\Doc;
0 ignored issues
show
Bug introduced by
The type PhpParser\Comment\Doc 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\Identifier;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Identifier 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\Name;
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...
13
use PhpParser\Node\NullableType;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\NullableType 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\UnionType;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\UnionType 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\UseStatements;
16
use PhUml\Code\Variables\TypeDeclaration;
17
use PhUml\Parser\Code\TypeResolver;
18
19
final class TypeBuilder
20
{
21
    public function __construct(private TypeResolver $typeResolver)
22
    {
23
    }
24
25
    public function fromMethodParameter(
26
        Identifier|Name|NullableType|UnionType|null $type,
0 ignored issues
show
Bug introduced by
The type PhUml\Parser\Code\Builders\Members\null 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...
27
        ?Doc $docBlock,
28
        string $name,
29
        UseStatements $useStatements
30
    ): TypeDeclaration {
31
        $methodComment = $docBlock?->getText();
32
        if ($type === null) {
33
            return $this->typeResolver->resolveForParameter($methodComment, $name, $useStatements);
34
        }
35
36
        $typeDeclaration = $this->fromParsedType($type);
37
        if (! $typeDeclaration->isBuiltInArray()) {
38
            return $typeDeclaration;
39
        }
40
41
        $typeFromDocBlock = $this->typeResolver->resolveForParameter($methodComment, $name, $useStatements);
42
43
        return $typeFromDocBlock->isPresent() ? $typeFromDocBlock : $typeDeclaration;
44
    }
45
46
    public function fromMethodReturnType(
47
        Identifier|Name|NullableType|UnionType|null $type,
48
        ?Doc $docBlock,
49
        UseStatements $useStatements
50
    ): TypeDeclaration {
51
        $methodComment = $docBlock?->getText();
52
        if ($type === null) {
53
            return $this->typeResolver->resolveForReturn($methodComment, $useStatements);
54
        }
55
56
        $typeDeclaration = $this->fromParsedType($type);
57
        if (! $typeDeclaration->isBuiltInArray()) {
58
            return $typeDeclaration;
59
        }
60
61
        $typeFromDocBlock = $this->typeResolver->resolveForReturn($methodComment, $useStatements);
62
63
        return $typeFromDocBlock->isPresent() ? $typeFromDocBlock : $typeDeclaration;
64
    }
65
66
    public function fromAttributeType(
67
        Identifier|Name|NullableType|UnionType|null $type,
68
        ?Doc $docBlock,
69
        UseStatements $useStatements
70
    ): TypeDeclaration {
71
        $attributeComment = $docBlock?->getText();
72
        if ($type === null) {
73
            return $this->typeResolver->resolveForAttribute($attributeComment, $useStatements);
74
        }
75
76
        $typeDeclaration = $this->fromParsedType($type);
77
        if (! $typeDeclaration->isBuiltInArray()) {
78
            return $typeDeclaration;
79
        }
80
81
        $typeFromDocBlock = $this->typeResolver->resolveForAttribute($attributeComment, $useStatements);
82
83
        return $typeFromDocBlock->isPresent() ? $typeFromDocBlock : $typeDeclaration;
84
    }
85
86
    private function fromParsedType(Identifier|Name|NullableType|UnionType|null $type): TypeDeclaration
87
    {
88
        return match (true) {
89
            $type instanceof NullableType => TypeDeclaration::fromNullable((string) $type->type),
90
            $type instanceof Name, $type instanceof Identifier => TypeDeclaration::from((string) $type),
91
            $type === null => TypeDeclaration::absent(),
92
            default => TypeDeclaration::fromUnionType($this->fromUnionType($type)),
93
        };
94
    }
95
96
    /** @return string[] */
97
    private function fromUnionType(UnionType $type): array
98
    {
99
        return array_map(
100
            static fn (Identifier|Name $name): string => (string) $name,
101
            $type->types
102
        );
103
    }
104
}
105