Skip to content

Make Container return template type with conditional return type #910

@alleknalle

Description

@alleknalle

Hi there,

I would like to follow up on #869 and created this new issue because the other one was closed.
I understand that you would like to be consistent with PSR-11, but the docblock from PHP-DI is already different then the one from PSR-11, like MarcHagen states in #869 (comment)

PSR-11 -> https://github.com/php-fig/container/blob/master/src/ContainerInterface.php

/**
     * Finds an entry of the container by its identifier and returns it.
     *
     * @param string $id Identifier of the entry to look for.
     *
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
     * @throws ContainerExceptionInterface Error while retrieving the entry.
     *
     * @return mixed Entry.
     */
    public function get(string $id);

PHP-DI -> https://github.com/PHP-DI/PHP-DI/blob/master/src/Container.php

/**
     * Returns an entry of the container by its name.
     *
     * @template T
     * @param string|class-string<T> $id Entry name or a class name.
     *
     * @return mixed|T
     * @throws DependencyException Error while resolving the entry.
     * @throws NotFoundException No entry found for the given name.
     */
    public function get(string $id) : mixed

The one from PHP-DI is already better, because it adds class-string<T> to the param and T to the return type.
It would be very helpful to make the return type more specific like zonuexe did in his PR. And as improvement on that PR I would like to suggest to use @return ($id is class-string<T> ? T : mixed) like stated in #869 (comment)

I think PSR will never add psalm or phpstan specific return types, so the only place where we can make it more specific is in PHP-DI.

Another way would be to remove the docblock in PHP-DI, so people can use the Psalm and PHPStan stubs for the PSR interface like in https://github.com/laminas/laminas-mvc-i18n/blob/1.10.x/.psr-container.php.stub

Could you please take another look at it?

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