Skip to content

[Psalm] ProviderInterface does not allow for arrays to be returned #5303

Closed
@nesl247

Description

@nesl247

API Platform version(s) affected: 3.0.7

Description
In

* @template T of object
the type is defined to be an object. Because of this, when running psalm, it complains if a provider returns an array, given that an array is not an object.

ERROR: InvalidTemplateParam - src/TestProvider.php:13:31 - Extended template param T expects type object, type array<array-key, mixed> given (see https://psalm.dev/183)
class TestProvider implements ProviderInterface

How to reproduce

<?php

declare(strict_types=1);

namespace App;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;

/**
 * @implements ProviderInterface<array>
 */
class TestProvider implements ProviderInterface
{
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
    {
        return [];
    }
}

Possible Solution
Change it to T of object|array. I'm not sure if this is incorrect, but I think it's valid, and it does fix the issue.

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions