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

Đang mở Phù hợp với người mới
#361 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
63/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
testing

Hướng nghiên cứu

Bắt đầu từ src/Rules/PHPUnit/EmptyDataProviderRule.php, nơi processNode gọi isDataProvider (khoảng dòng 75 và 100), rồi kiểm tra cách DataProviderHelper::yieldDataProviderAttributes đọc đối số đầu tiên của attribute. Tái hiện lỗi bằng đoạn ReproTest.php trong issue, dùng #[DataProvider(methodName: 'provideValues')] và có bao gồm bleedingEdge.neon. Hoàn thành khi trường hợp dùng đối số có tên không còn gây lỗi nội bộ strcasecmp và có một test bao phủ nó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
PHP
Star
547
Fork
61
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
13

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phpstan/phpstan-phpunit

Tất cả issue của phpstan/phpstan-phpunit

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.