Passed
Push — master ( f7d584...3bbc21 )
by Alexander
02:20
created

ArrayHelper::permute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 12
nc 2
nop 2
crap 3
1
<?php
2
3
4
namespace Horat1us\Yii\Helpers;
5
6
use yii\helpers\ArrayHelper as YiiArrayHelper;
7
8
class ArrayHelper extends YiiArrayHelper
9
{
10
    /**
11
     * @param $items
12
     * @param array $perms
13
     * @return array
14
     */
15 1
    public static function permute($items, $perms = [])
16
    {
17 1
        if (empty($items)) {
18 1
            $return = [$perms];
19
        } else {
20 1
            $return = [];
21 1
            for ($i = count($items) - 1; $i >= 0; --$i) {
22 1
                $newItems = $items;
23 1
                $newPerms = $perms;
24 1
                list($foo) = array_splice($newItems, $i, 1);
25 1
                array_unshift($newPerms, $foo);
26 1
                $return = array_merge($return, static::permute($newItems, $newPerms));
27
            }
28
        }
29 1
        return $return;
30
    }
31
}