Passed
Push — union-types ( cd7236...b93e9c )
by Luis
10:52
created

TypeBuilder::fromUnionType()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
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\Name as NodeName;
14
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...
15
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...
16
use PhUml\Code\Attributes\AttributeDocBlock;
17
use PhUml\Code\Methods\MethodDocBlock;
18
use PhUml\Code\Variables\TypeDeclaration;
19
20
final class TypeBuilder
21
{
22
    public function fromMethodParameter(
23
        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...
24
        ?Doc $docBlock,
25
        string $name
26
    ): TypeDeclaration {
27
        $methodDocBlock = new MethodDocBlock($docBlock === null ? null : $docBlock->getText());
28
        if ($type === null) {
29
            return $methodDocBlock->typeOfParameter($name);
30
        }
31
32
        $typeDeclaration = $this->fromParsedType($type);
33
        if (! $typeDeclaration->isBuiltInArray()) {
34
            return $typeDeclaration;
35
        }
36
        if (! $methodDocBlock->hasTypeOfParameter($name)) {
37
            return $typeDeclaration;
38
        }
39
        return $methodDocBlock->typeOfParameter($name);
40
    }
41
42
    public function fromMethodReturnType(
43
        Identifier|Name|NullableType|UnionType|null $type,
44
        ?Doc $docBlock
45
    ): TypeDeclaration {
46
        $methodDocBlock = new MethodDocBlock($docBlock === null ? null : $docBlock->getText());
47
        if ($type === null) {
48
            return $methodDocBlock->returnType();
49
        }
50
51
        $typeDeclaration = $this->fromParsedType($type);
52
        if (! $typeDeclaration->isBuiltInArray()) {
53
            return $typeDeclaration;
54
        }
55
        if (! $methodDocBlock->hasReturnType()) {
56
            return $typeDeclaration;
57
        }
58
        return $methodDocBlock->returnType();
59
    }
60
61
    public function fromAttributeType(
62
        Identifier|Name|NullableType|UnionType|null $type,
63
        ?Doc $docBlock
64
    ): TypeDeclaration {
65
        $attributeDocBlock = new AttributeDocBlock($docBlock === null ? null : $docBlock->getText());
66
        if ($type === null) {
67
            return $attributeDocBlock->attributeType();
68
        }
69
70
        $typeDeclaration = $this->fromParsedType($type);
71
        if (! $typeDeclaration->isBuiltInArray()) {
72
            return $typeDeclaration;
73
        }
74
        if (! $attributeDocBlock->hasAttributeType()) {
75
            return $typeDeclaration;
76
        }
77
        return $attributeDocBlock->attributeType();
78
    }
79
80
    private function fromParsedType(Identifier|Name|NullableType|UnionType|null $type): TypeDeclaration
81
    {
82
        return match (true) {
83
            $type instanceof NullableType => TypeDeclaration::fromNullable((string) $type->type),
84
            $type instanceof Name => TypeDeclaration::from($type->getLast()),
85
            $type instanceof Identifier => TypeDeclaration::from((string) $type),
86
            $type === null => TypeDeclaration::absent(),
87
            default => TypeDeclaration::fromUnionType($this->fromUnionType($type)),
88
        };
89
    }
90
91
    /** @return string[] */
92
    private function fromUnionType(UnionType $type): array
93
    {
94
        return array_map(function (Identifier|NodeName $name): string {
95
            return $name instanceof Identifier ? $name->name : $name->getLast();
96
        }, $type->types);
97
    }
98
}
99