Passed
Push — 109 ( d49bc8...e353ae )
by Max
02:35
created

RepeatEnvelope::times()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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