Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAcce
continue;
}

$methodPrototypes[] = $type->getUnresolvedMethodPrototype($methodName, $scope)->withCalledOnType($this);
$prototype = $type->getUnresolvedMethodPrototype($methodName, $scope);
if ($this instanceof TemplateType) {
$prototype = $prototype->withCalledOnType($this);
}
$methodPrototypes[] = $prototype;
}

$methodsCount = count($methodPrototypes);
Expand Down
60 changes: 60 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11687.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace Bug11687;

use function PHPStan\Testing\assertType;

class A
{
public static function retStaticConst(): int
{
return 1;
}

/**
* @return static
*/
public static function retStatic()
{
return new static(); // @phpstan-ignore new.static
}
}

class B extends A
{
/**
* @return 2
*/
public static function retStaticConst(): int
{
return 2;
}

public function foo(): void
{
$clUnioned = mt_rand() === 0
? A::class
: X::class;

assertType('int', A::retStaticConst());
assertType('bool', X::retStaticConst());
assertType('bool|int', $clUnioned::retStaticConst());

assertType('Bug11687\A', A::retStatic());
assertType('bool', X::retStatic());
assertType('bool|Bug11687\A', $clUnioned::retStatic());
}
}

class X
{
public static function retStaticConst(): bool
{
return false;
}

public static function retStatic(): bool
{
return false;
}
}
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12562.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug12562;

use function PHPStan\Testing\assertType;

class UserV1
{
public function toV2(): UserV2
{
return new UserV2();
}
}

class UserV2
{
public function toV2(): self
{
return $this;
}
}

class UserV2a extends UserV2
{
/**
* @return $this
*/
public function toV2(): self
{
return $this;
}
}

function doSomething(UserV1|UserV2 $user, UserV1|UserV2a $user2): void
{
assertType('Bug12562\UserV2', $user->toV2());
assertType('Bug12562\UserV2', $user2->toV2());
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/static-late-binding.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function foo(): void
assertType('static(StaticLateBinding\B)', parent::retStatic());
assertType('static(StaticLateBinding\B)', $this->retStatic());
assertType('bool', X::retStatic());
assertType('bool|StaticLateBinding\A|StaticLateBinding\X', $clUnioned::retStatic()); // should be bool|StaticLateBinding\A https://github.com/phpstan/phpstan/issues/11687
assertType('bool|StaticLateBinding\A', $clUnioned::retStatic()); // should be bool|StaticLateBinding\A https://github.com/phpstan/phpstan/issues/11687

assertType('StaticLateBinding\A', A::retStatic(...)());
assertType('StaticLateBinding\B', B::retStatic(...)());
Expand Down
Loading