RepetitionEnvelope::times()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
 * @template X subject type
10
 * @template Y result type
11
 * @implements Repetition<Y>
12
 */
13
abstract class RepetitionEnvelope implements Repetition
14
{
15
    /**
16
     * Ctor.
17
     *
18
     * @phpstan-param T             $subject
19
     * @phpstan-param Closure(T): Y $context
20
     * @param mixed   $subject element to be processed.
21
     * @param Closure $context context for the element.
22
     */
23
    public function __construct(
24
        /**
25
         * Element to be processed.
26
         *
27
         * @phpstan-var X
28
         * @var mixed
29
         */
30
        private mixed $subject,
31
32
        /**
33
         * Context for the element.
34
         *
35
         * @phpstan-var Closure(X): Y
36
         * @var Closure
37
         */
38
        private Closure $context
39
    ) {
40
    }
41
42
    /**
43
     * Returns several results of applying context to subject.
44
     *
45
     * @param int $count
46
     * @phpstan-return Y[]
47
     * @return mixed[]
48
     */
49
    final public function times(int $count): array
50
    {
51
        return array_map(
52
            $this->context,
53
            array_fill_keys(range(0, $count - 1), $this->subject)
54
        );
55
    }
56
}
57