mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 19:53:14 +01:00
Fix scope overflow in AddDefaultValueForUndefinedVariableRector (#1557)
Fix scope overflow in AddDefaultValueForUndefinedVariableRector
This commit is contained in:
commit
ae833f1e82
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Stage constructor.
|
||||
*
|
||||
* @param string $stage
|
||||
* @param boolean $undefined Should the object be undefined?
|
||||
*/
|
@ -67,6 +67,7 @@ final class PhpDocInfoPrinterTest extends AbstractKernelTestCase
|
||||
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc12.txt'];
|
||||
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc13.txt'];
|
||||
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc14.txt'];
|
||||
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc15.txt'];
|
||||
}
|
||||
|
||||
public function provideMultiline(): Iterator
|
||||
|
9
packages/Php/src/Exception/BreakScopeException.php
Normal file
9
packages/Php/src/Exception/BreakScopeException.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class BreakScopeException extends Exception
|
||||
{
|
||||
}
|
@ -15,6 +15,7 @@ use PhpParser\Node\Stmt\StaticVar;
|
||||
use PhpParser\Node\Stmt\Unset_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Php\Exception\BreakScopeException;
|
||||
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
@ -90,30 +91,38 @@ CODE_SAMPLE
|
||||
{
|
||||
$this->undefinedVariables = [];
|
||||
|
||||
$this->callableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (Node $node) {
|
||||
if (! $node instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
$this->callableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (Node $node) {
|
||||
// entering new scope - break!
|
||||
if ($node instanceof FunctionLike) {
|
||||
throw new BreakScopeException();
|
||||
}
|
||||
|
||||
if ($this->shouldSkipVariable($node)) {
|
||||
return null;
|
||||
}
|
||||
if (! $node instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$variableName = $this->getName($node);
|
||||
if ($variableName === null) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipVariable($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// defined 100 %
|
||||
/** @var Scope $nodeScope */
|
||||
$nodeScope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
if ($nodeScope->hasVariableType($variableName)->yes()) {
|
||||
return null;
|
||||
}
|
||||
$variableName = $this->getName($node);
|
||||
if ($variableName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// @todo improve
|
||||
$this->undefinedVariables[] = $variableName;
|
||||
});
|
||||
// defined 100 %
|
||||
/** @var Scope $nodeScope */
|
||||
$nodeScope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
if ($nodeScope->hasVariableType($variableName)->yes()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->undefinedVariables[] = $variableName;
|
||||
});
|
||||
} catch (BreakScopeException $breakScopeException) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
if ($this->undefinedVariables === []) {
|
||||
return null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user