Skip to content

2 layer deep Anonymous class trait usage infinite loop #7913

@jgedarovich

Description

@jgedarovich

Bug Report

Subject Details
Rector versions 0.14.8 , 0.15.23
Php version 8.0.28

Minimal PHP Code Causing Issue

With the default generated rector.php:

<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/src',
    ]);

    // register a single rule
    $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

    // define sets of rules
    //    $rectorConfig->sets([
    //        LevelSetList::UP_TO_PHP_80
    //    ]);
};

and a valid php file of the form of test.php:

<?php
// test.php
trait RectorTest {
    public function blah()
    {
        return (new class() {
            public function foo()
            {
                return (new class() {
                    use RectorTest;
                });
            }
        });
    }
}

when running rector process against the aformentioned test.php with the default configuration:

rector process test.php

the rector process hangs until operating system oomkills it

Expected Behaviour

that rector processes the file successfully (ok rector is done message)

Additional notes

  • If you comment out the line use RectorTest; of test.php, and re-run, rector is able to successfully process the nodes.
  • use RectorTest; also doesn't trip up rector if it's used in the first anonymous class returned, unlike test.php which is one layer of depth deeper (a trait that returns an anonymous class that uses the original trait, vs a trait that returns an anonymous class that has a method that returns ANOTHER anonymous class that uses the original trait) maybe just read the code 😂
  • phpstan-1.10.7.phar and phpstan-1.10.14.phar have no issue when analyze test.php is ran, we thus far suspect that perhaps the callback in https://github.com/rectorphp/rector/blob/main/packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php#L134-L145 that is given to phpstans processStmtNode method of phpstan/phpstan/modified/src/Analyser/NodeScopeResolver.php to play a role. or perhaps something in https://github.com/ondrejmirtes/BetterReflection 🤔 🤷

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions