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

[NodeTypeResolver] Handle crash after next exit() on no namespaced code after removal next attribute #4074

Merged
merged 7 commits into from
Jun 5, 2023
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
Expand Up @@ -48,6 +48,8 @@
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -79,7 +81,8 @@ public function __construct(
private readonly ScopeFactory $scopeFactory,
private readonly PrivatesAccessor $privatesAccessor,
private readonly NodeNameResolver $nodeNameResolver,
private readonly ClassAnalyzer $classAnalyzer
private readonly ClassAnalyzer $classAnalyzer,
private readonly FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser
) {
$this->nodeTraverser = new NodeTraverser();

Expand Down Expand Up @@ -107,6 +110,14 @@ public function processNodes(
Assert::allIsInstanceOf($stmts, Stmt::class);
$this->nodeTraverser->traverse($stmts);

if (! $isScopeRefreshing) {
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);

if (count($stmts) === 1 && $stmts[0] instanceof FileWithoutNamespace) {
$stmts = $stmts[0]->stmts;
}
}

$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);

// skip chain method calls, performance issue: https://github.com/phpstan/phpstan/issues/254
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

// no namespace on purpose to cover crash on FileWithoutNamespace
exit();

// this part is needed to produce crash
echo 'some statement';

function run2()
{
preg_split('#a#', null);
}

?>
-----
<?php

// no namespace on purpose to cover crash on FileWithoutNamespace
exit();

// this part is needed to produce crash
echo 'some statement';

function run2()
{
preg_split('#a#', '');
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\NullToStrictStringFuncCallArgRector\Fixture;

exit();

echo 'some statement';

function run2()
{
preg_split('#a#', null);
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\NullToStrictStringFuncCallArgRector\Fixture;

exit();

echo 'some statement';

function run2()
{
preg_split('#a#', '');
}

?>
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v54';
private const CACHE_KEY = 'v55';

private ContainerInterface|null $container = null;

Expand Down