Debian Codegraph @

vendor/zircote/swagger-php/src/Processors/ExpandTraits.php

raw
1 <?php declare(strict_types=1);
2
3 /**
4 * @license Apache 2.0
5 */
6
7 namespace OpenApi\Processors;
8
9 use OpenApi\Analysis;
10 use OpenApi\Annotations as OA;
11 use OpenApi\Generator;
12
13 /**
14 * Look at all (direct) traits for a schema and:
15 * - merge trait annotations/methods/properties into the schema if the trait does not have a schema itself
16 * - inherit from the trait if it has a schema (allOf).
17 */
18 class ExpandTraits
19 {
20 use Concerns\MergePropertiesTrait;
21
22 public function __invoke(Analysis $analysis): void
23 {
24 $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true);
25
26 // do regular trait inheritance / merge
27 foreach ($schemas as $schema) {
28 if ($schema->_context->is('trait')) {
29 $traits = $analysis->getTraitsOfClass($schema->_context->fullyQualifiedName($schema->_context->trait), true);
30 $existing = [];
31 foreach ($traits as $trait) {
32 $traitSchema = $analysis->getAnnotationForSource($trait['context']->fullyQualifiedName($trait['trait']));
33 if ($traitSchema) {
34 $refPath = Generator::isDefault($traitSchema->schema) ? $trait['trait'] : $traitSchema->schema;
35 $this->inheritFrom($analysis, $schema, $traitSchema, $refPath, $trait['context']);
36 } else {
37 $this->mergeMethods($schema, $trait, $existing);
38 $this->mergeProperties($schema, $trait, $existing);
39 }
40 }
41 }
42 }
43
44 foreach ($schemas as $schema) {
45 if ($schema->_context->is('class') && !$schema->_context->is('generated')) {
46 // look at class traits
47 $traits = $analysis->getTraitsOfClass($schema->_context->fullyQualifiedName($schema->_context->class), true);
48 $existing = [];
49 foreach ($traits as $trait) {
50 $traitSchema = $analysis->getAnnotationForSource($trait['context']->fullyQualifiedName($trait['trait']));
51 if ($traitSchema) {
52 $refPath = Generator::isDefault($traitSchema->schema) ? $trait['trait'] : $traitSchema->schema;
53 $this->inheritFrom($analysis, $schema, $traitSchema, $refPath, $trait['context']);
54 } else {
55 $this->mergeMethods($schema, $trait, $existing);
56 $this->mergeProperties($schema, $trait, $existing);
57 }
58 }
59
60 // also merge ancestor traits of non schema parents
61 $ancestors = $analysis->getSuperClasses($schema->_context->fullyQualifiedName($schema->_context->class));
62 $existing = [];
63 foreach ($ancestors as $ancestor) {
64 $ancestorSchema = $analysis->getAnnotationForSource($ancestor['context']->fullyQualifiedName($ancestor['class']));
65 if ($ancestorSchema) {
66 // stop here as we inherit everything above
67 break;
68 } else {
69 $traits = $analysis->getTraitsOfClass($schema->_context->fullyQualifiedName($ancestor['class']), true);
70 foreach ($traits as $trait) {
71 $this->mergeMethods($schema, $trait, $existing);
72 $this->mergeProperties($schema, $trait, $existing);
73 }
74 }
75 }
76 }
77 }
78 }
79 }