|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 7.2 |
|
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
|
|
|
use PhUml\Code\Name; |
|
11
|
|
|
use PhUml\Code\Named; |
|
12
|
|
|
use PhUml\Code\WithName; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* It represents a variable's type declaration |
|
16
|
|
|
*/ |
|
17
|
|
|
final class TypeDeclaration implements Named |
|
18
|
|
|
{ |
|
19
|
|
|
use WithName; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string[] All valid types for PHP 7.1, pseudo-types, and aliases */ |
|
22
|
|
|
private static $builtInTypes = [ |
|
23
|
|
|
'int', 'bool', 'string', 'array', 'float', 'callable', 'iterable', |
|
24
|
|
|
// pseudo-types |
|
25
|
|
|
'mixed', 'number', 'object', 'resource', 'self', |
|
26
|
|
|
// aliases |
|
27
|
|
|
'boolean', 'integer', 'double', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
213 |
|
/** @var bool */ |
|
31
|
|
|
private $isNullable; |
|
32
|
213 |
|
|
|
33
|
|
|
public static function absent(): TypeDeclaration |
|
34
|
|
|
{ |
|
35
|
249 |
|
return new TypeDeclaration(null); |
|
36
|
|
|
} |
|
37
|
249 |
|
|
|
38
|
|
|
public static function from(?string $type): TypeDeclaration |
|
39
|
|
|
{ |
|
40
|
183 |
|
return new TypeDeclaration($type); |
|
41
|
|
|
} |
|
42
|
183 |
|
|
|
43
|
|
|
public static function fromNullable(string $type): TypeDeclaration |
|
44
|
|
|
{ |
|
45
|
|
|
return new TypeDeclaration($type, true); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function isPresent(): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->name !== null; |
|
51
|
117 |
|
} |
|
52
|
|
|
|
|
53
|
117 |
|
/** |
|
54
|
117 |
|
* It helps building the relationships between classes/interfaces since built-in |
|
55
|
27 |
|
* types are not part of a UML class diagram |
|
56
|
|
|
* |
|
57
|
117 |
|
* @see \PhUml\Code\Variables\WithTypeDeclaration::isAReference() for more details |
|
58
|
|
|
*/ |
|
59
|
|
|
public function isBuiltIn(): bool |
|
60
|
117 |
|
{ |
|
61
|
|
|
$type = (string) $this->name; |
|
62
|
117 |
|
if ($this->isArray()) { |
|
63
|
|
|
$type = $this->removeArraySuffix(); |
|
64
|
|
|
} |
|
65
|
27 |
|
return $this->isPresent() && \in_array($type, self::$builtInTypes, true); |
|
66
|
|
|
} |
|
67
|
27 |
|
|
|
68
|
|
|
public function isArray(): bool |
|
69
|
|
|
{ |
|
70
|
318 |
|
return strpos((string) $this->name, '[]') === \strlen((string) $this->name) - 2; |
|
71
|
|
|
} |
|
72
|
318 |
|
|
|
73
|
318 |
|
public function isNullable(): bool |
|
74
|
|
|
{ |
|
75
|
93 |
|
return $this->isNullable; |
|
76
|
|
|
} |
|
77
|
93 |
|
|
|
78
|
|
|
public function removeArraySuffix(): string |
|
79
|
|
|
{ |
|
80
|
|
|
return substr((string) $this->name, 0, -2); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function __toString() |
|
84
|
|
|
{ |
|
85
|
|
|
return ($this->isNullable ? '?' : '') . $this->name; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function __construct(?string $name, bool $isNullable = false) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->name = $name !== null ? Name::from($name) : null; |
|
91
|
|
|
$this->isNullable = $name !== null && $isNullable; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|