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

Narrow Closure::bind $newScope param #2817

Merged
merged 1 commit into from
Apr 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {}
Loading