|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 7.1 |
|
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\Code\Variables; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* It represents a variable's type declaration |
|
12
|
|
|
*/ |
|
13
|
|
|
class TypeDeclaration |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string[] All valid types for PHP 7.1 */ |
|
16
|
|
|
private static $builtInTypes = [ |
|
17
|
|
|
'int', 'bool', 'string', 'array', 'float', 'callable', 'iterable' |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $name; |
|
22
|
|
|
|
|
23
|
168 |
|
public static function absent(): TypeDeclaration |
|
24
|
|
|
{ |
|
25
|
168 |
|
return new TypeDeclaration(null); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
171 |
|
public static function from(?string $text): TypeDeclaration |
|
29
|
|
|
{ |
|
30
|
171 |
|
return new TypeDeclaration($text); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
132 |
|
public function isPresent(): bool |
|
34
|
|
|
{ |
|
35
|
132 |
|
return null !== $this->name; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* It helps building the relationships between classes/interfaces since built-in |
|
40
|
|
|
* types are not part of a UML class diagram |
|
41
|
|
|
* |
|
42
|
|
|
* @see \PhUml\Code\Variables\WithTypeDeclaration::isAReference() for more details |
|
43
|
|
|
*/ |
|
44
|
75 |
|
public function isBuiltIn(): bool |
|
45
|
|
|
{ |
|
46
|
75 |
|
$type = $this->name; |
|
47
|
75 |
|
if ($this->isArray()) { |
|
48
|
24 |
|
$type = $this->removeArraySuffix(); |
|
49
|
|
|
} |
|
50
|
75 |
|
return $this->isPresent() && \in_array($type, self::$builtInTypes, true); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
75 |
|
private function isArray(): bool |
|
54
|
|
|
{ |
|
55
|
75 |
|
return strpos($this->name, '[]') === \strlen($this->name) - 2; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
24 |
|
private function removeArraySuffix(): string |
|
59
|
|
|
{ |
|
60
|
24 |
|
return substr($this->name, 0, -2); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
228 |
|
private function __construct(?string $name) |
|
64
|
|
|
{ |
|
65
|
228 |
|
$this->name = $name; |
|
66
|
228 |
|
} |
|
67
|
|
|
|
|
68
|
78 |
|
public function __toString() |
|
69
|
|
|
{ |
|
70
|
78 |
|
return (string)$this->name; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|