-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Handle no scope crash on foreach value with array item variable (
#3068) * [Core] Handle no scope crash on foreach value with array item variable * [ci-review] Rector Rectify * [ci-review] Rector Rectify * Fixed 🎉 * raname fixture rector * phpstan * Final touch: check against exactly ArrayItem Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
fa9a953
commit 4140e78
Showing
6 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/Issues/ScopeNotAvailable/FixtureForeachValue/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Tests\Issues\ScopeNotAvailable\Fixture; | ||
|
||
class FixtureForeachValue | ||
{ | ||
public function run() | ||
{ | ||
$value = [ | ||
[3, 1, false], | ||
[1, 2, false], | ||
[0, 0, true], | ||
]; | ||
|
||
foreach ($value as [$a_b, $b_c, $d_e]) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Tests\Issues\ScopeNotAvailable; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ForeachValueScopeTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureForeachValue'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/foreach_value_configured.php'; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/Issues/ScopeNotAvailable/Variable/ArrayItemForeachValueRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Tests\Issues\ScopeNotAvailable\Variable; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use Rector\Core\Rector\AbstractScopeAwareRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class ArrayItemForeachValueRector extends AbstractScopeAwareRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Hello!', [new CodeSample('', '')]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Expr>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Variable::class]; | ||
} | ||
|
||
public function refactorWithScope(Node $node, Scope $scope): Node | ||
{ | ||
return $node; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Issues/ScopeNotAvailable/config/foreach_value_configured.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Core\Tests\Issues\ScopeNotAvailable\Variable\ArrayItemForeachValueRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ArrayItemForeachValueRector::class); | ||
}; |