1111
1212namespace Piwik \Plugins \OpenApiDocs \Annotations ;
1313
14+ use Matomo \Dependencies \OpenApiDocs \phpDocumentor \Reflection \DocBlock \Tags \Param ;
15+ use Matomo \Dependencies \OpenApiDocs \phpDocumentor \Reflection \DocBlock \Tags \TagWithType ;
16+ use Matomo \Dependencies \OpenApiDocs \phpDocumentor \Reflection \DocBlockFactory ;
1417use Piwik \API \DocumentationGenerator ;
1518use Piwik \API \NoDefaultValue ;
1619use Piwik \API \Proxy ;
2427use Piwik \UrlHelper ;
2528use Piwik \Validators \BaseValidator ;
2629use Piwik \Validators \NotEmpty ;
27- use PHPStan \PhpDocParser \Lexer \Lexer ;
28- use PHPStan \PhpDocParser \Parser \PhpDocParser ;
29- use PHPStan \PhpDocParser \Parser \TypeParser ;
30- use PHPStan \PhpDocParser \Parser \ConstExprParser ;
31- use PHPStan \PhpDocParser \Parser \TokenIterator ;
3230
3331class AnnotationGenerator
3432{
@@ -282,21 +280,21 @@ protected function buildAnnotationForMethod(array $rules, string $pluginName, \R
282280 */
283281 public function getParamInfoFromDocBlock (string $ docBlock ): array
284282 {
285- $ lexer = new Lexer ();
286- $ tokens = $ lexer ->tokenize ($ docBlock );
287- $ expressionParser = new ConstExprParser ();
288- $ parser = new PhpDocParser (new TypeParser ($ expressionParser ), $ expressionParser );
289- $ node = $ parser ->parse (new TokenIterator ($ tokens ));
283+ $ factory = DocBlockFactory::createInstance ();
284+ $ docBlockObject = $ factory ->create ($ docBlock );
290285
291286 $ params = [];
292- foreach ($ node ->getParamTagValues () as $ param ) {
293- $ name = ltrim ($ param ->parameterName , '$ ' );
287+ foreach ($ docBlockObject ->getTagsByName ('param ' ) as $ param ) {
288+ if (!($ param instanceof Param)) {
289+ continue ;
290+ }
291+ $ name = ltrim ($ param ->getVariableName (), '$ ' );
294292 $ params [$ name ] = [
295- 'type ' => (string )$ param ->type ,
293+ 'type ' => (string ) $ param ->getType () ,
296294 // Normalise the description. E.g. remove linebreaks and indentation
297- 'description ' => trim (preg_replace (['/^\h+/m ' , '/\R+/u ' ,], ['' , ' ' ], $ param ->description )),
298- 'byRef ' => $ param ->isReference ,
299- 'variadic ' => $ param ->isVariadic ,
295+ 'description ' => trim (preg_replace (['/^\h+/m ' , '/\R+/u ' ,], ['' , ' ' ], ( string ) $ param ->getDescription () )),
296+ 'byRef ' => $ param ->isReference () ,
297+ 'variadic ' => $ param ->isVariadic () ,
300298 ];
301299 }
302300 return $ params ;
@@ -313,27 +311,24 @@ public function getParamInfoFromDocBlock(string $docBlock): array
313311 */
314312 public function getResponseInfoFromDocBlock (string $ docBlock ): array
315313 {
316- $ lexer = new Lexer ();
317- $ tokens = $ lexer ->tokenize ($ docBlock );
318- $ expressionParser = new ConstExprParser ();
319- $ parser = new PhpDocParser (new TypeParser ($ expressionParser ), $ expressionParser );
320- $ node = $ parser ->parse (new TokenIterator ($ tokens ));
314+ $ factory = DocBlockFactory::createInstance ();
315+ $ docBlockObject = $ factory ->create ($ docBlock );
321316
322317 $ responseInfo = ['type ' => null ];
323- $ returnTags = $ node -> getReturnTagValues ( );
324- if (empty ($ returnTags )) {
318+ $ returnTags = $ docBlockObject -> getTagsByName ( ' return ' );
319+ if (empty ($ returnTags ) || !( $ returnTags [ 0 ] instanceof TagWithType) ) {
325320 return $ responseInfo ;
326321 }
327322
328323 $ returnTag = $ returnTags [0 ];
329- $ tagValue = strval ($ returnTag ->type );
324+ $ tagValue = strval ($ returnTag ->getType () );
330325 $ responseInfo ['type ' ] = $ this ->getOpenApiTypeFromPhpType ($ tagValue );
331326 if ($ responseInfo ['type ' ] === 'string ' && !empty ($ tagValue ) && strtolower ($ tagValue ) !== 'string ' ) {
332327 $ responseInfo ['type ' ] = '' ;
333328 $ responseInfo ['description ' ] = 'Response of unknown type ' ;
334329 }
335- if (!empty ($ returnTag ->description )) {
336- $ responseInfo ['description ' ] = $ returnTag ->description ;
330+ if (!empty ($ returnTag ->getDescription () )) {
331+ $ responseInfo ['description ' ] = $ returnTag ->getDescription () ;
337332 }
338333
339334 return $ responseInfo ;
0 commit comments