Skip to content

[Arrows] MC-21844: SVC false-positive: short names & FQCN in DockBlock #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/Analyzer/ClassMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,20 +422,16 @@ private function isReturnsEqualByNullability(ClassMethod $before, ClassMethod $a
*/
private function getDocReturnDeclaration(ClassMethod $method)
{
if ($method->getDocComment() !== null) {
$lexer = new Lexer();
$typeParser = new TypeParser();
$constExprParser = new ConstExprParser();
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);

$tokens = $lexer->tokenize((string)$method->getDocComment());
$tokenIterator = new TokenIterator($tokens);
$phpDocNode = $phpDocParser->parse($tokenIterator);
$tags = $phpDocNode->getTagsByName('@return');
/** @var PhpDocTagNode $tag */
$tag = array_shift($tags);
if (
($parsedComment = $method->getAttribute('docCommentParsed'))
&& isset($parsedComment['return'])
) {
$result = implode('|', $parsedComment['return']);

return $result;
} else {
return ' ';
}
return isset($tag) ? (string)$tag->value : ' ';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Scanner/ScannerRegistryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Magento\SemanticVersionChecker\Visitor\ApiTraitVisitor;
use PhpParser\Lexer\Emulative;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use Magento\SemanticVersionChecker\Visitor\NameResolver;
use PhpParser\Parser\Php7 as Parser;
use PHPSemVerChecker\Registry\Registry;
use PHPSemVerChecker\Visitor\ClassVisitor;
Expand Down
155 changes: 155 additions & 0 deletions src/Visitor/NameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SemanticVersionChecker\Visitor;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitor\NameResolver as ParserNameResolver;
use PhpParser\BuilderHelpers;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;

/**
* Extended Name Resolver that parse and resolve also docblock hintings
*/
class NameResolver extends ParserNameResolver
{
/**
* @inheritDoc
*/
public function enterNode(Node $node)
{
$return = parent::enterNode($node);

if ($node instanceof ClassMethod) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Property, ClassConst also may have annotation that describes a type. For Magento this is a less important cases and may be addressed in separate PRs.

$this->resolveDocBlockParamTypes($node);
}

return $return;
}

/**
* @param ClassMethod $node
* @return void
*/
private function resolveDocBlockParamTypes(ClassMethod $node)
{
/** @var PhpDocNode $docNode */
$docNode = $this->getParsedDocNode($node);
if ($docNode) {
$result = [];
/** @var ParamTagValueNode[] $paramTags */
$paramTags = $docNode->getParamTagValues();
/** @var ParamTagValueNode $paramTag */
foreach ($paramTags as $paramTag) {
$paramNode = [
'name' => $paramTag->parameterName ?? '',
'type' => $this->parseType($paramTag->type),
];
$result['params'][] = $paramNode;
}

/** @var ReturnTagValueNode[] $returnTags */
$returnTags = $docNode->getReturnTagValues();
/** @var ReturnTagValueNode $returnTag */
$returnTag = array_shift($returnTags);
if ($returnTag) {
$result['return'] = $this->parseType($returnTag->type);
}
$node->setAttribute('docCommentParsed', $result);
}
}

/**
* Parse param or return type into array of resolved types
*
* @param TypeNode $type
* @return array
*/
private function parseType($type)
{
$result = [];
if ($type instanceof UnionTypeNode) {
foreach ($type->types as $typeNode) {
$normalizedType = BuilderHelpers::normalizeType((string)$typeNode);
$resolvedType = $this->resolveType($normalizedType);
$result[] = $resolvedType;
}
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Please ensure that PHPStan\PhpDocParser\Ast\Type\NullableTypeNode should not be handled as special case (equals to union null|type)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?type is parsed as NullableType
type|null is parsed as UnionType
So I handle only UnionType case as list of FullyQualified types because there can be also annotation like that
Product|int|null

$normalizedType = BuilderHelpers::normalizeType((string)$type);
$resolvedType = $this->resolveType($normalizedType);
$result[] = $resolvedType;
}

uasort(
$result,
function ($elementOne, $elementTwo) {
return ((string)$elementOne < (string)$elementTwo) ? -1 : 1;
}
);

return $result;
}

/**
* Resolve type from Relative to FQCN
*
* @param $node
* @return Name|Node\NullableType|Node\UnionType
*/
private function resolveType($node)
{
if ($node instanceof Name) {
return $this->resolveClassName($node);
}
if ($node instanceof Node\NullableType) {
$node->type = $this->resolveType($node->type);
return $node;
}
if ($node instanceof Node\UnionType) {
foreach ($node->types as &$type) {
$type = $this->resolveType($type);
}
return $node;
}
return $node;
}

/**
* Analyses the Method doc block and returns parsed node
*
* @param ClassMethod $method
* @return PhpDocNode|null
*/
private function getParsedDocNode(ClassMethod $method)
{
$docComment = $method->getDocComment();
if ($docComment !== null) {
$lexer = new Lexer();
$typeParser = new TypeParser();
$constExprParser = new ConstExprParser();
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);
$tokens = $lexer->tokenize((string)$docComment);
$tokenIterator = new TokenIterator($tokens);
$phpDocNode = $phpDocParser->parse($tokenIterator);

return $phpDocNode;
}

return null;
}
}