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

已关闭
#9,955 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

已经有一个关联 PR 被合并了。

  • #818 来自 @samsonasik —— 已合并

评估

难度
4/5
预计耗时
1-2 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
php
领域
testing-qa

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
PHP
星标
10.5k
派生
742
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rectorphp/rector 的其他 Issue

查看 rectorphp/rector 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。