Completed
Push — master ( e41e63...2cec7d )
by Mariano
03:44
created

RandomChooser::chooseVariant()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 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