Random::create()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\phptree\Builder;
11
12
use loophp\phptree\Node\NodeInterface;
13
14
use function is_callable;
15
16
class Random implements BuilderInterface
17
{
18 1
    public static function create(iterable $nodes): ?NodeInterface
19
    {
20 1
        $root = null;
21
22 1
        foreach ($nodes as $key => $value) {
23 1
            if (0 === $key) {
24 1
                $root = self::createNode($value);
25
26 1
                continue;
27
            }
28
29 1
            if (!$root instanceof NodeInterface) {
30 1
                continue;
31
            }
32
33 1
            self::pickRandomNode($root)->add(self::createNode($value));
34
        }
35
36 1
        return $root;
37
    }
38
39
    /**
40
     * @param array<int, class-string|callable():(NodeInterface)|mixed> $parameters
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, class-string|...:(NodeInterface)|mixed> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<int, class-string|callable():(NodeInterface)|mixed>.
Loading history...
41
     */
42 1
    private static function createNode(array $parameters = []): NodeInterface
43
    {
44 1
        $parameters = array_map(
45
            /**
46
             * @param class-string|callable():(NodeInterface)|mixed $parameter
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|callable():(NodeInterface)|mixed at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|callable():(NodeInterface)|mixed.
Loading history...
47
             *
48
             * @return class-string|mixed
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|mixed at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|mixed.
Loading history...
49
             */
50
            static function ($parameter) {
51 1
                if (is_callable($parameter)) {
52 1
                    return $parameter();
53
                }
54
55 1
                return $parameter;
56 1
            },
57
            $parameters
58
        );
59
60 1
        $class = array_shift($parameters);
61
62 1
        return new $class(...$parameters);
63
    }
64
65 1
    private static function pickRandomNode(NodeInterface $node): NodeInterface
66
    {
67 1
        $pick = (int) random_int(0, $node->count());
68
69 1
        $i = 0;
70
71 1
        foreach ($node->all() as $child) {
72 1
            if (++$i === $pick) {
73 1
                return $child;
74
            }
75
        }
76
77 1
        return $node;
78
    }
79
}
80