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
11 changes: 11 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4816,6 +4816,17 @@ private function getParameterTypeFromParameterClosureTypeExtension(CallLike $cal
return $staticMethodParameterClosureTypeExtension->getTypeFromStaticMethodCall($calleeReflection, $callLike, $parameter, $scope);
}
}
} elseif ($callLike instanceof New_ && $callLike->class instanceof Name) {
$staticCall = new StaticCall(
$callLike->class,
new Identifier('__construct'),
$callLike->getArgs(),
Copy link
Contributor

Choose a reason for hiding this comment

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

We use NodeScopeResolver in rector, I think getArgs() should be replaced with args here, or check isFirstClassCallable() early here to avoid error assert error, see:

https://github.com/nikic/PHP-Parser/blob/8c360e27327c8bd29e1c57721574709d0d706118/lib/PhpParser/Node/Expr/CallLike.php#L32

Copy link
Member

Choose a reason for hiding this comment

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

$callLike here cannot be a first class callable.

);
foreach ($this->parameterClosureTypeExtensionProvider->getStaticMethodParameterClosureTypeExtensions() as $staticMethodParameterClosureTypeExtension) {
if ($staticMethodParameterClosureTypeExtension->isStaticMethodSupported($calleeReflection, $parameter)) {
return $staticMethodParameterClosureTypeExtension->getTypeFromStaticMethodCall($calleeReflection, $staticCall, $parameter, $scope);
}
}
} elseif ($callLike instanceof MethodCall) {
foreach ($this->parameterClosureTypeExtensionProvider->getMethodParameterClosureTypeExtensions() as $methodParameterClosureTypeExtension) {
if ($methodParameterClosureTypeExtension->isMethodSupported($calleeReflection, $parameter)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ class StaticMethodParameterClosureTypeExtension implements \PHPStan\Type\StaticM

public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool
{
return $methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticMethodWithCallable';
if ($methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticMethodWithCallable') {
return true;
}

if ($methodReflection->getDeclaringClass()->getName() === Bar::class && $methodReflection->getName() === '__construct') {
return true;
}

return false;
}

public function getTypeFromStaticMethodCall(
Expand All @@ -116,6 +124,32 @@ public function getTypeFromStaticMethodCall(
ParameterReflection $parameter,
Scope $scope
): ?Type {
if ($methodReflection->getDeclaringClass()->getName() === Bar::class && $methodReflection->getName() === '__construct') {
$args = $methodCall->getArgs();

if (count($args) < 2) {
return null;
}

$integer = $scope->getType($args[0]->value)->getConstantScalarValues()[0];

if ($integer === 1) {
return new CallableType(
[
new NativeParameterReflection('test', false, new IntegerType(), PassedByReference::createNo(), false, null),
],
new MixedType()
);
}

return new CallableType(
[
new NativeParameterReflection('test', false, new StringType(), PassedByReference::createNo(), false, null),
],
new MixedType()
);
}

return new CallableType(
[
new NativeParameterReflection('test', false, new FloatType(), PassedByReference::createNo(), false, null),
Expand Down Expand Up @@ -173,6 +207,20 @@ public function getValue()
}
}

class Bar
{

/**
* @param int $foo
* @param callable(mixed) $callback
*/
public function __construct(int $foo, callable $callback)
{

}

}

/**
* @param int $foo
* @param callable(Generic<array-key>) $callback
Expand All @@ -192,6 +240,10 @@ function test(Foo $foo): void
(new Foo)->methodWithCallable(2, fn (Generic $i) => assertType('string', $i->getValue()));

Foo::staticMethodWithCallable(fn ($i) => assertType('float', $i));

new Bar(1, fn ($i) => assertType('int', $i));

new Bar(2, fn ($i) => assertType('string', $i));
}

functionWithCallable(1, fn ($i) => assertType('int', $i->getValue()));
Expand Down
58 changes: 57 additions & 1 deletion tests/PHPStan/Analyser/data/parameter-closure-type-extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ class StaticMethodParameterClosureTypeExtension implements \PHPStan\Type\StaticM

public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool
{
return $methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticMethodWithClosure';
if ($methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticMethodWithClosure') {
return true;
}

if ($methodReflection->getDeclaringClass()->getName() === Bar::class && $methodReflection->getName() === '__construct') {
return true;
}

return false;
}

public function getTypeFromStaticMethodCall(
Expand All @@ -126,6 +134,32 @@ public function getTypeFromStaticMethodCall(
ParameterReflection $parameter,
Scope $scope
): ?Type {
if ($methodReflection->getDeclaringClass()->getName() === Bar::class && $methodReflection->getName() === '__construct') {
$args = $methodCall->getArgs();

if (count($args) < 2) {
return null;
}

$integer = $scope->getType($args[0]->value)->getConstantScalarValues()[0];

if ($integer === 1) {
return new ClosureType(
[
new NativeParameterReflection('test', false, new IntegerType(), PassedByReference::createNo(), false, null),
],
new VoidType()
);
}

return new ClosureType(
[
new NativeParameterReflection('test', false, new StringType(), PassedByReference::createNo(), false, null),
],
new VoidType()
);
}

return new ClosureType(
[
new NativeParameterReflection('test', false, new FloatType(), PassedByReference::createNo(), false, null),
Expand Down Expand Up @@ -185,6 +219,20 @@ public function getValue()
}
}

class Bar
{

/**
* @param int $foo
* @param Closure(mixed): void $callback
*/
public function __construct(int $foo, Closure $callback)
{

}

}

/**
* @param int $foo
* @param Closure(Generic<array-key>): void $callback
Expand All @@ -209,6 +257,14 @@ function test(Foo $foo): void
Foo::staticMethodWithClosure(function ($i) {
assertType('float', $i);
});

new Bar(1, function ($i) {
assertType('int', $i);
});

new Bar(2, function ($i) {
assertType('string', $i);
});
}

functionWithClosure(1, function ($i) {
Expand Down
Loading