Skip to content
Closed
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
17 changes: 17 additions & 0 deletions src/Analyser/ExprHandler/Helper/MethodCallReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\Type\DynamicReturnTypeExtensionRegistryProvider;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use function count;

#[AutowiredService]
Expand All @@ -35,6 +37,21 @@ public function methodCallReturnType(
return null;
}

if ($typeWithMethod instanceof UnionType && !$typeWithMethod instanceof TemplateType) {
$memberTypes = [];
foreach ($typeWithMethod->getTypes() as $memberType) {
$memberResult = $this->methodCallReturnType($scope, $memberType, $methodName, $methodCall);
if ($memberResult === null) {
continue;
}
$memberTypes[] = $memberResult;
}
if (count($memberTypes) > 0) {
return TypeCombinator::union(...$memberTypes);
}
return null;
}

$methodReflection = $typeWithMethod->getMethod($methodName, $scope);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
Expand Down
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
{
/**
* 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