Skip to content
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,6 @@
/**
* Stage constructor.
*
* @param string $stage
* @param boolean $undefined Should the object be undefined?
*/
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function provideDocFilesForPrint(): Iterator
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc12.txt'];
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc13.txt'];
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc14.txt'];
yield [__DIR__ . '/PhpDocInfoPrinterSource/doc15.txt'];
}

public function provideMultiline(): Iterator
Expand Down
9 changes: 9 additions & 0 deletions packages/Php/src/Exception/BreakScopeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Rector\Php\Exception;

use Exception;

final class BreakScopeException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -90,30 +91,38 @@ public function refactor(Node $node): ?Node
{
$this->undefinedVariables = [];

$this->callableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (Node $node) {
if (! $node instanceof Variable) {
return null;
}

if ($this->shouldSkipVariable($node)) {
return null;
}

$variableName = $this->getName($node);
if ($variableName === null) {
return null;
}

// defined 100 %
/** @var Scope $nodeScope */
$nodeScope = $node->getAttribute(AttributeKey::SCOPE);
if ($nodeScope->hasVariableType($variableName)->yes()) {
return null;
}

// @todo improve
$this->undefinedVariables[] = $variableName;
});
try {
$this->callableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (Node $node) {
// entering new scope - break!
if ($node instanceof FunctionLike) {
throw new BreakScopeException();
}

if (! $node instanceof Variable) {
return null;
}

if ($this->shouldSkipVariable($node)) {
return null;
}

$variableName = $this->getName($node);
if ($variableName === null) {
return null;
}

// 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;
Expand Down