Passed
Push — master ( 4748af...82e71c )
by Pol
03:57
created

RSample::on()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use loophp\collection\Contract\Operation;
9
10
/**
11
 * Class RSample.
12
 */
13
final class RSample implements Operation
14
{
15
    /**
16
     * @var float
17
     */
18
    private $probability;
19
20
    /**
21
     * RSample constructor.
22
     *
23
     * @param float $probability
24
     */
25 1
    public function __construct(float $probability)
26
    {
27 1
        $this->probability = $probability;
28 1
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function on(iterable $collection): Closure
34
    {
35 1
        $probability = $this->probability;
36
37
        $callback = static function ($item) use ($probability): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
        $callback = static function (/** @scrutinizer ignore-unused */ $item) use ($probability): bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38 1
            return (mt_rand() / mt_getrandmax()) < $probability;
39 1
        };
40
41 1
        return (new Filter($callback))->on($collection);
42
    }
43
}
44