@@ -45,8 +45,6 @@ public function __invoke(Analysis $analysis): void
4545 return ;
4646 }
4747
48- $ analysis ->annotations = $ this ->collectAnnotations ($ analysis ->annotations );
49-
5048 // allow multiple runs to catch nested dependencies
5149 for ($ ii = 0 ; $ ii < 10 ; ++$ ii ) {
5250 if (!$ this ->cleanup ($ analysis )) {
@@ -60,14 +58,14 @@ protected function cleanup(Analysis $analysis): bool
6058 $ usedRefs = [];
6159 foreach ($ analysis ->annotations as $ annotation ) {
6260 if (property_exists ($ annotation , 'ref ' ) && !Generator::isDefault ($ annotation ->ref ) && $ annotation ->ref !== null ) {
63- $ usedRefs [$ annotation ->ref ] = $ annotation -> ref ;
61+ $ usedRefs [$ annotation ->ref ] = true ;
6462 }
6563
6664 foreach (['allOf ' , 'anyOf ' , 'oneOf ' ] as $ sub ) {
6765 if (property_exists ($ annotation , $ sub ) && !Generator::isDefault ($ annotation ->{$ sub })) {
6866 foreach ($ annotation ->{$ sub } as $ subElem ) {
6967 if (is_object ($ subElem ) && property_exists ($ subElem , 'ref ' ) && !Generator::isDefault ($ subElem ->ref ) && $ subElem ->ref !== null ) {
70- $ usedRefs [$ subElem ->ref ] = $ subElem -> ref ;
68+ $ usedRefs [$ subElem ->ref ] = true ;
7169 }
7270 }
7371 }
@@ -77,47 +75,37 @@ protected function cleanup(Analysis $analysis): bool
7775 if (!Generator::isDefault ($ annotation ->security )) {
7876 foreach ($ annotation ->security as $ security ) {
7977 foreach (array_keys ($ security ) as $ securityName ) {
80- $ ref = OA \Components::COMPONENTS_PREFIX . 'securitySchemes/ ' . $ securityName ;
81- $ usedRefs [$ ref ] = $ ref ;
78+ $ usedRefs [OA \Components::COMPONENTS_PREFIX . 'securitySchemes/ ' . $ securityName ] = true ;
8279 }
8380 }
8481 }
8582 }
8683 }
8784
88- $ unusedRefs = [];
85+ $ unusedComponents = [];
8986 foreach (OA \Components::$ _nested as $ nested ) {
9087 if (2 == count ($ nested )) {
91- // $nested[1] is the name of the property that holds the component name
9288 [$ componentType , $ nameProperty ] = $ nested ;
9389 if (!Generator::isDefault ($ analysis ->openapi ->components ->{$ componentType })) {
94- foreach ($ analysis ->openapi ->components ->{$ componentType } as $ component ) {
90+ foreach ($ analysis ->openapi ->components ->{$ componentType } as $ ii => $ component ) {
9591 $ ref = OA \Components::ref ($ component );
96- if (!in_array ( $ ref , $ usedRefs )) {
97- $ unusedRefs [ $ ref ] = [$ ref , $ nameProperty ];
92+ if (!isset ( $ usedRefs[ $ ref ] )) {
93+ $ unusedComponents [ ] = [$ componentType , $ ii , $ component ];
9894 }
9995 }
10096 }
10197 }
10298 }
10399
104- // remove unused
105- foreach ($ unusedRefs as $ refDetails ) {
106- [$ ref , $ nameProperty ] = $ refDetails ;
107- [$ hash , $ components , $ componentType , $ name ] = explode ('/ ' , $ ref );
108- foreach ($ analysis ->openapi ->components ->{$ componentType } as $ ii => $ component ) {
109- if ($ component ->{$ nameProperty } == $ name ) {
110- $ annotation = $ analysis ->openapi ->components ->{$ componentType }[$ ii ];
111- $ this ->removeAnnotation ($ analysis ->annotations , $ annotation );
112- unset($ analysis ->openapi ->components ->{$ componentType }[$ ii ]);
113-
114- if (!$ analysis ->openapi ->components ->{$ componentType }) {
115- $ analysis ->openapi ->components ->{$ componentType } = Generator::UNDEFINED ;
116- }
117- }
100+ foreach ($ unusedComponents as [$ componentType , $ ii , $ component ]) {
101+ $ this ->removeAnnotationRecursive ($ analysis , $ component );
102+ unset($ analysis ->openapi ->components ->{$ componentType }[$ ii ]);
103+
104+ if (!$ analysis ->openapi ->components ->{$ componentType }) {
105+ $ analysis ->openapi ->components ->{$ componentType } = Generator::UNDEFINED ;
118106 }
119107 }
120108
121- return [] !== $ unusedRefs ;
109+ return [] !== $ unusedComponents ;
122110 }
123111}
0 commit comments