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

Cerrado
#9,955 1 comentario 0 reacciones 0 asignados Ver en GitHub

Ya se ha fusionado un pull request relacionado.

  • #818 de @samsonasik — fusionado

Evaluación

Dificultad
4/5
Tiempo estimado
1-2 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
php
Área
testing-qa

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
PHP
Estrellas
10.5k
Forks
742
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rectorphp/rector

Todos los issues de rectorphp/rector

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.