Skip to content

[PHPUnit] AddInstanceofAssertForNullableInstanceRector enters an infinite loop on a test trait #9955

Closed
rectorphp/rector-phpunit
#818

Description

@Wohlie

Bug Report

Subject Details
Rector version 2.8.3, confirmed with vendor/bin/rector --version
PHP version 8.4.27

AddInstanceofAssertForNullableInstanceRector repeatedly inserts the same assertInstanceOf() call when processing a public test method in a trait. This happens within a single Rector invocation, not just across subsequent CLI runs. Rector never reaches a stable result.

I reproduced this with a fresh installation of rector/rector:2.8.3 and only this rule enabled. The input uses PHP's built-in Throwable interface and contains no project-specific code.

Minimal PHP Code Causing Issue

input.php:

<?php
trait T
{
    function test(Throwable $e)
    {
        $p = $e->getPrevious();
        $p->getMessage();
    }
}

rector.php:

<?php

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
    ->withRules([
        AddInstanceofAssertForNullableInstanceRector::class,
    ]);

Run with a hard deadline to avoid leaving the process running indefinitely:

vendor/bin/rector --version
# Rector 2.8.3

timeout -s KILL 10 vendor/bin/rector process input.php \
    --config=rector.php \
    --dry-run --debug --no-progress-bar --clear-cache

The uninstrumented run did not finish before the deadline and was killed with exit code 137.

Expected Behaviour

Rector should add a single assertion before $p->getMessage() and terminate:

$p = $e->getPrevious();
$this->assertInstanceOf(\Throwable::class, $p);
$p->getMessage();

The transformation should be idempotent: processing the transformed code again should not insert another identical assertion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions