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

Aperta Adatta ai principianti
#361 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
63/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
testing

Direzione di ricerca

Inizia da src/Rules/PHPUnit/EmptyDataProviderRule.php, dove processNode chiama isDataProvider (intorno alle righe 75 e 100), e verifica come DataProviderHelper::yieldDataProviderAttributes legge il primo argomento dell'attributo. Riproduci il problema con lo snippet ReproTest.php della issue, usando #[DataProvider(methodName: 'provideValues')] e includendo bleedingEdge.neon. È completato quando il caso con argomento nominato non provoca più l'errore interno strcasecmp e un test lo copre.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
PHP
Stelle
547
Fork
61
Merge medio
1g 9h
PR unite (30g)
13

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di phpstan/phpstan-phpunit

Tutte le issue di phpstan/phpstan-phpunit

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.