Doctrine forAllでmust be of type ○○, int given,が出る


サンプルコード(エラーがでる)

$collection=new ArrayCollection(["a","b","c"]);
if($collection->forAll(function(Hoge $hoge){
   //何か処理
}

エラー内容

Argument #1 ($hoge) must be of type Hoge, int given

背景

filterやmapなどと違い、forAllは与えられた無名関数に(key,value)の組を与えます。
ドキュメント:https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html#forall

修正

$collection=new ArrayCollection(["a","b","c"]);
if($collection->forAll(function(int $key,Hoge $hoge){
   //何か処理
}

あとがき

filterとかから書き換えたときに陥りがちなエラーで、ドキュメントを見れば一発ですが意外と気づかないもんですね。