Skip to content
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

[PHPStan 1.0] Remove node naming visitors and re-use PHPStan ones #1144

Merged
merged 7 commits into from
Nov 5, 2021

Conversation

TomasVotruba
Copy link
Member

No description provided.

@TomasVotruba TomasVotruba changed the title remove node naming visitors [PHPStan 1.0] Remove node naming visitors and re-use PHPStan ones Nov 4, 2021
@TomasVotruba TomasVotruba force-pushed the tv-traversing-less branch 20 times, most recently from ecda4e3 to ab88e1f Compare November 5, 2021 13:42
@TomasVotruba TomasVotruba force-pushed the tv-traversing-less branch 8 times, most recently from 07295c0 to e4597a6 Compare November 5, 2021 14:49
@TomasVotruba TomasVotruba force-pushed the tv-traversing-less branch 3 times, most recently from 93ae985 to fb751c3 Compare November 5, 2021 20:42
@TomasVotruba TomasVotruba force-pushed the tv-traversing-less branch 5 times, most recently from 345aaa1 to 1f3994e Compare November 5, 2021 22:38
Comment on lines 36 to -72
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NameResolver(null, [
self::OPTION_PRESERVE_ORIGINAL_NAMES => true,
// required by PHPStan
self::OPTION_REPLACE_NODES => true,
]));

/** @var Stmt[] $stmts */
$stmts = $nodeTraverser->traverse($stmts);

$smartFileInfo = $file->getSmartFileInfo();

$stmts = $this->phpStanNodeScopeResolver->processNodes($stmts, $smartFileInfo);

$nodeTraverserForPreservingName = new NodeTraverser();

$preservingNameResolver = new NameResolver(null, [
self::OPTION_PRESERVE_ORIGINAL_NAMES => true,
// this option would override old non-fqn-namespaced nodes otherwise, so it needs to be disabled
self::OPTION_REPLACE_NODES => false,
]);

$nodeTraverserForPreservingName->addVisitor($preservingNameResolver);
$stmts = $nodeTraverserForPreservingName->traverse($stmts);

Copy link
Member Author

Choose a reason for hiding this comment

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

Main point of this PR is to remove these 2 node name traversers. Why?

Pros

Cons

It has downside in losing rules that rely deply on original node names without overriding, mainly with alias imports in union. Thus 2 rules had to removed:

Yet, I think it's worth it, as we cut 3 traverses to 1 and based on shared names with PHPStan traversing, making complexity lower in general.

@@ -12,6 +12,6 @@ echo mysql_error();
namespace Rector\Core\Tests\Issues\Issue6675\Fixture;

$db = mysqli_connect("server","user","password");
echo mysqli_error($db);
echo mysql_error();
Copy link
Member

Choose a reason for hiding this comment

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

previous code is already correct as it migrate mysql to mysqli

}

return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr);
}
Copy link
Member

@samsonasik samsonasik Nov 5, 2021

Choose a reason for hiding this comment

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

This cause invalid result on mysql to mysqli on multiple rules applied like in my previous comment

public function run(): SharedShortName
{
}
}
Copy link
Member

Choose a reason for hiding this comment

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

this expected output seems previously correct, as the docblock @return changed to short name, the "skip" here means skipping from re-import

@TomasVotruba
Copy link
Member Author

TomasVotruba commented Nov 5, 2021

Thanks for feedback. Could you try restore behavior of those fixture after I merge this PR? I failed to do so due to changes caused by switching to PHPStan's NameResolver. I've put in last 2 days and I'm bit frustrated with it.

@TomasVotruba TomasVotruba merged commit 2304666 into main Nov 5, 2021
@TomasVotruba TomasVotruba deleted the tv-traversing-less branch November 5, 2021 23:06
@@ -31,7 +31,7 @@ use Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\Source\
class MockPropertiesTest extends \PHPUnit\Framework\TestCase
{
private \Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\Fixture\MockProperties $mockProperties;
private \PHPUnit\Framework\MockObject\MockObject|\Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\Source\OrderFactory $factory;
private \OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory;
Copy link
Member

Choose a reason for hiding this comment

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

this is incorrect as OrderFactory is from Source namespace from use statement

@samsonasik
Copy link
Member

@TomasVotruba I will try.

@@ -67,7 +67,7 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class PageLinksGeneratorTest extends \PHPUnit\Framework\TestCase
{
private \Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\Fixture\PageLinksGenerator $pageLinksGenerator;
private \PHPUnit\Framework\MockObject\MockObject|\Symfony\Bundle\FrameworkBundle\Routing\Router $router;
private \PHPUnit\Framework\MockObject\MockObject|\Router $router;
Copy link
Member

@samsonasik samsonasik Nov 6, 2021

Choose a reason for hiding this comment

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

this is also incorrect for \Router as the namespace Symfony\Bundle\FrameworkBundle\Routing\Router imported in use statement.

@@ -64,7 +64,7 @@ use PhpSpec\ObjectBehavior;
class RatesTest extends \PHPUnit\Framework\TestCase
{
private \Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\Fixture\Rates $rates;
private \PHPUnit\Framework\MockObject\MockObject|\spec\Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\Fixture\Provider $provider;
private \PHPUnit\Framework\MockObject\MockObject|\Provider $provider;
Copy link
Member

@samsonasik samsonasik Nov 6, 2021

Choose a reason for hiding this comment

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

this should be Provider

};
}
}
?>
Copy link
Member

Choose a reason for hiding this comment

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

this is incorrect and the behaviour need to be kept

@@ -70,7 +70,7 @@ class CreateMeTest extends \PHPUnit\Framework\TestCase

public function testCalled()
{
/** @var spec\Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\Fixture\Cart|\PHPUnit\Framework\MockObject\MockObject $cart */
/** @var Cart|\PHPUnit\Framework\MockObject\MockObject $cart */
Copy link
Member

Choose a reason for hiding this comment

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

this is incorrect, same with property to use namespaced name, the @var should use namespaced as well as namespace changed.

Copy link
Member

Choose a reason for hiding this comment

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

or possibly not, it depends on if actually the class used moved or not after namespace changed, as next this->createMock() uses Cart::class, it technically doesn't break as it doesn't add \ prefix.

it is ok then for now.

Copy link
Member

Choose a reason for hiding this comment

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

the outside class type actualy need to be moved under Source to avoid miss naming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants