Skip to content

Commit

Permalink
Narrow Closure::bind $newScope param
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored and ondrejmirtes committed Apr 22, 2024
1 parent 29c440e commit 542cb0a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions resources/functionMap_bleedingEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [
'new' => [
'Closure::bind' => ['Closure', 'old'=>'Closure', 'to'=>'?object', 'scope='=>'object|class-string|\'static\'|null'],
'Closure::bindTo' => ['Closure', 'new'=>'?object', 'newscope='=>'object|class-string|\'static\'|null'],
'error_log' => ['bool', 'message'=>'string', 'message_type='=>'0|1|2|3|4', 'destination='=>'string', 'extra_headers='=>'string'],
'SplFileObject::flock' => ['bool', 'operation'=>'int-mask<LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB>', '&w_wouldblock='=>'0|1'],
'Imagick::adaptiveBlurImage' => ['bool', 'radius'=>'float', 'sigma'=>'float', 'channel='=>'Imagick::CHANNEL_*'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -189,7 +190,8 @@ public function dataMethods(): array
'optional' => true,
'type' => new UnionType([
new ObjectWithoutClassType(),
new StringType(),
new ClassStringType(),
new ConstantStringType('static'),
new NullType(),
]),
'nativeType' => new UnionType([
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,10 @@ public function testClosureBind(): void
'Call to an undefined method CallClosureBind\Foo::nonexistentMethod().',
44,
],
[
'Parameter #2 $newScope of method Closure::bindTo() expects \'static\'|class-string|object|null, \'CallClosureBind\\\Bar3\' given.',
74,
],
]);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,4 +817,15 @@ public function testConditionalParam(): void
]);
}

public function testClosureBind(): void
{
$this->checkThisOnly = false;
$this->analyse([__DIR__ . '/data/closure-bind.php'], [
[
'Parameter #3 $newScope of static method Closure::bind() expects \'static\'|class-string|object|null, \'CallClosureBind\\\Bar3\' given.',
68,
],
]);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Methods/data/closure-bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,33 @@ public function fooMethod(): Foo
})->call(new Foo());
}

public function x(): bool
{
return 1.0;
}

public function testClassString(): bool
{
$fx = function () {
return $this->x();
};

$res = 0.0;
$res += \Closure::bind($fx, $this)();
$res += \Closure::bind($fx, $this, 'static')();
$res += \Closure::bind($fx, $this, Foo2::class)();
$res += \Closure::bind($fx, $this, 'CallClosureBind\Bar2')();
$res += \Closure::bind($fx, $this, 'CallClosureBind\Bar3')();

$res += $fx->bindTo($this)();
$res += $fx->bindTo($this, 'static')();
$res += $fx->bindTo($this, Foo2::class)();
$res += $fx->bindTo($this, 'CallClosureBind\Bar2')();
$res += $fx->bindTo($this, 'CallClosureBind\Bar3')();

return $res;
}

}

class Bar2 extends Bar {}

0 comments on commit 542cb0a

Please sign in to comment.