-
Notifications
You must be signed in to change notification settings - Fork 27
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
$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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: Please ensure that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?type is parsed as NullableType |
||
$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; | ||
} | ||
} |
There was a problem hiding this comment.
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.