Skip to content

v7.0.10 container::get static type is misleading #913

@etienneroudeix

Description

@etienneroudeix

Hi there 👋

It is all about recently updated PHPdoc for Container::get

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

There is nothing preventing one to do $container->set(MyClass::class, "not my class type").

Therefore it is misleading pretending that $container->get(MyClass::class) will always return an instance of MyClass.

I had this unexpected behavior for I explicitly disable a class injection is a given context by doing $container->set(MyClass::class, null). And the class-name is still registered and getting it will return null and will not throw an exception.

With the v7.0.10, phpstan started telling my i do not need a nullsafe operand when using the $container->get(MyClass::class) result.

I would recommend reverting #912

Cheers and thank you for your work !

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