Skip to content

Commit

Permalink
[NodeTypeResolver] Handle crash after next exit() on no namespaced co…
Browse files Browse the repository at this point in the history
…de after removal next attribute (#4074)
  • Loading branch information
samsonasik committed Jun 5, 2023
1 parent 8e80c7a commit dab4482
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
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

0 comments on commit dab4482

Please sign in to comment.