-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
Conversation
ecda4e3
to
ab88e1f
Compare
07295c0
to
e4597a6
Compare
93ae985
to
fb751c3
Compare
345aaa1
to
1f3994e
Compare
{ | ||
$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); | ||
|
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.
Main point of this PR is to remove these 2 node name traversers. Why?
Pros
-
They already happen in PHPStan parser that we now use since PHPStan 1.0
-
Here are used to work around PHPStan changing names without keeping the originals, without those, Rector would print nodes in FQN forms everywhere
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:
- [CodeQuality] Remove FixClassCaseSensitivityNameRector, conflicting with name node removal and edge case already handled by PHPStan reports #1160
- [CodingStyle] Remove RemoveUnusedAliasRector, job rather for coding standard tool and opinonated #1157
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.
1f3994e
to
063002e
Compare
@@ -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(); |
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.
previous code is already correct as it migrate mysql to mysqli
} | ||
|
||
return $this->exprUsedInNodeAnalyzer->isUsed($node, $expr); | ||
} |
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.
This cause invalid result on mysql to mysqli on multiple rules applied like in my previous comment
public function run(): SharedShortName | ||
{ | ||
} | ||
} |
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.
this expected output seems previously correct, as the docblock @return
changed to short name, the "skip" here means skipping from re-import
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. |
@@ -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; |
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.
this is incorrect as OrderFactory
is from Source
namespace from use statement
@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; |
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.
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; |
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.
this should be Provider
}; | ||
} | ||
} | ||
?> |
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.
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 */ |
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.
this is incorrect, same with property to use namespaced name, the @var
should use namespaced as well as namespace changed.
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.
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.
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.
the outside class type actualy need to be moved under Source
to avoid miss naming
No description provided.