Passed
Pull Request — master (#23)
by
unknown
13:36 queued 10:51
created

TypeBuilder::fromParsedType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Parser\Code\Builders\Members;
7
8
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...
9
use PhpParser\Node\ComplexType;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\ComplexType 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...
10
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...
11
use PhpParser\Node\IntersectionType;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\IntersectionType 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\CompositeType;
0 ignored issues
show
Bug introduced by
The type PhUml\Code\Variables\CompositeType 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\Variables\TypeDeclaration;
18
use PhUml\Parser\Code\TypeResolver;
19
use RuntimeException;
20
21
final class TypeBuilder
22
{
23 59
    public function __construct(private readonly TypeResolver $typeResolver)
24
    {
25
    }
26
27 25
    public function fromMethodParameter(
28
        Identifier|Name|ComplexType|null $type,
29
        ?Doc $docBlock,
30
        string $name,
31
        UseStatements $useStatements
32
    ): TypeDeclaration {
33 25
        $methodComment = $docBlock?->getText();
34 25
        if ($type === null) {
35 18
            return $this->typeResolver->resolveForParameter($methodComment, $name, $useStatements);
36
        }
37
38 22
        $typeDeclaration = $this->fromParsedType($type);
39 22
        if (! $typeDeclaration->isBuiltInArray()) {
40 21
            return $typeDeclaration;
41
        }
42
43 8
        $typeFromDocBlock = $this->typeResolver->resolveForParameter($methodComment, $name, $useStatements);
44
45 8
        return $typeFromDocBlock->isPresent() ? $typeFromDocBlock : $typeDeclaration;
46
    }
47
48 23
    public function fromMethodReturnType(
49
        Identifier|Name|ComplexType|null $type,
50
        ?Doc $docBlock,
51
        UseStatements $useStatements
52
    ): TypeDeclaration {
53 23
        $methodComment = $docBlock?->getText();
54 23
        if ($type === null) {
55 19
            return $this->typeResolver->resolveForReturn($methodComment, $useStatements);
56
        }
57
58 20
        $typeDeclaration = $this->fromParsedType($type);
59 20
        if (! $typeDeclaration->isBuiltInArray()) {
60 19
            return $typeDeclaration;
61
        }
62
63 2
        $typeFromDocBlock = $this->typeResolver->resolveForReturn($methodComment, $useStatements);
64
65 2
        return $typeFromDocBlock->isPresent() ? $typeFromDocBlock : $typeDeclaration;
66
    }
67
68 22
    public function fromPropertyType(
69
        Identifier|Name|ComplexType|null $type,
70
        ?Doc $docBlock,
71
        UseStatements $useStatements
72
    ): TypeDeclaration {
73 22
        $propertyComment = $docBlock?->getText();
74 22
        if ($type === null) {
75 18
            return $this->typeResolver->resolveForProperty($propertyComment, $useStatements);
76
        }
77
78 9
        $typeDeclaration = $this->fromParsedType($type);
79 9
        if (! $typeDeclaration->isBuiltInArray()) {
80 8
            return $typeDeclaration;
81
        }
82
83 2
        $typeFromDocBlock = $this->typeResolver->resolveForProperty($propertyComment, $useStatements);
84
85 2
        return $typeFromDocBlock->isPresent() ? $typeFromDocBlock : $typeDeclaration;
86
    }
87
88 31
    private function fromParsedType(Identifier|Name|ComplexType|null $type): TypeDeclaration
89
    {
90
        return match (true) {
91 31
            $type instanceof NullableType => TypeDeclaration::fromNullable((string) $type->type),
92 31
            $type instanceof Name, $type instanceof Identifier => TypeDeclaration::from((string) $type),
93 17
            $type === null => TypeDeclaration::absent(),
94 17
            $type instanceof UnionType => TypeDeclaration::fromCompositeType(
95 17
                $this->fromCompositeType($type),
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $type of PhUml\Parser\Code\Builde...er::fromCompositeType() does only seem to accept PhpParser\Node\Intersect...hpParser\Node\UnionType, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
                $this->fromCompositeType(/** @scrutinizer ignore-type */ $type),
Loading history...
96
                CompositeType::UNION
97
            ),
98 1
            $type instanceof IntersectionType => TypeDeclaration::fromCompositeType(
99 1
                $this->fromCompositeType($type),
100
                CompositeType::INTERSECTION
101
            ),
102 31
            default => throw new RuntimeException(sprintf('%s is not supported', $type::class)),
103
        };
104
    }
105
106
    /** @return string[] */
107 17
    private function fromCompositeType(UnionType|IntersectionType $type): array
108
    {
109 17
        return array_map(
110 17
            static fn (Identifier|Name $name): string => (string) $name,
111 17
            $type->types
112
        );
113
    }
114
}
115