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

Issue with template inheritance #5496

Closed
VincentLanglet opened this issue Mar 28, 2021 · 3 comments
Closed

Issue with template inheritance #5496

VincentLanglet opened this issue Mar 28, 2021 · 3 comments
Labels

Comments

@VincentLanglet
Copy link
Contributor

The following code is valid with phpstan
https://phpstan.org/r/3475a9a3-539c-49d8-a476-77fb11b77d98

But not with psalm https://psalm.dev/r/a9385e48ec

Copy pasting the $classs property fix the issue
https://psalm.dev/r/498a052aff

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/a9385e48ec
<?php

/**
 * @phpstan-template T of object
 */
interface AA {
    /**
     * @phpstan-return class-string<T>
     */
    public function foo(): string;
}

/**
 * @phpstan-template T of object
 */
class B
{
    /**
     * @var string
     * @phpstan-var class-string<T>
     */
    protected $classs;
    
    /**
     * @phpstan-param class-string<T> $class
     */
    public function __construct(string $class)
    {
        $this->classs = $class;
    }
}

/**
 * @phpstan-template T of object
 * @phpstan-extends B<T>
 * @phpstan-implements AA<T>
 */
class A extends B implements AA {
    public function foo(): string
    {
        return $this->classs;
    }
}
Psalm output (using commit f41deea):

INFO: LessSpecificReturnStatement - 41:16 - The type 'class-string' is more general than the declared return type 'class-string<T:A as object>' for A::foo
https://psalm.dev/r/498a052aff
<?php

/**
 * @phpstan-template T of object
 */
interface AA {
    /**
     * @phpstan-return class-string<T>
     */
    public function foo(): string;
}

/**
 * @phpstan-template T of object
 */
class B
{
    /**
     * @var string
     * @phpstan-var class-string<T>
     */
    protected $classs;
    
    /**
     * @phpstan-param class-string<T> $class
     */
    public function __construct(string $class)
    {
        $this->classs = $class;
    }
}

/**
 * @phpstan-template T of object
 * @phpstan-extends B<T>
 * @phpstan-implements AA<T>
 */
class A extends B implements AA {
    /**
     * @var string
     * @phpstan-var class-string<T>
     */
    protected $classs;
    
    public function foo(): string
    {
        return $this->classs;
    }
}
Psalm output (using commit f41deea):

No issues!

@weirdan weirdan added the bug label Mar 28, 2021
@weirdan
Copy link
Collaborator

weirdan commented Mar 28, 2021

Simplified: https://psalm.dev/r/f32dc309ac

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/f32dc309ac
<?php
/** @template T of object */
abstract class B {
    /** @var class-string<T> */
    protected $classs;
    /** @return class-string<T> */
    abstract public function foo(): string;
}

/**
 * @template T of object
 * @extends B<T>
 * @psalm-suppress MissingConstructor
 */
class A extends B {
    public function foo(): string {
        /** @psalm-trace $this->classs */;
        return $this->classs;
    }
}
Psalm output (using commit f41deea):

INFO: Trace - 17:42 - $this->classs: class-string

INFO: LessSpecificReturnStatement - 18:16 - The type 'class-string' is more general than the declared return type 'class-string<T:A as object>' for A::foo

@muglug muglug closed this as completed in 9a714b7 Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants