psalm-plugin-phpunit icon indicating copy to clipboard operation
psalm-plugin-phpunit copied to clipboard

Duplicate types for PHPUnit data providers

Open christeredvartsen opened this issue 2 years ago • 0 comments

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?

christeredvartsen avatar Mar 22 '23 12:03 christeredvartsen