Closure::bindTo Throwable instead of warning when passing object other class
まだ誰も着手していません。
- 主要言語
- C
- スター
- 40.4k
- フォーク
- 8.2k
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 96
説明
Description:
Currently, when binding a new object to Closure, which is an object of a different class, a "warning" is logged in the log, but the method itself has no effect, which can be confusing.
It seems to me better to add Throwable in such a scenario, it will make it easier to handle the case.
The following code:
<?php
class SomeClass
{
public function testMethod() {
echo 123;
}
}
class OtherClass
{
public function testMethod() {
echo 234;
}
}
$a = new SomeClass();
$b = new OtherClass();
$closure = Closure::fromCallable([$a, 'testMethod']);
try {
$closure->bindTo($b);
} catch (Throwable $e) {
echo 'Oh, sorry u cannot do it!';
return;
}
$closure();
Resulted:
Warning: Cannot bind method SomeClass::testMethod() to object of class OtherClass in /in/VgKd4 on line 23
123
Expected:
Oh, sorry u cannot do it!
Workaround:
As it stands, to be able to handle such a scenario you need to use Reflection as follows:
$reflection = new ReflectionFunction($closure);
$oldThis = $reflection->getClosureThis();
if ($oldThis && $oldThis::class === $b::class) {
$closure->bindTo($b);
} else {
echo 'Oh, sorry u cannot do it!';
}
Of course, this code can be simplified, but a simple try/catch is a much better solution.
Especially since I assume that the parser will check again if the object is of the same class when executes bindTo()
PHP Version
PHP 8.2.6
Operating System
Linux
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された Closure::fromCallable() と bindTo() の再現コードから始め、次に Closure::bindTo のエントリーポイントと現在の警告動作を確認します。互換性のないクラスのオブジェクトのバインドを Throwable として捕捉でき、示されたケースのカバレッジがあり、有効なバインドにリグレッションがないことを完了条件とします。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, php
- 領域
- backend
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100