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

Open Beginner friendly
#361 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
63/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
testing

Research direction

Start in src/Rules/PHPUnit/EmptyDataProviderRule.php, where processNode calls isDataProvider (around lines 75 and 100), and check how DataProviderHelper::yieldDataProviderAttributes reads the first attribute argument. Reproduce with the ReproTest.php snippet from the issue, using #[DataProvider(methodName: 'provideValues')] and bleedingEdge.neon included. Done when the named-argument case no longer triggers the strcasecmp internal error and a test covers it.

Written by the indexing model from the issue text.

Description

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.

Dominant language
PHP
Stars
547
Forks
61
Avg merge
1d 9h
Merged PRs (30d)
13

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-phpunit

All issues in phpstan/phpstan-phpunit

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.