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

Aperta
#9,955 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

@samsonasik ci sta già lavorando.

Dal 3/11/2026.

  • #818 di @samsonasik — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
php
Ambito
testing-qa

Direzione di ricerca

Start with the AddInstanceofAssertForNullableInstanceRector class under the PHPUnit CodeQuality ClassMethod rules, and check how it handles methods inside a trait. Reproduce with the input.php and rector.php from the report, using a timeout so the loop cannot run forever. Done means one assertInstanceOf() is added, a second pass makes no change, and the run terminates. Check the linked open pull request first, since it may already cover this.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

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.

Lingua principale
PHP
Stelle
10.5k
Fork
742
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rectorphp/rector

Tutte le issue di rectorphp/rector

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.