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; |
|
|
|
|
9
|
|
|
use PhpParser\Node\ComplexType; |
|
|
|
|
10
|
|
|
use PhpParser\Node\Identifier; |
|
|
|
|
11
|
|
|
use PhpParser\Node\IntersectionType; |
|
|
|
|
12
|
|
|
use PhpParser\Node\Name; |
|
|
|
|
13
|
|
|
use PhpParser\Node\NullableType; |
|
|
|
|
14
|
|
|
use PhpParser\Node\UnionType; |
|
|
|
|
15
|
|
|
use PhUml\Code\UseStatements; |
16
|
|
|
use PhUml\Code\Variables\CompositeType; |
|
|
|
|
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), |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths