Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate types for PHPUnit data providers #136

Open
christeredvartsen opened this issue Mar 22, 2023 · 0 comments
Open

Duplicate types for PHPUnit data providers #136

christeredvartsen opened this issue Mar 22, 2023 · 0 comments

Comments

@christeredvartsen
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant