Skip to content

Duplicate types for PHPUnit data providers #136

@christeredvartsen

Description

@christeredvartsen

I have a question regarding the need to duplicate the types specified in @return in a data provider. Given the following test:

<?php declare(strict_types=1);
namespace MyNamespace;

use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
    /**
     * @return array<string,array{data:array<string,string>}>
     */
    public static function getData(): array
    {
        return [
            'some data' => [
                'data' => [
                    'foo' => 'bar',
                    'bar' => 'foo',
                ],
            ],
        ];
    }

    /**
     * @dataProvider getData
     */
    public function testSomething(array $data): void 
    {
        foreach ($data as $key => $value) {
            // do some assertions
        }
    }
}

If I run psalm with an errorLevel set to 1 on this code I get the following error:

ERROR: MixedAssignment - tests/SomeTest.php:28:35 - Unable to determine the type that $value is being assigned to (see https://psalm.dev/032)
        foreach ($data as $key => $value) {

I thought psalm might get this information from the @return docblock in the data provider. Do I need to duplicate the type for array $data as $param array< ... > $data on the testSomething method, or am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions