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

Revert "[Printer] Remove AlwaysRememberedExpr check on BetterStandardPrinter" #6517

Merged
merged 1 commit into from
Nov 27, 2024

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik enabled auto-merge (squash) November 27, 2024 10:10
@samsonasik samsonasik merged commit 8d33d50 into main Nov 27, 2024
36 checks passed
@samsonasik samsonasik deleted the revert-6484-remove-always-remembered-expr branch November 27, 2024 10:11
@ondrejmirtes
Copy link
Contributor

This bug will return for sure if you don't do what I tell you:

Class Rector\PhpParser\Printer\BetterStandardPrinter should extended PHPStan's Printer which has this method.

This is PHPStan's Printer class: https://github.com/phpstan/phpstan-src/blob/2.0.x/src/Node/Printer/Printer.php

@samsonasik
Copy link
Member Author

@ondrejmirtes, @TomasVotruba don't want to extends PHPStan printer, see discussion of it #4305 (review)

@samsonasik
Copy link
Member Author

samsonasik commented Nov 27, 2024

Also, as PHPStan printer now final, so can't extends, so we can't do special node printing handling base on attribute kind that node have after node changed if we use PHPStan printer.

@ondrejmirtes
Copy link
Contributor

I could un-final it in PHPStan. I want to be free to add more virtual nodes in PHPStan. Rector will inevitably break in the future because of this.

@samsonasik
Copy link
Member Author

I will let @TomasVotruba decide, we have node visitor which unwrap AlwaysRememberedExpr (this node only)

final class WrappedNodeRestoringNodeVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node): ?Node
{
if (! $node instanceof AlwaysRememberedExpr) {
return null;
}
$expr = $node;
while ($expr instanceof AlwaysRememberedExpr) {
$expr = $expr->getExpr();
}
return $expr;
}

that called after processNodes()

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
if ($hasUnreachableStatementNode) {
$nodeTraverser->addVisitor(new UnreachableStatementNodeVisitor($this, $filePath, $scope));
}
$nodeTraverser->traverse($stmts);
return $stmts;

this works well, until on specific circumtance, it somehow overlapped with what PHPStan apply during scan process, so this failsafe check instanceof AlwaysRememberedExpris needed in the BetterStandardPrinter:

while ($node instanceof AlwaysRememberedExpr) {
$node = $node->getExpr();
}

that's can't be tested with unit test unfortunatelly.

@TomasVotruba
Copy link
Member

We can give it a try :) we'll choose whichever option causes less maintenance

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this pull request Nov 27, 2024
@ondrejmirtes
Copy link
Contributor

Done phpstan/phpstan-src@1abeeb7

@samsonasik
Copy link
Member Author

@ondrejmirtes I tried it, it seems after $this->nodeScopeResolver->processNodes(), it seems still needs unwrap, otherwise, it will still got error:

Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\OptionalParametersAfterRequiredRectorTest::test with data set #7 ('/Users/samsonasik/www/rector-...hp.inc')
assert($subStartPos >= 0 && $subEndPos >= 0)

/Users/samsonasik/www/rector-src/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php:688
/Users/samsonasik/www/rector-src/src/PhpParser/Printer/BetterStandardPrinter.php:116

see PR:

@ondrejmirtes
Copy link
Contributor

Sorry for mystifying here, the Printer class from PHPStan isn't usable for you. Making it final again.

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.

3 participants