Skip to content

PHPStan reports wrongly a method as deprecated, because the overwriting method from a trait is not detected #76

@pl-github

Description

@pl-github

Bug report

I am using phpstan in combination with phpstan-deprecation-rules and getting false-positives in my test classes since a deprecation annotation has been added to PHPUnit.

The code detecting deprecations in phpstan-deprecation-rules looks good:

$classReflection = $this->reflectionProvider->getClass($referencedClass);
$methodReflection = $classReflection->getMethod($methodName, $scope);

if (!$methodReflection->isDeprecated()->yes()) {
    continue;
}

But the $methodReflection is pointing to the method defined in the (TestCase) base class instead of the overwriting method in the ProphecyTrait.

To reproduce this issue easily, I have created a minimal setup.

source file to check

<?php

declare(strict_types=1);

trait MyTrait
{
    protected function prophesize(): void
    {
        echo 'Trait';
    }
}

abstract class MyBaseClass
{
    /** @deprecated Use MyTrait::prophesize() */
    protected function prophesize(): void
    {
        echo 'Base';
    }
}

final class MyClass extends MyBaseClass
{
    use MyTrait;

    public function callProphesize(): void
    {
        $this->prophesize(); // wrongly detected deprecation
    }
}

phpstan.neon

parameters:
    level: max
    paths:
        - src

includes:
    - %rootDir%/../../phpstan/phpstan-deprecation-rules/rules.neon

Expected output

I am expectiong that the $methodReflection variable is pointing to MyTrait::prophesize() so that $methodReflection->isDeprecated()->yes() returns false and no deprecation is detected by phpstan-deprecation-rules.

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

phpstan is a great tool that helps a lot in daily 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