Skip to content
Merged
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 @@ -590,7 +590,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 // 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());
}
90 changes: 90 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14203.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug14203;

use function PHPStan\Testing\assertType;

/**
* @template TKey of array-key
* @template TValue
*/
class Collection
{
/**
Copy link
Copy Markdown
Contributor

@staabm staabm Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file mixes tabs and spaces for indent

* Create a new collection.
*
* @param array<TKey, TValue> $items
*/
final public function __construct(protected $items = [])
{
}

/**
* @template TMapValue
*
* @param callable(TValue, TKey): TMapValue $callback
* @return static<TKey, TMapValue>
*/
public function map(callable $callback)
{
$newItems = [];

foreach ($this->items as $key => $value) {
$newItems[$key] = $callback($value, $key);
}

return new static($newItems);
}
}

class SpecificA {
public function __construct(
public readonly int $valueA,
public readonly string $someSharedValue,
) {}
}

class SpecificB {
public function __construct(
public readonly int $valueB,
public readonly string $someSharedValue,
) {}
}

class MyDTO {
public function __construct(
public readonly string $thatSharedValue,
) {}
}

function works(): void {
$myCollection = new Collection([new SpecificA(1, 'A'), new SpecificB(2, 'B')]);

$result = $myCollection->map(static fn (SpecificA|SpecificB $specific): MyDTO => new MyDTO($specific->someSharedValue));
assertType('Bug14203\Collection<int, Bug14203\MyDTO>', $result);
}

/**
* @return Collection<int, SpecificA>
*/
function getA(): Collection {
return new Collection([new SpecificA(1, 'A')]);
}

/**
* @return Collection<int, SpecificB>
*/
function getB(): Collection {
return new Collection([new SpecificB(2, 'B')]);
}

function breaks(): void {
$myCollection = random_int(0, 1) === 0
? getA()
: getB();

$result = $myCollection->map(static fn (SpecificA|SpecificB $specific): MyDTO => new MyDTO($specific->someSharedValue));
assertType('Bug14203\Collection<int, Bug14203\MyDTO>', $result);
}
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());

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