Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isSuperTypeOf of callable/Closure when parameter count differ #2949

Merged
merged 1 commit into from
Mar 5, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 12 additions & 24 deletions src/Type/CallableTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public static function isParametersAcceptorSuperTypeOf(
foreach ($theirParameters as $theirParameter) {
$lastParameter = $theirParameter;
}
$theirParameterCount = count($theirParameters);
$ourParameterCount = count($ourParameters);
if (
$lastParameter !== null
&& $lastParameter->isVariadic()
&& count($theirParameters) < count($ourParameters)
&& $theirParameterCount < $ourParameterCount
) {
foreach ($ourParameters as $i => $ourParameter) {
if (array_key_exists($i, $theirParameters)) {
Expand All @@ -38,7 +40,7 @@ public static function isParametersAcceptorSuperTypeOf(
}
}

$result = null;
$result = AcceptsResult::createYes();
foreach ($theirParameters as $i => $theirParameter) {
$parameterDescription = $theirParameter->getName() === '' ? sprintf('#%d', $i + 1) : sprintf('#%d $%s', $i + 1, $theirParameter->getName());
if (!isset($ourParameters[$i])) {
Expand All @@ -52,11 +54,7 @@ public static function isParametersAcceptorSuperTypeOf(
$parameterDescription,
),
]);
if ($result === null) {
$result = $accepts;
} else {
$result = $result->and($accepts);
}
$result = $result->and($accepts);
continue;
}

Expand All @@ -70,11 +68,7 @@ public static function isParametersAcceptorSuperTypeOf(
$parameterDescription,
),
]);
if ($result === null) {
$result = $accepts;
} else {
$result = $result->and($accepts);
}
$result = $result->and($accepts);
}

if ($treatMixedAsAny) {
Expand All @@ -95,11 +89,11 @@ public static function isParametersAcceptorSuperTypeOf(
]));
}

if ($result === null) {
$result = $isSuperType;
} else {
$result = $result->and($isSuperType);
}
$result = $result->and($isSuperType);
}

if (!$treatMixedAsAny && $theirParameterCount < $ourParameterCount) {
$result = $result->and(AcceptsResult::createMaybe());
}

$theirReturnType = $theirs->getReturnType();
Expand All @@ -109,13 +103,7 @@ public static function isParametersAcceptorSuperTypeOf(
$isReturnTypeSuperType = new AcceptsResult($ours->getReturnType()->isSuperTypeOf($theirReturnType), []);
}

if ($result === null) {
$result = $isReturnTypeSuperType;
} else {
$result = $result->and($isReturnTypeSuperType);
}

return $result;
return $result->and($isReturnTypeSuperType);
}

}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-inheritance.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9123.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10037.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/PhpDoc/data/bug-10594.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/set-type-type-specifying.php');
}

Expand Down
10 changes: 0 additions & 10 deletions tests/PHPStan/Levels/data/acceptTypes-5.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@
"line": 251,
"ignorable": true
},
{
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
"line": 319,
"ignorable": true
},
{
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
"line": 320,
"ignorable": true
},
{
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, float given.",
"line": 412,
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Levels/data/acceptTypes-7.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
"line": 302,
"ignorable": true
},
{
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, (Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface)|(Closure(): Levels\\AcceptTypes\\FooInterface) given.",
"line": 319,
"ignorable": true
},
{
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, (Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface)|(Closure(): Levels\\AcceptTypes\\FooInterface) given.",
"line": 320,
"ignorable": true
},
{
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, float|int given.",
"line": 415,
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public function testBug10214(): void
$this->analyse([__DIR__ . '/data/bug-10214.php'], []);
}

public function testBug10594(): void
{
$this->analyse([__DIR__ . '/data/bug-10594.php'], []);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-10594.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace Bug10594;

use Closure;
use ReflectionException;
use ReflectionFunction;
use ReflectionNamedType;
use function PHPStan\Testing\assertType;

/**
* @phpstan-type Callback1 Closure(string):string
* @phpstan-type Callback2 Closure(string, string):string
*/
class NarrowingCallbackTest
{
/**
* @param Callback1|Callback2 $callback
*
* @phpstan-assert-if-true Callback1 $callback
* @phpstan-assert-if-false Callback2 $callback
* @throws ReflectionException
*/
public function isCallback1(Closure $callback): bool {
assertType('(Closure(string): string)|(Closure(string, string): string)', $callback);

$refl = new ReflectionFunction($callback);
$parameters = $refl->getParameters();
if (count($parameters) >= 2) {
return $parameters[1]->getType() !== null && ($parameters[1]->getType() instanceof ReflectionNamedType && $parameters[1]->getType()->getName() !== 'string');
}
return true;
}
}
16 changes: 16 additions & 0 deletions tests/PHPStan/Type/CallableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function dataIsSuperTypeOf(): array
new CallableType([new NativeParameterReflection('foo', false, new MixedType(), PassedByReference::createNo(), false, null)], new MixedType(), false),
TrinaryLogic::createYes(),
],
[
new CallableType([
new NativeParameterReflection('foo', false, new StringType(), PassedByReference::createNo(), false, null),
new NativeParameterReflection('bar', false, new StringType(), PassedByReference::createNo(), false, null),
], new MixedType(), false),
new CallableType([new NativeParameterReflection('foo', false, new StringType(), PassedByReference::createNo(), false, null)], new MixedType(), false),
TrinaryLogic::createMaybe(),
],
];
}

Expand Down Expand Up @@ -338,6 +346,14 @@ public function dataAccepts(): array
]),
TrinaryLogic::createYes(),
],
[
new CallableType([
new NativeParameterReflection('foo', false, new StringType(), PassedByReference::createNo(), false, null),
new NativeParameterReflection('bar', false, new StringType(), PassedByReference::createNo(), false, null),
], new MixedType(), false),
new CallableType([new NativeParameterReflection('foo', false, new StringType(), PassedByReference::createNo(), false, null)], new MixedType(), false),
TrinaryLogic::createYes(),
],
];
}

Expand Down