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
39 changes: 35 additions & 4 deletions src/Reflection/Type/IntersectionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
final class IntersectionTypeMethodReflection implements ExtendedMethodReflection
{

private ?ExtendedMethodReflection $methodWithMostParameters = null;

/**
* @param ExtendedMethodReflection[] $methods
*/
Expand All @@ -31,7 +33,7 @@ public function __construct(private string $methodName, private array $methods)

public function getDeclaringClass(): ClassReflection
{
return $this->methods[0]->getDeclaringClass();
return $this->getMethodWithMostParameters()->getDeclaringClass();
}

public function isStatic(): bool
Expand Down Expand Up @@ -104,7 +106,7 @@ public function getVariants(): array
$phpDocReturnType,
$nativeReturnType,
$acceptor->getCallSiteVarianceMap(),
), $this->methods[0]->getVariants());
), $this->getMethodWithMostParameters()->getVariants());
}

public function getOnlyVariant(): ExtendedParametersAcceptor
Expand Down Expand Up @@ -237,7 +239,7 @@ public function isAbstract(): TrinaryLogic

public function getAttributes(): array
{
return $this->methods[0]->getAttributes();
return $this->getMethodWithMostParameters()->getAttributes();
}

public function mustUseReturnValue(): TrinaryLogic
Expand All @@ -247,7 +249,36 @@ public function mustUseReturnValue(): TrinaryLogic

public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
{
return $this->methods[0]->getResolvedPhpDoc();
return $this->getMethodWithMostParameters()->getResolvedPhpDoc();
}

/**
* Since every intersected method should be compatible,
* selects the method whose variant has the widest parameter list,
* so intersection ordering does not affect call validation.
*/
private function getMethodWithMostParameters(): ExtendedMethodReflection
{
if ($this->methodWithMostParameters !== null) {
return $this->methodWithMostParameters;
}

$methodWithMostParameters = $this->methods[0];
$maxParameters = 0;
foreach ($this->methods as $method) {
foreach ($method->getVariants() as $variant) {
if (count($variant->getParameters()) <= $maxParameters) {
continue;
}

$maxParameters = count($variant->getParameters());
$methodWithMostParameters = $method;
}
}

$this->methodWithMostParameters = $methodWithMostParameters;

return $methodWithMostParameters;
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3975,4 +3975,21 @@ public function testBug4608(): void
]);
}

public function testBug11978(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-11978.php'], [
[
'Method Bug11978\ViewB::render() invoked with 2 parameters, 0-1 required.',
25,
],
[
'Method Bug11978\ViewB::render() invoked with 2 parameters, 0-1 required.',
26,
],
]);
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-11978.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug11978;

interface ViewA {
public function render(): string;
}
interface ViewB {
public function render(string $foo = ''): string;
}

class Foo
{
public function __construct(
private readonly ViewA&ViewB $view1,
private readonly ViewB&ViewA $view2,
) {}

public function renderFoo(string $foo): string
{
$a = $this->view1->render($foo);
$b = $this->view2->render($foo);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

append these line to the test:

$c = $this->view1->render($foo, $foo);
$d = $this->view2->render($foo, $foo);
$e = $this->view1->render();
$f = $this->view2->render();

and adjust expected errors in tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Background make tests also completed successfully. All done — the review changes are committed and pushed.

$c = $this->view1->render($foo, $foo);
$d = $this->view2->render($foo, $foo);
$e = $this->view1->render();
$f = $this->view2->render();

return $a . $b;
}
}
Loading