Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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: 9 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,17 @@

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 (isset($args[1]) && $scope->getType($args[1]->value)->isNull()->yes()) {

Check warning on line 38 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 null; } - 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 38 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 null; } - 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()]);
}

}
9 changes: 8 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,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return null;
}

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

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

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-5009.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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)|null)', $bound);

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

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

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

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

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