Skip to content

Commit 2c96659

Browse files
authored
Add doc parser code change (#16)
* Make code change after dependency update * Making a few more adjustments to switch dependencies * Try adding bootstrap to alias classes for PHPStan * Try some changes * Revert "Try some changes" This reverts commit 43b085b. * Revert "Try adding bootstrap to alias classes for PHPStan" This reverts commit 1626077. * Adding autoload to test file * Trying simplified bootstrap for PHPStan
1 parent 3d5d599 commit 2c96659

5 files changed

Lines changed: 29 additions & 32 deletions

File tree

Annotations/AnnotationGenerator.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace 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;
1417
use Piwik\API\DocumentationGenerator;
1518
use Piwik\API\NoDefaultValue;
1619
use Piwik\API\Proxy;
@@ -24,11 +27,6 @@
2427
use Piwik\UrlHelper;
2528
use Piwik\Validators\BaseValidator;
2629
use 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

3331
class 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;

Commands/GenerateAnnotations.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ protected function doExecute(): int
7272
$input = $this->getInput();
7373
$output = $this->getOutput();
7474

75-
if (!class_exists('PHPStan\PhpDocParser\Parser\PhpDocParser')) {
76-
$output->writeln("<error>This command requires phpstan/phpdoc-parser. It should be available while running Matomo in development mode.</error>");
77-
return self::FAILURE;
78-
}
79-
8075
$plugin = $input->getOption('plugin');
8176
if (empty($plugin)) {
8277
throw new \RuntimeException('Please specify a plugin name.');

phpstan-bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Include the scoped dependencies in the autoloader
4+
require_once __DIR__ . '/vendor/autoload.php';

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ parameters:
1111
- scoper.inc.php
1212
- Specs/SpecGenerator.php
1313
bootstrapFiles:
14+
- phpstan-bootstrap.php
1415
- ../../bootstrap-phpstan.php
1516
universalObjectCratesClasses:
1617
- Piwik\Config

tests/Unit/AnnotationGeneratorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Piwik\Plugins\OpenApiDocs\tests\Unit;
1414

15+
require_once PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/vendor/autoload.php';
1516
use PHPUnit\Framework\TestCase;
1617
use Piwik\API\DocumentationGenerator;
1718
use Piwik\API\NoDefaultValue;
@@ -153,7 +154,7 @@ class AnnotationGeneratorTest extends TestCase
153154
* @param int[] $idDestinationSites Optional array of IDs identifying which site(s) the new custom report is to be
154155
* assigned to. The default is [idSite] when nothing is provided.
155156
*
156-
* @return array
157+
* @return array Some test description for the return annotation.
157158
* @throws Exception
158159
*/';
159160

@@ -316,6 +317,7 @@ public function testGetResponseInfoFromDocBlock(): void
316317
// TODO - Update to use resource file and/or dataprovider to test more than one comment block
317318
$expected = [
318319
'type' => 'array',
320+
'description' => 'Some test description for the return annotation.',
319321
];
320322
$this->assertEquals($expected, $this->annotationGenerator->getResponseInfoFromDocBlock(self::EXAMPLE_API_METHOD_DOC_BLOCK1));
321323
}

0 commit comments

Comments
 (0)