Skip to content

Commit

Permalink
Handle AlwaysRememberedExpr in deep Expr (#4310)
Browse files Browse the repository at this point in the history
* add test fixture for match print

* Fix deep AlwaysRememberedExpr

* clean up

* clean up

---------

Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
  • Loading branch information
samsonasik and TomasVotruba committed Jun 21, 2023
1 parent 6a8724c commit df459e7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ final class WrappedNodeRestoringNodeVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node): ?Node
{
if ($node instanceof AlwaysRememberedExpr) {
return $node->getExpr();
if (! $node instanceof AlwaysRememberedExpr) {
return null;
}

return null;
$expr = $node;
while ($expr instanceof AlwaysRememberedExpr) {
$expr = $expr->getExpr();
}

return $expr;
}
}
26 changes: 26 additions & 0 deletions tests/Issues/PrintMatchPhpstan/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Core\Tests\Issues\PrintMatchPhpstan\Fixture;

use ReflectionParameter;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\Config\Loader\FileLoader;

final class DefinitionFileLoader
{
/**
* @param ReflectionParameter[] $parameters
*/
private function executeCallback(array $parameters, $configurator): void
{
$arguments = [];
foreach ($parameters as $parameter) {
$reflectionType = $parameter->getType();

$arguments[] = match ($reflectionType->getName()) {
DefinitionConfigurator::class => $configurator,
FileLoader::class, self::class => $this,
};
}
}
}
28 changes: 28 additions & 0 deletions tests/Issues/PrintMatchPhpstan/PrintMatchPhpstanTest.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\PrintMatchPhpstan;

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

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

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

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
10 changes: 10 additions & 0 deletions tests/Issues/PrintMatchPhpstan/config/configured_rule.php
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\Set\ValueObject\DowngradeLevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(DowngradeLevelSetList::DOWN_TO_PHP_81);
};

0 comments on commit df459e7

Please sign in to comment.