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

Exclude class-string and string from normalization with Union type #7513

Closed
zonuexe opened this issue Jun 22, 2022 · 3 comments
Closed

Exclude class-string and string from normalization with Union type #7513

zonuexe opened this issue Jun 22, 2022 · 3 comments

Comments

@zonuexe
Copy link
Contributor

zonuexe commented Jun 22, 2022

Feature request

I've tried extending PSR-11 with a stub to create a conditional return type that returns an instance of a class name.

<?php

declare(strict_types=1);

namespace Psr\Container;

/**
 * Describes the interface of a container that exposes methods to read its entries.
 */
interface ContainerInterface
{
    /**
     * @template T
     * @phpstan-param string|class-string<T> $id
     * @phpstan-return ($id is class-string<T> ? T : mixed)
     */
    public function get(string $id);

    /**
     * @param string $id Identifier of the entry to look for.
     * @return bool
     */
    public function has(string $id);
}

https://phpstan.org/r/0cf57a6a-db47-46fa-838b-0128a1beef6d

Contrary to expectations, this conditional return type does not work because string|class-string<T> is normalized to string.
string is a super type of class-string, but in practice I think it is more convenient to keep it as a union type without normalizing it.

Did PHPStan help you today? Did it make you happy in any way?

We've written some PHPStan extensions for our internal projects, but we're very pleased with the introduction of the conditional return type as we can't keep up with the implementation of the extensions to meet the demand ❤️

@rvanvelzen
Copy link
Contributor

Duplicate of #7141

@ondrejmirtes
Copy link
Member

Please read and follow the original issue.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants