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

[CodeQuality] Handle crash on do {} while with first class callable on OptionalParametersAfterRequiredRector #6554

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;

final class SkipFirstClassCallableInDo
{
public function getSubscribedEvents()
{
do {

} while ($this->textElement(...));
}

public function textElement()
{
return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;

final class SkipFirstClassCallableInWhile
{
public function getSubscribedEvents()
{
while ($this->textElement(...)) {

}
}

public function textElement()
{
return 1;
}
}
15 changes: 14 additions & 1 deletion src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rector\NodeTypeResolver\PHPStan\Scope;

use Error;
use PHPStan\Node\Printer\Printer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
Expand Down Expand Up @@ -375,7 +377,18 @@ public function processNodes(
}
};

$this->nodeScopeResolverProcessNodes($stmts, $scope, $nodeCallback);
try {
$this->nodeScopeResolverProcessNodes($stmts, $scope, $nodeCallback);
} catch (Error $error) {
if (! str_starts_with($error->getMessage(), 'Call to undefined method ' . Printer::class . '::pPHPStan_')) {
throw $error;
}

// nothing we can do more precise here as error printing from deep internal PHPStan Printer service with service injection we cannot reset
// in the middle of process
// fallback to fill by found scope
RectorNodeScopeResolver::processNodes($stmts, $scope);
}

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
Expand Down
Loading