Skip to content

Commit

Permalink
[AutoImport] Do not add cast on valid Integer type on aliased Name No…
Browse files Browse the repository at this point in the history
…de on auto import enabled (#6237)

* [AutoImport] Do not add cast on valid Integer type on aliased Name Node on auto import enabled

* Fix

* Fix
  • Loading branch information
samsonasik committed Aug 16, 2024
1 parent 174adf1 commit 54bf439
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

/**
* @implements NodeTypeResolverInterface<StaticCall|MethodCall>
Expand Down Expand Up @@ -72,6 +74,10 @@ public function resolve(Node $node): Type
$callerType = $this->nodeTypeResolver->getType($node->class);
}

if ($callerType instanceof AliasedObjectType) {
$callerType = new FullyQualifiedObjectType($callerType->getFullyQualifiedName());
}

foreach ($callerType->getObjectClassReflections() as $objectClassReflection) {
$classMethodReturnType = $this->resolveClassMethodReturnType(
$objectClassReflection,
Expand Down
2 changes: 2 additions & 0 deletions src/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rector\CodingStyle\Node\NameImporter;
use Rector\Naming\Naming\AliasNameResolver;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Guard\AddUseStatementGuard;

final class NameImportingPostRector extends AbstractPostRector
Expand Down Expand Up @@ -46,6 +47,7 @@ public function enterNode(Node $node): Node|int|null
// make use of existing use import
$nameInUse = $this->resolveNameInUse($node, $currentUses);
if ($nameInUse instanceof Name) {
$nameInUse->setAttribute(AttributeKey::NAMESPACED_NAME, $node->toString());
return $nameInUse;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\Issues\AutoImport\Fixture;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Rector\Tests\Issues\AutoImport\Source\SomeClass as SomeClass1;
use Fixture\SomeClass;

class DoNotAddCastValidIntTypeFromNameAliased extends Command
{
private ?OutputInterface $outputInterface = null;

protected function execute(InputInterface $input, OutputInterface $output): int
{
return \Rector\Tests\Issues\AutoImport\Source\SomeClass::zero();
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\AutoImport\Fixture;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Rector\Tests\Issues\AutoImport\Source\SomeClass as SomeClass1;
use Fixture\SomeClass;

class DoNotAddCastValidIntTypeFromNameAliased extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
return SomeClass1::zero();
}
}

?>

0 comments on commit 54bf439

Please sign in to comment.