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

Offen Anfängerfreundlich
#361 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
63/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
php
Bereich
testing

Rechercherichtung

Beginne in src/Rules/PHPUnit/EmptyDataProviderRule.php, wo processNode isDataProvider aufruft (etwa in den Zeilen 75 und 100), und prüfe, wie DataProviderHelper::yieldDataProviderAttributes das erste Attributargument liest. Reproduziere das Problem mit dem Snippet aus ReproTest.php aus dem Issue, mit #[DataProvider(methodName: 'provideValues')] und eingebundener bleedingEdge.neon. Fertig ist es, wenn der Fall mit benanntem Argument keinen strcasecmp-internen Fehler mehr auslöst und ein Test ihn abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
PHP
Sterne
547
Forks
61
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
13

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus phpstan/phpstan-phpunit

Alle Issues in phpstan/phpstan-phpunit

Ähnliche Issues

Weitere Issues zu PHP

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.