Skip to content

Commit

Permalink
Fixed detecting method signature compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 20, 2021
1 parent 1991bc7 commit 8f0150d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Rules/Methods/MethodSignatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
Expand Down Expand Up @@ -223,9 +224,10 @@ private function transformStaticType(ClassReflection $declaringClass, Type $type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($declaringClass): Type {
if ($type instanceof StaticType) {
$changedType = $type->changeBaseClass($declaringClass);
if ($declaringClass->isFinal()) {
$changedType = $changedType->getStaticObjectType();
$changedType = new ObjectType($declaringClass->getName());
} else {
$changedType = $type->changeBaseClass($declaringClass);
}
return $traverse($changedType);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,11 @@ public function testBug4707Two(): void
$this->analyse([__DIR__ . '/data/bug-4707-two.php'], []);
}

public function testBug4729(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/bug-4729.php'], []);
}

}
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4729.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Bug4729;

/** @template T of int */
interface I
{
/**
* @return static
*/
function get(): I;
}

/**
* @template T of int
* @implements I<T>
*/
final class B implements I
{
function get(): I
{
return $this;
}
}

/**
* @template T of int
* @implements I<T>
*/
class C implements I
{
function get(): I
{
return $this;
}
}

0 comments on commit 8f0150d

Please sign in to comment.