Skip to content

Commit

Permalink
Make use of toPhpDocNode() - take #5 (#4567)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jul 21, 2023
1 parent efce1c7 commit 2e0ce28
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\StaticTypeMapper\PhpDocParser\IdentifierTypeMapper;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,7 @@ public function getNodeClass(): string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
if ($type instanceof ParentObjectWithoutClassType) {
return new IdentifierTypeNode('parent');
}

if ($type instanceof TemplateObjectWithoutClassType) {
$attributeAwareIdentifierTypeNode = new IdentifierTypeNode($type->getName());
return new EmptyGenericTypeNode($attributeAwareIdentifierTypeNode);
}

// special case for anonymous classes that implement another type
if ($type instanceof ObjectWithoutClassTypeWithParentTypes) {
$parentTypes = $type->getParentTypes();
if (count($parentTypes) === 1) {
$parentType = $parentTypes[0];
return new FullyQualifiedIdentifierTypeNode($parentType->getClassName());
}
}

return new IdentifierTypeNode('object');
return $type->toPhpDocNode();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions packages/StaticTypeMapper/PhpDocParser/IdentifierTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $name
return $this->mapIdentifierTypeNode($typeNode, $node);
}

public function mapIdentifierTypeNode(IdentifierTypeNode $typeNode, Node $node): Type
public function mapIdentifierTypeNode(IdentifierTypeNode $identifierTypeNode, Node $node): Type
{
$type = $this->scalarStringToTypeMapper->mapScalarStringToType($typeNode->name);
$type = $this->scalarStringToTypeMapper->mapScalarStringToType($identifierTypeNode->name);
if (! $type instanceof MixedType) {
return $type;
}
Expand All @@ -68,7 +68,7 @@ public function mapIdentifierTypeNode(IdentifierTypeNode $typeNode, Node $node):
return $type;
}

$loweredName = strtolower($typeNode->name);
$loweredName = strtolower($identifierTypeNode->name);
if ($loweredName === ObjectReference::SELF) {
return $this->mapSelf($node);
}
Expand All @@ -85,17 +85,17 @@ public function mapIdentifierTypeNode(IdentifierTypeNode $typeNode, Node $node):
return new IterableType(new MixedType(), new MixedType());
}

if (str_starts_with($typeNode->name, '\\')) {
$typeWithoutPreslash = Strings::substring($typeNode->name, 1);
if (str_starts_with($identifierTypeNode->name, '\\')) {
$typeWithoutPreslash = Strings::substring($identifierTypeNode->name, 1);
$objectType = new FullyQualifiedObjectType($typeWithoutPreslash);
} else {
if ($typeNode->name === 'scalar') {
if ($identifierTypeNode->name === 'scalar') {
// pseudo type, see https://www.php.net/manual/en/language.types.intro.php
$scalarTypes = [new BooleanType(), new StringType(), new IntegerType(), new FloatType()];
return new UnionType($scalarTypes);
}

$objectType = new ObjectType($typeNode->name);
$objectType = new ObjectType($identifierTypeNode->name);
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
Expand Down

0 comments on commit 2e0ce28

Please sign in to comment.