Debian Codegraph @

vendor/zircote/swagger-php/src/Analysers/ReflectionAnalyser.php

raw
1 <?php declare(strict_types=1);
2
3 /**
4 * @license Apache 2.0
5 */
6
7 namespace OpenApi\Analysers;
8
9 use OpenApi\Analysis;
10 use OpenApi\Annotations as OA;
11 use OpenApi\Context;
12 use OpenApi\Generator;
13 use OpenApi\GeneratorAwareTrait;
14 use OpenApi\OpenApiException;
15
16 /**
17 * OpenApi analyser using reflection.
18 *
19 * Can read either PHP <code>DocBlock</code>s or <code>Attribute</code>s.
20 *
21 * Due to the nature of reflection, this requires all related classes to be auto-loadable.
22 */
23 class ReflectionAnalyser implements AnalyserInterface
24 {
25 use GeneratorAwareTrait;
26
27 /** @var list<AnnotationFactoryInterface> */
28 protected array $annotationFactories = [];
29
30 /**
31 * @param list<AnnotationFactoryInterface> $annotationFactories
32 */
33 public function __construct(array $annotationFactories = [])
34 {
35 foreach ($annotationFactories as $annotationFactory) {
36 if ($annotationFactory->isSupported()) {
37 $this->annotationFactories[] = $annotationFactory;
38 }
39 }
40
41 if (!$this->annotationFactories) {
42 throw new OpenApiException('No suitable annotation factory found. At least one of "Doctrine Annotations" or PHP 8.1 are required');
43 }
44 }
45
46 public function setGenerator(Generator $generator): static
47 {
48 $this->generator = $generator;
49
50 foreach ($this->annotationFactories as $annotationFactory) {
51 $annotationFactory->setGenerator($generator);
52 }
53
54 return $this;
55 }
56
57 public function fromFile(string $filename, Context $context): Analysis
58 {
59 $scanner = new TokenScanner();
60 $fileDetails = $scanner->scanFile($filename);
61
62 $analysis = new Analysis([], $context);
63 foreach ($fileDetails as $fqdn => $details) {
64 $this->analyzeFqdn($fqdn, $analysis, $details);
65 }
66
67 return $analysis;
68 }
69
70 public function fromFqdn(string $fqdn, Analysis $analysis): Analysis
71 {
72 $fqdn = ltrim($fqdn, '\\');
73
74 $rc = new \ReflectionClass($fqdn);
75 if (!$filename = $rc->getFileName()) {
76 return $analysis;
77 }
78
79 $scanner = new TokenScanner();
80 $fileDetails = $scanner->scanFile($filename);
81
82 $this->analyzeFqdn($fqdn, $analysis, $fileDetails[$fqdn]);
83
84 return $analysis;
85 }
86
87 protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details): Analysis
88 {
89 if (!class_exists($fqdn) && !interface_exists($fqdn) && !trait_exists($fqdn) && (!function_exists('enum_exists') || !enum_exists($fqdn))) {
90 $analysis->context->logger->warning('Skipping unknown ' . $fqdn);
91
92 return $analysis;
93 }
94
95 $rc = new \ReflectionClass($fqdn);
96 $contextType = $rc->isInterface()
97 ? 'interface'
98 : ($rc->isTrait() ? 'trait' : ($rc->isEnum() ? 'enum' : 'class'));
99 $context = new Context([
100 $contextType => $rc->getShortName(),
101 'namespace' => $rc->getNamespaceName() ?: null,
102 'uses' => $details['uses'],
103 'comment' => $rc->getDocComment() ?: null,
104 'filename' => $rc->getFileName() ?: null,
105 'line' => $rc->getStartLine(),
106 'annotations' => [],
107 'scanned' => $details,
108 'reflector' => $rc,
109 ], $analysis->context);
110
111 $definition = [
112 $contextType => $rc->getShortName(),
113 'extends' => null,
114 'implements' => [],
115 'traits' => [],
116 'properties' => [],
117 'methods' => [],
118 'context' => $context,
119 ];
120 $normaliseClass = static fn (string $name): string => '\\' . ltrim($name, '\\');
121 if ($parentClass = $rc->getParentClass()) {
122 $definition['extends'] = $normaliseClass($parentClass->getName());
123 }
124 $definition[$contextType === 'class' ? 'implements' : 'extends'] = array_map($normaliseClass, $details['interfaces']);
125 $definition['traits'] = array_map($normaliseClass, $details['traits']);
126
127 foreach ($this->annotationFactories as $annotationFactory) {
128 $analysis->addAnnotations($annotationFactory->build($rc, $context), $context);
129 }
130
131 foreach ($rc->getMethods() as $method) {
132 if (in_array($method->name, $details['methods'])) {
133 $definition['methods'][$method->getName()] = $ctx = new Context([
134 'method' => $method->getName(),
135 'comment' => $method->getDocComment() ?: null,
136 'filename' => $method->getFileName() ?: null,
137 'line' => $method->getStartLine(),
138 'annotations' => [],
139 'reflector' => $method,
140 ], $context);
141 foreach ($this->annotationFactories as $annotationFactory) {
142 $annotations = $annotationFactory->build($method, $ctx);
143 $analysis->addAnnotations($annotations, $ctx);
144 }
145 }
146 }
147
148 foreach ($rc->getProperties() as $property) {
149 if (in_array($property->name, $details['properties'])) {
150 $definition['properties'][$property->getName()] = $ctx = new Context([
151 'property' => $property->getName(),
152 'comment' => $property->getDocComment() ?: null,
153 'annotations' => [],
154 'reflector' => $property,
155 ], $context);
156 if ($property->isStatic()) {
157 $ctx->static = true;
158 }
159 foreach ($this->annotationFactories as $annotationFactory) {
160 $analysis->addAnnotations($annotationFactory->build($property, $ctx), $ctx);
161 }
162 }
163 }
164
165 foreach ($rc->getReflectionConstants() as $constant) {
166 foreach ($this->annotationFactories as $annotationFactory) {
167 $definition['constants'][$constant->getName()] = $ctx = new Context([
168 'constant' => $constant->getName(),
169 'comment' => $constant->getDocComment() ?: null,
170 'annotations' => [],
171 'reflector' => $constant,
172 ], $context);
173 foreach ($annotationFactory->build($constant, $ctx) as $annotation) {
174 if ($annotation instanceof OA\Property) {
175 $analysis->addAnnotation($annotation, $ctx);
176 }
177 }
178 }
179 }
180
181 $addDefinition = 'add' . ucfirst($contextType) . 'Definition';
182 $analysis->{$addDefinition}($definition);
183
184 return $analysis;
185 }
186 }