Internal error in EmptyDataProviderRule with a named argument in `#[DataProvider]` (2.1.3)

未關閉 適合新手
#361 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
63/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
php
領域
testing

研究方向

從 src/Rules/PHPUnit/EmptyDataProviderRule.php 開始,查看 processNode 呼叫 isDataProvider 的位置(約第 75 和 100 行),並檢查 DataProviderHelper::yieldDataProviderAttributes 如何讀取第一個屬性參數。使用 issue 中的 ReproTest.php 程式片段重現問題,採用 #[DataProvider(methodName: 'provideValues')],並包含 bleedingEdge.neon。當使用具名參數的情況不再觸發 strcasecmp 內部錯誤,且有測試涵蓋該情況時,即完成。

由索引模型根據 Issue 內容生成。

描述

We know that PHPUnit marks its attributes with @no-named-arguments, so #[DataProvider(methodName: '...')] is not covered by its BC promise and this may well be on our side. Still, it worked in 2.1.2 and now PHPStan stops with an internal error instead of a reported error, so I wanted to let you know.

Versions: phpstan/phpstan-phpunit 2.1.3 (2.1.2 is fine), phpstan/phpstan 2.3.7, phpunit/phpunit 13.4.8, PHP 8.5.13. It happens only with bleedingEdge.neon included.

phpstan.neon:

includes:
    - vendor/phpstan/phpstan-phpunit/extension.neon
    - vendor/phpstan/phpstan-phpunit/rules.neon
    - phar://vendor/phpstan/phpstan/phpstan.phar/conf/bleedingEdge.neon
parameters:
    level: 0
    paths:
        - tests

tests/ReproTest.php:

<?php

declare(strict_types=1);

namespace Repro;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class ReproTest extends TestCase
{
    #[DataProvider(methodName: 'provideValues')]
    public function testValue(int $value): void
    {
        if ($value === 0) {
            return;
        }

        self::assertSame(1, $value);
    }

    /**
     * @return list<array{int}>
     */
    public static function provideValues(): array
    {
        return [[0], [1]];
    }
}

Result:

Internal error: strcasecmp(): Argument #1 ($string1) must be of type string, null given while analysing file /tmp/repro/tests/ReproTest.php
#0 vendor/phpstan/phpstan-phpunit/src/Rules/PHPUnit/EmptyDataProviderRule.php(100): strcasecmp()
#1 vendor/phpstan/phpstan-phpunit/src/Rules/PHPUnit/EmptyDataProviderRule.php(75): PHPStan\Rules\PHPUnit\EmptyDataProviderRule->isDataProvider()
#2 phar://vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyserCallback.php(168): PHPStan\Rules\PHPUnit\EmptyDataProviderRule->processNode()

With #[DataProvider('provideValues')] there is no error.

It looks like #354 changed return [] to continue for a bare return;, so a method like testValue() now reaches isDataProvider(). There, DataProviderHelper::yieldDataProviderAttributes() reads the attribute through reflection as $args[0], which is null when the argument is named (the key is methodName).

Skipping the attribute, or reading $args['methodName'] ?? $args[0] ?? null, would be enough for us. If you prefer to keep named arguments unsupported, that is fine too, we will switch to positional arguments.

主要語言
PHP
星號
547
分支
61
平均合併
1 天 9 小時
30 天內合併 PR
13

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

phpstan/phpstan-phpunit 的其他 Issue

查看 phpstan/phpstan-phpunit 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。