Passed
Push — master ( b97084...328dfe )
by Max
45s queued 12s
created

RepetitionEnvelope   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A times() 0 5 1
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Fakes;
4
5
use Closure;
6
7
/**
8
 * Repeats some process several times and returns its result.
9
 * @todo #109:30min Add Rewinding repetition or repetition which converts
10
 *  iterator to array for rewind tests and use it in iterator tests.
11
 * @template X subject type
12
 * @template Y result type
13
 * @implements Repetition<Y>
14
 */
15
abstract class RepetitionEnvelope implements Repetition
16
{
17
    /**
18
     * Ctor.
19
     *
20
     * @phpstan-param T             $subject
21
     * @phpstan-param Closure(T): Y $context
22
     * @param mixed   $subject element to be processed.
23
     * @param Closure $context context for the element.
24
     */
25
    public function __construct(
26
        /**
27
         * Element to be processed.
28
         *
29
         * @phpstan-var X
30
         * @var mixed
31
         */
32
        private mixed $subject,
33
34
        /**
35
         * Context for the element.
36
         *
37
         * @phpstan-var Closure(X): Y
38
         * @var Closure
39
         */
40
        private Closure $context
41
    ) {
42
    }
43
44
    /**
45
     * Returns several results of applying context to subject.
46
     *
47
     * @param int $count
48
     * @phpstan-return Y[]
49
     * @return mixed[]
50
     */
51
    final public function times(int $count): array
52
    {
53
        return array_map(
54
            $this->context,
55
            array_fill_keys(range(0, $count - 1), $this->subject)
56
        );
57
    }
58
}
59