Skip to content

Commit 1782c27

Browse files
authored
Add reflection details to context (#1782)
1 parent 642f454 commit 1782c27

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/Analysers/AttributeAnnotationFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function build(\Reflector $reflector, Context $context): array
7171
foreach ($rp->getAttributes($attributeName, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
7272
/** @var OA\Property|OA\Parameter|OA\RequestBody $instance */
7373
$instance = $attribute->newInstance();
74-
$instance->_context = new Context(['nested' => false], $context);
74+
$instance->_context = new Context(['nested' => false, 'reflector' => $rp], $context);
7575

7676
$type = (($rnt = $rp->getType()) && $rnt instanceof \ReflectionNamedType) ? $rnt->getName() : Generator::UNDEFINED;
7777
$nullable = $rnt ? $rnt->allowsNull() : true;
@@ -89,7 +89,7 @@ public function build(\Reflector $reflector, Context $context): array
8989

9090
if ($rp->isPromoted()) {
9191
// ensure each property has its own context
92-
$instance->_context = new Context(['generated' => true, 'annotations' => [$instance]], $context);
92+
$instance->_context = new Context(['generated' => true, 'annotations' => [$instance], 'reflector' => $rp], $context);
9393

9494
// promoted parameter - docblock is available via class/property
9595
if ($comment = $rp->getDeclaringClass()->getProperty($rp->getName())->getDocComment()) {
@@ -101,7 +101,7 @@ public function build(\Reflector $reflector, Context $context): array
101101
$instance->name = $rp->getName();
102102
}
103103
$instance->required = !$nullable;
104-
$context = new Context(['nested' => $this], $context);
104+
$context = new Context(['nested' => $this, 'reflector' => $rp], $context);
105105
$context->comment = null;
106106
$instance->merge([new OA\Schema(['type' => $type, '_context' => $context])]);
107107
}
@@ -115,6 +115,7 @@ public function build(\Reflector $reflector, Context $context): array
115115
if ($annotation instanceof OA\Property && Generator::isDefault($annotation->type)) {
116116
// pick up simple return types
117117
$annotation->type = $rrt->getName();
118+
$annotation->_context->reflector = $rrt;
118119
}
119120
}
120121
}

src/Analysers/ReflectionAnalyser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
101101
'line' => $rc->getStartLine(),
102102
'annotations' => [],
103103
'scanned' => $details,
104+
'reflector' => $rc,
104105
], $analysis->context);
105106

106107
$definition = [
@@ -131,6 +132,7 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
131132
'filename' => $method->getFileName() ?: null,
132133
'line' => $method->getStartLine(),
133134
'annotations' => [],
135+
'reflector' => $method,
134136
], $context);
135137
foreach ($this->annotationFactories as $annotationFactory) {
136138
$analysis->addAnnotations($annotationFactory->build($method, $ctx), $ctx);
@@ -144,6 +146,7 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
144146
'property' => $property->getName(),
145147
'comment' => $property->getDocComment() ?: null,
146148
'annotations' => [],
149+
'reflector' => $property,
147150
], $context);
148151
if ($property->isStatic()) {
149152
$ctx->static = true;
@@ -170,6 +173,7 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)
170173
'constant' => $constant->getName(),
171174
'comment' => $constant->getDocComment() ?: null,
172175
'annotations' => [],
176+
'reflector' => $constant,
173177
], $context);
174178
foreach ($annotationFactory->build($constant, $ctx) as $annotation) {
175179
if ($annotation instanceof OA\Property) {

src/Context.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
* @property array|null $implements
3131
* @property string|null $method
3232
* @property string|null $property
33-
* @property string|\ReflectionType|null $type
33+
* @property \Reflector|null $reflector Optional reflection details
34+
* @property string|null $type Optional native data type
3435
* @property bool|null $static Indicate a static method
3536
* @property bool|null $nullable Indicate a nullable value
3637
* @property bool|null $generated Indicate the context was generated by a processor or

0 commit comments

Comments
 (0)