Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,15 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
usedVariables: $cachedClosureData['usedVariables'],
acceptsNamedArguments: TrinaryLogic::createYes(),
mustUseReturnValue: $mustUseReturnValue,
isStatic: TrinaryLogic::createFromBoolean($expr->static),
);
}
if (self::$resolveClosureTypeDepth >= 2) {
return new ClosureType(
$parameters,
$scope->getFunctionType($expr->returnType, false, false),
$isVariadic,
isStatic: TrinaryLogic::createFromBoolean($expr->static),
);
}

Expand Down Expand Up @@ -446,6 +448,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
usedVariables: $usedVariables,
acceptsNamedArguments: TrinaryLogic::createYes(),
mustUseReturnValue: $mustUseReturnValue,
isStatic: TrinaryLogic::createFromBoolean($expr->static),
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
TemplateTypeMap::createEmpty(),
TemplateTypeVarianceMap::createEmpty(),
acceptsNamedArguments: TrinaryLogic::createYes(),
isStatic: TrinaryLogic::createYes(),
);
}
if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) {
Expand Down
12 changes: 12 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class ClosureType implements TypeWithClassName, CallableParametersAcceptor

private Assertions $assertions;

private TrinaryLogic $isStatic;

/**
* @api
* @param list<ParameterReflection>|null $parameters
Expand All @@ -112,6 +114,7 @@ public function __construct(
?TrinaryLogic $acceptsNamedArguments = null,
?TrinaryLogic $mustUseReturnValue = null,
?Assertions $assertions = null,
?TrinaryLogic $isStatic = null,
)
{
if ($acceptsNamedArguments === null) {
Expand All @@ -132,6 +135,7 @@ public function __construct(
$this->callSiteVarianceMap = $callSiteVarianceMap ?? TemplateTypeVarianceMap::createEmpty();
$this->impurePoints = $impurePoints ?? [new SimpleImpurePoint('functionCall', 'call to an unknown Closure', false)];
$this->assertions = $assertions ?? Assertions::createEmpty();
$this->isStatic = $isStatic ?? TrinaryLogic::createMaybe();
}

public function getAsserts(): Assertions
Expand Down Expand Up @@ -302,6 +306,7 @@ function (): string {
$this->usedVariables,
$this->acceptsNamedArguments,
$this->mustUseReturnValue,
isStatic: $this->isStatic,
);

return $printer->print($selfWithoutParameterNames->toPhpDocNode());
Expand Down Expand Up @@ -461,6 +466,11 @@ public function isCommonCallable(): bool
return $this->isCommonCallable;
}

public function isStaticClosure(): TrinaryLogic
{
return $this->isStatic;
}

public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
{
return [$this];
Expand Down Expand Up @@ -709,6 +719,7 @@ public function traverse(callable $cb): Type
$this->acceptsNamedArguments,
$this->mustUseReturnValue,
$this->assertions,
$this->isStatic,
);
}

Expand Down Expand Up @@ -761,6 +772,7 @@ public function traverseSimultaneously(Type $right, callable $cb): Type
$this->acceptsNamedArguments,
$this->mustUseReturnValue,
$this->assertions,
$this->isStatic,
);
}

Expand Down
15 changes: 13 additions & 2 deletions src/Type/Php/ClosureBindDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;

#[AutowiredService]
Expand All @@ -27,12 +29,21 @@

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
$closureType = $scope->getType($methodCall->getArgs()[0]->value);
$args = $methodCall->getArgs();
$closureType = $scope->getType($args[0]->value);
if (!($closureType instanceof ClosureType)) {
return null;
}

return $closureType;
if ($closureType->isStaticClosure()->no()) {
return $closureType;
}

if (isset($args[1]) && $scope->getType($args[1]->value)->isNull()->yes()) {

Check warning on line 42 in src/Type/Php/ClosureBindDynamicReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $closureType; } - if (isset($args[1]) && $scope->getType($args[1]->value)->isNull()->yes()) { + if (isset($args[1]) && !$scope->getType($args[1]->value)->isNull()->no()) { return $closureType; }

Check warning on line 42 in src/Type/Php/ClosureBindDynamicReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $closureType; } - if (isset($args[1]) && $scope->getType($args[1]->value)->isNull()->yes()) { + if (isset($args[1]) && !$scope->getType($args[1]->value)->isNull()->no()) { return $closureType; }
return $closureType;
}

return new BenevolentUnionType([$closureType, new NullType()]);
}

}
13 changes: 12 additions & 1 deletion src/Type/Php/ClosureBindToDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;

#[AutowiredService]
Expand All @@ -32,7 +34,16 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return null;
}

return $closureType;
if ($closureType->isStaticClosure()->no()) {
return $closureType;
}

$args = $methodCall->getArgs();
if (isset($args[0]) && $scope->getType($args[0]->value)->isNull()->yes()) {
return $closureType;
}

return new BenevolentUnionType([$closureType, new NullType()]);
}

}
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-5009.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

namespace Bug5009;

use Closure;
use function PHPStan\Testing\assertType;

$foo = function (): void {};
$bar = $foo->bindTo(null);
assertType('Closure(): void', $bar);

$baz = Closure::bind($foo, null);
assertType('Closure(): void', $baz);

$newThis = new \stdClass();
$bound = $foo->bindTo($newThis);
assertType('Closure(): void', $bound);

$staticBound = Closure::bind($foo, $newThis);
assertType('Closure(): void', $staticBound);

$bound2 = $foo->bindTo($newThis, 'stdClass');
assertType('Closure(): void', $bound2);

$static = static function (): void {};
$boundStatic = $static->bindTo($newThis);
assertType('((Closure(): void)|null)', $boundStatic);

$boundStaticNull = $static->bindTo(null);
assertType('Closure(): void', $boundStaticNull);

$staticBound2 = Closure::bind($static, $newThis);
assertType('((Closure(): void)|null)', $staticBound2);

$staticBoundNull = Closure::bind($static, null);
assertType('Closure(): void', $staticBoundNull);

/** @var \stdClass|null $maybeNull */
$maybeNull = null;
$boundMaybe = $foo->bindTo($maybeNull);
assertType('Closure(): void', $boundMaybe);

$staticBoundMaybe = $static->bindTo($maybeNull);
assertType('((Closure(): void)|null)', $staticBoundMaybe);
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

$newThis = new class {};
$boundClosure = $closure->bindTo($newThis);
assertType('Closure(object): true', $boundClosure);
assertType('((Closure(object): true)|null)', $boundClosure);

$staticallyBoundClosure = \Closure::bind($closure, $newThis);
assertType('Closure(object): true', $staticallyBoundClosure);
assertType('((Closure(object): true)|null)', $staticallyBoundClosure);

$returnType = $closure->call($newThis, new class {});
assertType('true', $returnType);

$staticallyBoundClosureCaseInsensitive = \closure::bind($closure, $newThis);
assertType('Closure(object): true', $staticallyBoundClosureCaseInsensitive);
assertType('((Closure(object): true)|null)', $staticallyBoundClosureCaseInsensitive);
Loading