|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\Reflection; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Io\Exception\ExternalFileException; |
|
13
|
|
|
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesTypeIndication; |
|
14
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
|
15
|
|
|
use Railt\Reflection\Contracts\Dictionary; |
|
16
|
|
|
use Railt\Reflection\Definition\Behaviour\HasDeprecation; |
|
17
|
|
|
use Railt\Reflection\Definition\Behaviour\HasInheritance; |
|
18
|
|
|
use Railt\Reflection\Exception\TypeConflictException; |
|
19
|
|
|
use Railt\Reflection\Invocation\Behaviour\HasDirectives; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AbstractTypeDefinition |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractTypeDefinition extends AbstractDefinition implements TypeDefinition |
|
25
|
|
|
{ |
|
26
|
|
|
use HasDirectives; |
|
27
|
|
|
use HasDeprecation; |
|
28
|
|
|
use HasInheritance; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $name; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string|null |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $description; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* AbstractTypeDefinition constructor. |
|
42
|
|
|
* @param Document|\Railt\Reflection\Contracts\Document $document |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
*/ |
|
45
|
9 |
|
public function __construct(Document $document, string $name) |
|
46
|
|
|
{ |
|
47
|
9 |
|
$this->name = $name; |
|
48
|
|
|
|
|
49
|
9 |
|
parent::__construct($document); |
|
50
|
9 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getDescription(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return (string)$this->description; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param null|string $description |
|
62
|
|
|
* @return TypeDefinition|$this |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function withDescription(?string $description): TypeDefinition |
|
65
|
|
|
{ |
|
66
|
9 |
|
$this->description = $description; |
|
67
|
|
|
|
|
68
|
9 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* @return TypeDefinition|$this |
|
74
|
|
|
*/ |
|
75
|
8 |
|
public function renameTo(string $name): TypeDefinition |
|
76
|
|
|
{ |
|
77
|
8 |
|
$this->name = $name; |
|
78
|
|
|
|
|
79
|
8 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return Dictionary |
|
84
|
|
|
*/ |
|
85
|
8 |
|
public function getDictionary(): Dictionary |
|
86
|
|
|
{ |
|
87
|
8 |
|
return $this->document->getDictionary(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param TypeDefinition $definition |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
8 |
|
public function instanceOf(TypeDefinition $definition): bool |
|
95
|
|
|
{ |
|
96
|
8 |
|
if ($definition::getType()->is(Type::ANY)) { |
|
97
|
8 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
8 |
|
if ($this->getName() === $definition->getName()) { |
|
101
|
8 |
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
8 |
|
return $this->isExtends($definition); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
56 |
|
public function __toString(): string |
|
111
|
|
|
{ |
|
112
|
56 |
|
return \sprintf('%s<%s>', $this->name ?? '?', static::getType()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
25 |
|
public function getName(): string |
|
119
|
|
|
{ |
|
120
|
25 |
|
return $this->name; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param TypeDefinition $type |
|
126
|
|
|
* @throws ExternalFileException |
|
127
|
|
|
*/ |
|
128
|
9 |
|
protected function verifyInputType(TypeDefinition $type): void |
|
129
|
|
|
{ |
|
130
|
9 |
|
if (! $type::getType()->isInputable()) { |
|
131
|
|
|
$error = 'Type %s can contains only inputable types, but %s given'; |
|
132
|
|
|
throw $this->error(new TypeConflictException(\sprintf($error, $this, $type))); |
|
133
|
|
|
} |
|
134
|
9 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param TypeDefinition $type |
|
138
|
|
|
* @throws ExternalFileException |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function verifyOutputType(TypeDefinition $type): void |
|
141
|
|
|
{ |
|
142
|
|
|
if (! $type::getType()->isInputable()) { |
|
143
|
|
|
$error = 'Type %s can not be defined as return type hint of %s'; |
|
144
|
|
|
throw $this->error(new TypeConflictException(\sprintf($error, $type, $this))); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|