Passed
Push — master ( f58add...4aa999 )
by Luis
54s queued 13s
created

TypeBuilder::fromAttributeType()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 9.6111
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.4
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\Attributes\AttributeDocBlock;
16
use PhUml\Code\Methods\MethodDocBlock;
17
use PhUml\Code\Variables\TypeDeclaration;
18
19
final class TypeBuilder
20
{
21
    /** @param Identifier|Name|NullableType|UnionType|null $type */
22
    public function fromMethodParameter($type, ?Doc $docBlock, string $name): TypeDeclaration
23
    {
24
        $methodDocBlock = new MethodDocBlock($docBlock === null ? null : $docBlock->getText());
25
        if ($type === null) {
26
            return $methodDocBlock->typeOfParameter($name);
27
        }
28
29
        $typeDeclaration = $this->fromParsedType($type);
30
        if ($typeDeclaration->isBuiltInArray() && $methodDocBlock->hasTypeOfParameter($name)) {
31
            return $methodDocBlock->typeOfParameter($name);
32
        }
33
        return $typeDeclaration;
34
    }
35
36
    /** @param Identifier|Name|NullableType|UnionType|null $type */
37
    public function fromMethodReturnType($type, ?Doc $docBlock): TypeDeclaration
38
    {
39
        $methodDocBlock = new MethodDocBlock($docBlock === null ? null : $docBlock->getText());
40
        if ($type === null) {
41
            return $methodDocBlock->returnType();
42
        }
43
44
        $typeDeclaration = $this->fromParsedType($type);
45
        if ($typeDeclaration->isBuiltInArray() && $methodDocBlock->hasReturnType()) {
46
            return $methodDocBlock->returnType();
47
        }
48
        return $typeDeclaration;
49
    }
50
51
    /** @param Identifier|Name|NullableType|UnionType|null $type */
52
    public function fromAttributeType($type, ?Doc $docBlock): TypeDeclaration
53
    {
54
        $attributeDocBlock = new AttributeDocBlock($docBlock === null ? null : $docBlock->getText());
55
        if ($type === null) {
56
            return $attributeDocBlock->attributeType();
57
        }
58
59
        $typeDeclaration = $this->fromParsedType($type);
60
        if ($typeDeclaration->isBuiltInArray() && $attributeDocBlock->hasAttributeType()) {
61
            return $attributeDocBlock->attributeType();
62
        }
63
        return $typeDeclaration;
64
    }
65
66
    /** @param Identifier|Name|NullableType|UnionType|null $type */
67
    private function fromParsedType($type): TypeDeclaration
68
    {
69
        switch (true) {
70
            case $type instanceof NullableType:
71
                return TypeDeclaration::fromNullable((string) $type->type);
72
            case $type instanceof Name:
73
                return TypeDeclaration::from($type->getLast());
74
            case $type instanceof Identifier:
75
                return TypeDeclaration::from((string) $type);
76
            default:
77
                throw UnsupportedType::declaredAs($type);
78
        }
79
    }
80
}
81