重複するビジネス ロジックを削除するには、再帰を使用してこのメソッドを単純化する必要がありますが、これを行う方法がわかりません。
public function compute()
{
$ret = array();
foreach ($this->_items as $item) {
$ret[] = array($item);
}
foreach ($this->_items as $item) {
foreach ($this->_items as $item2) {
$tmp = array($item, $item2);
if (count($tmp) === count(array_unique($tmp))) {
$ret[] = $tmp;
}
}
}
foreach ($this->_items as $item) {
foreach ($this->_items as $item2) {
foreach ($this->_items as $item3) {
$tmp = array($item, $item2, $item3);
if (count($tmp) === count(array_unique($tmp))) {
$ret[] = $tmp;
}
}
}
}
return $ret;
}
編集:
このメソッドは、配列要素のすべての組み合わせを返すことになっているため、次のような配列がある場合:
[a, b, c]
次のように返されます。
[
[a],
[b],
[c],
[a, b],
[a, c],
[b, a],
[b, c],
[a, b, c],
[a, c, b],
[b, a, c],
[b, c, a],
[c, a, b],
[c, b, a]
]