sebastianbergmann/phpunit · error · InvalidDataProviderException

Data Provider method %s::%s() is not static

Error message

Data Provider method %s::%s() is not static

What it means

Thrown while resolving a data provider when the referenced method exists and is public but not static. PHPUnit calls providers as $className::$methodName() (static late binding) before any test instance exists, so ReflectionMethod::isStatic() is required.

Source

Thrown at src/Metadata/Api/DataProvider.php:139

                            $_dataProvider->methodName(),
                            $testMethod->getDeclaringClass()->getName(),
                            $testMethod->getName(),
                        ),
                    );
                }

                if (!$method->isPublic()) {
                    throw new InvalidDataProviderException(
                        sprintf(
                            'Data Provider method %s::%s() is not public',
                            $className,
                            $methodName,
                        ),
                    );
                }

                if (!$method->isStatic()) {
                    throw new InvalidDataProviderException(
                        sprintf(
                            'Data Provider method %s::%s() is not static',
                            $className,
                            $methodName,
                        ),
                    );
                }

                if ($method->getNumberOfParameters() > 0) {
                    throw new InvalidDataProviderException(
                        sprintf(
                            'Data Provider method %s::%s() expects an argument',
                            $className,
                            $methodName,
                        ),
                    );
                }

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Add the 'static' keyword to the provider method

Example fix

// before
#[DataProvider('provideCases')]
public function testX(int $n): void { ... }

public function provideCases(): array { ... }

// after
#[DataProvider('provideCases')]
public function testX(int $n): void { ... }

public static function provideCases(): array { ... }
Defensive patterns

Strategy: validation

Validate before calling

$method = new ReflectionMethod($testClass, 'provideCases');

if (!$method->isStatic()) {
    // add 'static' before running the suite
}

Type guard

function isValidDataProvider(ReflectionMethod $method): bool
{
    return $method->isPublic()
        && $method->isStatic()
        && $method->getNumberOfParameters() === 0;
}

Prevention

When it happens

Trigger: Declaring the provider as a public instance method: 'public function provideCases(): array' and referencing it via #[DataProvider('provideCases')].

Common situations: Converting older tests where providers were instance methods; IDE refactorings dropping 'static'; copying providers between instance-based helper classes.

Related errors


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/b10179f06c5acdd5. Report an issue: GitHub.