use single string type, no array is needed

This commit is contained in:
Tomas Votruba 2018-04-29 22:35:02 +02:00
parent 2c0b50473a
commit ef9472f95b
4 changed files with 30 additions and 25 deletions

View File

@ -81,7 +81,7 @@ final class ParamTypeResolver implements PerNodeTypeResolverInterface, NodeTypeR
}
// resolve to FQN
$paramType = $this->namespaceAnalyzer->resolveTypeToFullyQualified([$paramType], $paramNode);
$paramType = $this->namespaceAnalyzer->resolveTypeToFullyQualified($paramType, $paramNode);
if ($paramType) {
$this->typeContext->addVariableWithTypes($variableName, [$paramType]);
}

View File

@ -197,7 +197,7 @@ final class DocBlockAnalyzer
$fullyQualifiedTypes = [];
foreach (explode('|', $typesAsString) as $type) {
$fullyQualifiedTypes[] = $this->namespaceAnalyzer->resolveTypeToFullyQualified([$type], $node);
$fullyQualifiedTypes[] = $this->namespaceAnalyzer->resolveTypeToFullyQualified($type, $node);
}
return $fullyQualifiedTypes;
@ -309,7 +309,7 @@ final class DocBlockAnalyzer
}
if ($typeNode instanceof IdentifierTypeNode) {
$fqnType = $this->namespaceAnalyzer->resolveTypeToFullyQualified([$typeNode->name], $this->node);
$fqnType = $this->namespaceAnalyzer->resolveTypeToFullyQualified($typeNode->name, $this->node);
if (is_a($fqnType, $oldType, true)) {
return new IdentifierTypeNode($newType);
}

View File

@ -25,30 +25,13 @@ final class NamespaceAnalyzer
$this->typeAnalyzer = $typeAnalyzer;
}
/**
* @param string[] $types
*/
public function resolveTypeToFullyQualified(array $types, Node $node): string
public function resolveTypeToFullyQualified(string $type, Node $node): string
{
/** @var Use_[] $useNodes */
$useNodes = (array) $node->getAttribute(Attribute::USE_NODES);
foreach ($useNodes as $useNode) {
$useUseNode = $useNode->uses[0];
$nodeUseName = $useUseNode->name->toString();
foreach ($types as $type) {
if (Strings::endsWith($nodeUseName, '\\' . $type)) {
return $nodeUseName;
}
// alias
if ($useUseNode->getAlias() && $type === $useUseNode->getAlias()->toString()) {
return $nodeUseName;
}
}
$useStatementMatch = $this->matchUseStatements($type, (array) $node->getAttribute(Attribute::USE_NODES));
if ($useStatementMatch) {
return $useStatementMatch;
}
$type = array_pop($types);
if ($this->typeAnalyzer->isPhpReservedType($type)) {
return $type;
}
@ -62,4 +45,26 @@ final class NamespaceAnalyzer
return ($namespace ? $namespace . '\\' : '') . $type;
}
/**
* @param Use_[] $useNodes
*/
private function matchUseStatements(string $type, array $useNodes): ?string
{
foreach ($useNodes as $useNode) {
$useUseNode = $useNode->uses[0];
$nodeUseName = $useUseNode->name->toString();
if (Strings::endsWith($nodeUseName, '\\' . $type)) {
return $nodeUseName;
}
// alias
if ($useUseNode->getAlias() && $type === $useUseNode->getAlias()->toString()) {
return $nodeUseName;
}
}
return null;
}
}

View File

@ -114,7 +114,7 @@ CODE_SAMPLE
$classMethodNode = $parentNode->getAttribute(Attribute::PARENT_NODE);
$oldType = $this->namespaceAnalyzer->resolveTypeToFullyQualified(
[(string) $nullableTypeNode->type],
(string) $nullableTypeNode->type,
$nullableTypeNode
);
$this->docBlockAnalyzer->changeType($classMethodNode, $oldType, $newType);