RandomChooser   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 20
ccs 7
cts 7
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A chooseVariant() 0 12 2
1
<?php
2
/**
3
 * This file is part of phpab/phpab. (https://github.com/phpab/phpab)
4
 *
5
 * @link https://github.com/phpab/phpab for the canonical source repository
6
 * @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/)
7
 * @license https://raw.githubusercontent.com/phpab/phpab/master/LICENSE.md MIT
8
 */
9
10
namespace PhpAb\Variant\Chooser;
11
12
use PhpAb\Variant\VariantInterface;
13
14
/**
15
 * A variant chooser that makes its choice randomly.
16
 *
17
 * @package PhpAb
18
 */
19
class RandomChooser implements ChooserInterface
20
{
21
    /**
22
     * {@inheritDoc}
23
     *
24
     * @param VariantInterface[] $variants Variants to choose from
25
     */
26 3
    public function chooseVariant($variants)
27
    {
28 3
        $count = count($variants);
29 3
        if (0 === $count) {
30 1
            return null;
31
        }
32
33 2
        $chosenCount = mt_rand(0, $count - 1);
34 2
        $keys = array_keys($variants);
35
36 2
        return $variants[$keys[$chosenCount]];
37
    }
38
}
39