🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Specific conclusion about reused Trait makes no sense (generic) #12267

Description

@rudiedirkx

Bug report

Traits are analyzed per context, and that context can be very different, and static. If the trait is generic, the parent class can add staticness in per-context analysis, but that's not static for the trait code:

class Class1 {
	/** @use PrintSomething<null> */
	use PrintSomething;
}

class Class2 {
	/** @use PrintSomething<Exception> */
	use PrintSomething;

The trait is used for all kinds of contexts, sometimes Exception, sometimes null, sometimes Model, etc.

The trait itself doesn't know which context, so it has to check:

if ($this->model instanceof Exception) {

but per-context, that's a stupid if, because in Class2-context, of course it's an Exception! And in Class1-context, of course it's not an Exception! But the trait itself doesn't know that, so it has to check. And the if check findings from PhpStan are silly:

11	Negated boolean expression is always true.
11	Negated boolean expression is always false.

It's always true AND always false 😮

Code snippet that reproduces the problem

Expected output

0 errors, because PhpStan knows $this->model is variable, not static, because it's a generic filled in by the parent class.

The echo $this->model; is a dumb example, but stuff happens there (if it's not the null context).

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

PhpStan is crazy smart mostly! template-type through array keys of an argument through a Builder!? ❤️ https://phpstan.org/r/6f3bfb6b-7f60-4935-805c-bf377bd6bd32

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions