|
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\Code\Variables; |
|
7
|
|
|
|
|
8
|
|
|
use PhUml\Code\Name; |
|
9
|
|
|
use Stringable; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* It represents a variable's type declaration |
|
13
|
|
|
*/ |
|
14
|
|
|
final class TypeDeclaration implements Stringable |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string[] All valid types for PHP 8.1, pseudo-types, and aliases */ |
|
17
|
|
|
private const BUILT_IN_TYPES = [ |
|
18
|
|
|
// https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.base |
|
19
|
|
|
'int', 'bool', 'string', 'array', 'float', 'callable', 'iterable', 'mixed', 'object', |
|
20
|
|
|
// https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.union.nullable |
|
21
|
|
|
'null', |
|
22
|
|
|
// https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.return-only |
|
23
|
|
|
'void', 'never', |
|
24
|
|
|
// pseudo-types |
|
25
|
|
|
'resource', |
|
26
|
|
|
// aliases |
|
27
|
|
|
'number', 'boolean', 'integer', 'double', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Name[] */ |
|
31
|
|
|
private readonly array $names; |
|
32
|
|
|
|
|
33
|
96 |
|
public static function absent(): TypeDeclaration |
|
34
|
|
|
{ |
|
35
|
96 |
|
return new TypeDeclaration(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
106 |
|
public static function from(?string $type): TypeDeclaration |
|
39
|
|
|
{ |
|
40
|
106 |
|
return new TypeDeclaration($type === null ? [] : [new Name($type)]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
24 |
|
public static function fromNullable(string $type): TypeDeclaration |
|
44
|
|
|
{ |
|
45
|
24 |
|
return new TypeDeclaration([new Name($type)], isNullable: true); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* A composite type can be either a union or an intersection type |
|
50
|
|
|
* |
|
51
|
|
|
* @param string[] $types |
|
52
|
|
|
* @link https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.composite.union Union types documentation |
|
53
|
|
|
* @link https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.composite.intersection Intersection types documentation |
|
54
|
|
|
*/ |
|
55
|
36 |
|
public static function fromCompositeType(array $types, CompositeType $compositeType): TypeDeclaration |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
36 |
|
return new TypeDeclaration( |
|
58
|
36 |
|
array_map(static fn (string $type) => new Name($type), $types), |
|
59
|
|
|
compositeType: $compositeType |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** @param Name[] $names */ |
|
64
|
159 |
|
private function __construct( |
|
65
|
|
|
array $names = [], |
|
66
|
|
|
private readonly bool $isNullable = false, |
|
67
|
|
|
private readonly CompositeType $compositeType = CompositeType::NONE |
|
68
|
|
|
) { |
|
69
|
159 |
|
$this->names = $names; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
76 |
|
public function isPresent(): bool |
|
73
|
|
|
{ |
|
74
|
76 |
|
return $this->names !== []; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** @return Name[] */ |
|
78
|
27 |
|
public function references(): array |
|
79
|
|
|
{ |
|
80
|
27 |
|
if (! $this->isPresent()) { |
|
81
|
11 |
|
return []; |
|
82
|
|
|
} |
|
83
|
26 |
|
if ($this->isBuiltIn()) { |
|
84
|
10 |
|
return []; |
|
85
|
|
|
} |
|
86
|
23 |
|
if ($this->isRegularType()) { |
|
87
|
21 |
|
return [$this->isArray() ? new Name($this->removeArraySuffix()) : $this->names[0]]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
$typesFromUnion = array_map(static fn (Name $name) => TypeDeclaration::from($name->fullName()), $this->names); |
|
91
|
4 |
|
$references = array_filter($typesFromUnion, static fn (TypeDeclaration $type) => ! $type->isBuiltIn()); |
|
92
|
|
|
|
|
93
|
4 |
|
return array_map( |
|
94
|
4 |
|
static fn (TypeDeclaration $reference) => $reference->isArray() |
|
95
|
2 |
|
? new Name($reference->removeArraySuffix()) |
|
96
|
4 |
|
: $reference->names[0], |
|
97
|
|
|
$references |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* It helps building the relationships between classes/interfaces since built-in |
|
103
|
|
|
* types are not part of a UML class diagram |
|
104
|
|
|
*/ |
|
105
|
44 |
|
public function isBuiltIn(): bool |
|
106
|
|
|
{ |
|
107
|
44 |
|
if (! $this->isRegularType()) { |
|
108
|
6 |
|
return false; |
|
109
|
|
|
} |
|
110
|
43 |
|
$type = (string) $this->names[0]; |
|
111
|
43 |
|
if ($this->isArray()) { |
|
112
|
8 |
|
$type = $this->removeArraySuffix(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
43 |
|
return in_array($type, self::BUILT_IN_TYPES, true); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
private function removeArraySuffix(): string |
|
119
|
|
|
{ |
|
120
|
8 |
|
return substr($this->names[0]->fullName(), 0, -2); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
33 |
|
public function isBuiltInArray(): bool |
|
124
|
|
|
{ |
|
125
|
33 |
|
if ($this->isRegularType()) { |
|
126
|
30 |
|
return (string) $this->names[0] === 'array'; |
|
127
|
|
|
} |
|
128
|
18 |
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
43 |
|
private function isArray(): bool |
|
132
|
|
|
{ |
|
133
|
43 |
|
return str_ends_with($this->names[0]->fullName(), '[]'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
public function isNullable(): bool |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $this->isNullable; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
73 |
|
private function isRegularType(): bool |
|
142
|
|
|
{ |
|
143
|
73 |
|
return count($this->names) === 1; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
50 |
|
public function __toString(): string |
|
147
|
|
|
{ |
|
148
|
50 |
|
return ($this->isNullable ? '?' : '') . implode($this->compositeType->value, $this->names); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
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