Skip to content

Commit 1769313

Browse files
committed
Support partial meta information
1 parent 9789d4f commit 1769313

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ XP Reflection ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
## 2.14.0 / 2023-09-23
7+
8+
* Added support for partial meta information - @thekid
9+
610
## 2.13.6 / 2023-06-25
711

812
* Fixed missing `xp::$sn` lookup when reflecting generics - @thekid

src/main/php/lang/meta/MetaInformation.class.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ public function typeAnnotations($reflect) {
7979
* @return ?string
8080
*/
8181
public function typeComment($reflect) {
82-
if ($meta= \xp::$meta[\xp::$cn[$reflect->name] ?? strtr($reflect->name, '\\', '.')]['class'] ?? null) {
83-
return $meta[DETAIL_COMMENT];
82+
$c= \xp::$cn[$reflect->name] ?? strtr($reflect->name, '\\', '.');
83+
if ($meta= \xp::$meta[$c]['class'][DETAIL_COMMENT] ?? null) {
84+
return $meta;
8485
} else if (false === ($c= $reflect->getDocComment())) {
8586
return null;
8687
} else {
@@ -114,8 +115,8 @@ public function constantAnnotations($reflect) {
114115
public function constantType($reflect) {
115116
$name= $reflect->getDeclaringClass()->name;
116117
$c= \xp::$cn[$name] ?? strtr($name, '\\', '.');
117-
if ($meta= \xp::$meta[$c][2][$reflect->name] ?? null) {
118-
return $meta[DETAIL_RETURNS];
118+
if ($meta= \xp::$meta[$c][2][$reflect->name][DETAIL_RETURNS] ?? null) {
119+
return $meta;
119120
} else {
120121
$tags= $this->tags($reflect);
121122
return $tags['var'][0] ?? $tags['type'][0] ?? null;
@@ -131,8 +132,8 @@ public function constantType($reflect) {
131132
public function constantComment($reflect) {
132133
$name= $reflect->getDeclaringClass()->name;
133134
$c= \xp::$cn[$name] ?? strtr($name, '\\', '.');
134-
if ($meta= \xp::$meta[$c][2][$reflect->name] ?? null) {
135-
return $meta[DETAIL_COMMENT];
135+
if ($meta= \xp::$meta[$c][2][$reflect->name][DETAIL_COMMENT] ?? null) {
136+
return $meta;
136137
} else if (false === ($c= $reflect->getDocComment())) {
137138
return null;
138139
} else {
@@ -165,8 +166,8 @@ public function propertyAnnotations($reflect) {
165166
public function propertyType($reflect) {
166167
$name= $reflect->getDeclaringClass()->name;
167168
$c= \xp::$cn[$name] ?? strtr($name, '\\', '.');
168-
if ($meta= \xp::$meta[$c][0][$reflect->name] ?? null) {
169-
return $meta[DETAIL_RETURNS];
169+
if ($meta= \xp::$meta[$c][0][$reflect->name][DETAIL_RETURNS] ?? null) {
170+
return $meta;
170171
} else {
171172
$tags= $this->tags($reflect);
172173
return $tags['var'][0] ?? $tags['type'][0] ?? null;
@@ -182,8 +183,8 @@ public function propertyType($reflect) {
182183
public function propertyComment($reflect) {
183184
$name= $reflect->getDeclaringClass()->name;
184185
$c= \xp::$cn[$name] ?? strtr($name, '\\', '.');
185-
if ($meta= \xp::$meta[$c][0][$reflect->name] ?? null) {
186-
return $meta[DETAIL_COMMENT];
186+
if ($meta= \xp::$meta[$c][0][$reflect->name][DETAIL_COMMENT] ?? null) {
187+
return $meta;
187188
} else if (false === ($c= $reflect->getDocComment())) {
188189
return null;
189190
} else {
@@ -215,8 +216,8 @@ public function methodAnnotations($reflect) {
215216
public function methodReturns($reflect) {
216217
$name= $reflect->getDeclaringClass()->name;
217218
$c= \xp::$cn[$name] ?? strtr($name, '\\', '.');
218-
if ($meta= \xp::$meta[$c][1][$reflect->name] ?? null) {
219-
return $meta[DETAIL_RETURNS];
219+
if ($meta= \xp::$meta[$c][1][$reflect->name][DETAIL_RETURNS] ?? null) {
220+
return $meta;
220221
} else {
221222
return $this->tags($reflect)['return'][0] ?? null;
222223
}
@@ -231,8 +232,8 @@ public function methodReturns($reflect) {
231232
public function methodComment($reflect) {
232233
$name= $reflect->getDeclaringClass()->name;
233234
$c= \xp::$cn[$name] ?? strtr($name, '\\', '.');
234-
if ($meta= \xp::$meta[$c][1][$reflect->name] ?? null) {
235-
return $meta[DETAIL_COMMENT];
235+
if ($meta= \xp::$meta[$c][1][$reflect->name][DETAIL_COMMENT] ?? null) {
236+
return $meta;
236237
} else if (false === ($c= $reflect->getDocComment())) {
237238
return null;
238239
} else {
@@ -295,7 +296,7 @@ public function virtualProperties($reflect) {
295296
if ($meta= \xp::$meta[\xp::$cn[$reflect->name] ?? strtr($reflect->name, '\\', '.')][0] ?? null) {
296297
foreach ($meta as $name => $property) {
297298
if ($arg= $property[DETAIL_ARGUMENTS] ?? null) {
298-
$r[$name]= [$arg[0], $property[DETAIL_RETURNS]];
299+
$r[$name]= [$arg[0], $property[DETAIL_RETURNS] ?? null];
299300
}
300301
}
301302
continue;

src/test/php/lang/reflection/unittest/ConstantsTest.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ public function with_constraint() {
105105
$this->declare('{ const string FIXTURE = "test"; }')->constant('FIXTURE')->constraint()
106106
);
107107
}
108+
109+
#[Test]
110+
public function no_type() {
111+
$type= $this->declare('{ const FIXTURE= 1; }');
112+
Assert::equals(Type::$VAR, $type->constant('FIXTURE')->constraint()->type());
113+
}
108114
}

src/test/php/lang/reflection/unittest/MethodsTest.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ public function returns_type_intersection_via_apidoc() {
152152
Assert::equals(new TypeIntersection([new XPClass('Countable'), new XPClass('Traversable')]), $returns->type());
153153
}
154154

155+
#[Test]
156+
public function no_returns() {
157+
$type= $this->declare('{ function fixture() { } }');
158+
Assert::equals(Type::$VAR, $type->method('fixture')->returns()->type());
159+
}
160+
155161
#[Test]
156162
public function annotations() {
157163
$t= $this->declare('{

src/test/php/lang/reflection/unittest/PropertiesTest.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ public function type_from_boolean_types($name) {
199199
);
200200
}
201201

202+
#[Test]
203+
public function no_type() {
204+
$type= $this->declare('{ public $fixture; }');
205+
Assert::equals(Type::$VAR, $type->property('fixture')->constraint()->type());
206+
}
207+
202208
#[Test]
203209
public function string_representation_without_type() {
204210
$t= $this->declare('{ public $fixture; }');

0 commit comments

Comments
 (0)