Skip to content

Commit

Permalink
[Downgradephp81][DowngradePhp80] Handle No scope crash on DowngradeSe…
Browse files Browse the repository at this point in the history
…tAccessibleReflectionPropertyRector + DowngradeMatchToSwitchRector (#5183)

* [Downgradephp81][DowngradePhp80] Handle No scope crash on DowngradeSetAccessibleReflectionPropertyRector + DowngradeMatchToSwitchRector

* fix

* clean up
  • Loading branch information
samsonasik authored Oct 17, 2023
1 parent 11c66a7 commit 369f16a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ private function isJustReprintedOverlappedTokenStart(Node $node, ?Node $original
return true;
}

return ! $node instanceof Stmt && $node->getAttributes() === [];
if ($node instanceof Stmt) {
return array_keys($node->getAttributes()) === [AttributeKey::STMT_KEY];
}

return $node->getAttributes() === [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rector\Core\Tests\Issues\ScopeNotAvailable\FixtureMatchToSwitchReflection;

final class ServiceValueResolver
{
public function resolve(Request $request, ArgumentMetadata $argument): array
{
try {

} catch (\RuntimeException $e) {

$r = new \ReflectionProperty($e, 'message');
$r->setValue($e, $message);

throw $e;
}
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\ScopeNotAvailable\FixtureMatchToSwitchReflection;

final class ServiceValueResolver
{
public function resolve(Request $request, ArgumentMetadata $argument): array
{
try {

} catch (\RuntimeException $e) {

$r = new \ReflectionProperty($e, 'message');
$r->setAccessible(true);
$r->setValue($e, $message);

throw $e;
}
}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/ScopeNotAvailable/MatchToSwitchReflectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\ScopeNotAvailable;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MatchToSwitchReflectionTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureMatchToSwitchReflection');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/match_to_switch_reflection.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector;
use Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
DowngradeSetAccessibleReflectionPropertyRector::class,
DowngradeMatchToSwitchRector::class,
]);
};

0 comments on commit 369f16a

Please sign in to comment.