Skip to content

Do not apply checkMissingOverrideMethodAttribute in traits #12471

@JoniJnm

Description

@JoniJnm

Bug report

The Overwride attribute in traits depends on the class using that trait. That means sometimes there is no way to fix the phpstan error.

For example, the following code:

<?php

trait T
{
    public function f():void{}
}

class A {
    public function f():void{}
}

class B {
    use T;
}

class C extends A {
    use T;
}

The analyze command output is:

 ------ ---------------------------------------------------------------------------------- 
  Line   T.php (in context of class C)                                                     
 ------ ---------------------------------------------------------------------------------- 
  5      Method C::f() overrides method A::f() but is missing the #[\Override] attribute.  
         🪪  method.missingOverride                                                        
         ✏️  <href=>/app/src/T.php:5                                                       
 ------ ---------------------------------------------------------------------------------- 

Adding #[\Override] in line 5:

 ------ ---------------------------------------------------------------------------- 
  Line   T.php (in context of class B)                                               
 ------ ---------------------------------------------------------------------------- 
  5      Method B::f() has #[\Override] attribute but does not override any method.  
         ✏️  <href=>/app/src/T.php:5                                                 
 ------ ---------------------------------------------------------------------------- 

So the trait depends on the context, sometimes the Override is needed and other times isn't.

Related: #10150

Code snippet that reproduces the problem

https://phpstan.org/r/d8041b39-580f-44d3-ab0b-18d62daafcd1

Expected output

no error (online repro does not allow to set checkMissingOverrideMethodAttribute: true)

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

PHPStan helps me every day.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions