14
14
use phpDocumentor \Reflection \DocBlock ;
15
15
use phpDocumentor \Reflection \DocBlockFactory ;
16
16
use phpDocumentor \Reflection \DocBlockFactoryInterface ;
17
+ use phpDocumentor \Reflection \Types \Context ;
17
18
use phpDocumentor \Reflection \Types \ContextFactory ;
18
19
use Symfony \Component \PropertyInfo \PropertyDescriptionExtractorInterface ;
19
20
use Symfony \Component \PropertyInfo \PropertyTypeExtractorInterface ;
@@ -38,6 +39,11 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
38
39
*/
39
40
private $ docBlocks = [];
40
41
42
+ /**
43
+ * @var Context[]
44
+ */
45
+ private $ contexts = [];
46
+
41
47
private $ docBlockFactory ;
42
48
private $ contextFactory ;
43
49
private $ phpDocTypeHelper ;
@@ -191,7 +197,7 @@ private function getDocBlockFromProperty(string $class, string $property): ?DocB
191
197
}
192
198
193
199
try {
194
- return $ this ->docBlockFactory ->create ($ reflectionProperty , $ this ->contextFactory -> createFromReflector ($ reflectionProperty ->getDeclaringClass ()));
200
+ return $ this ->docBlockFactory ->create ($ reflectionProperty , $ this ->createFromReflector ($ reflectionProperty ->getDeclaringClass ()));
195
201
} catch (\InvalidArgumentException $ e ) {
196
202
return null ;
197
203
}
@@ -227,9 +233,25 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
227
233
}
228
234
229
235
try {
230
- return [$ this ->docBlockFactory ->create ($ reflectionMethod , $ this ->contextFactory -> createFromReflector ($ reflectionMethod )), $ prefix ];
236
+ return [$ this ->docBlockFactory ->create ($ reflectionMethod , $ this ->createFromReflector ($ reflectionMethod-> getDeclaringClass () )), $ prefix ];
231
237
} catch (\InvalidArgumentException $ e ) {
232
238
return null ;
233
239
}
234
240
}
241
+
242
+ /**
243
+ * Prevents a lot of redundant calls to ContextFactory::createForNamespace().
244
+ */
245
+ private function createFromReflector (\ReflectionClass $ reflector ): Context
246
+ {
247
+ $ cacheKey = $ reflector ->getNamespaceName ().': ' .$ reflector ->getFileName ();
248
+
249
+ if (isset ($ this ->contexts [$ cacheKey ])) {
250
+ return $ this ->contexts [$ cacheKey ];
251
+ }
252
+
253
+ $ this ->contexts [$ cacheKey ] = $ this ->contextFactory ->createFromReflector ($ reflector );
254
+
255
+ return $ this ->contexts [$ cacheKey ];
256
+ }
235
257
}
0 commit comments