TimesCalled::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 23
rs 10
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
/**
6
 * This class checks how many times a specified method was called.
7
 * @template T of object
8
 * @mixin T
9
 * @implements Indifferent<T>
10
 * @implements \MaxGoryunov\SavingIterator\Src\Scalar<int>
11
 */
12
class TimesCalled implements Indifferent, Scalar
13
{
14
15
    /**
16
     * Ctor.
17
     * 
18
     * @phpstan-param T $origin
19
     * @param object $origin original object.
20
     * @param Count  $count  how many times the method was called.
21
     * @param string $method method to pay attention to.
22
     */
23
    public function __construct(
24
        /**
25
         * Original object.
26
         *
27
         * @phpstan-var T
28
         * @var object
29
         */
30
        private object $origin,
31
32
        /**
33
         * How many times the method was called.
34
         *
35
         * @var Count
36
         */
37
        private Count $count,
38
39
        /**
40
         * MEthod to pay attention to.
41
         *
42
         * @var string
43
         */
44
        private string $method
45
    ) {
46
    }
47
48
    /**
49
     * Returns the number of calls to the specified method.
50
     *
51
     * @return int
52
     */
53
    public function value(): int
54
    {
55
        return $this->count->value();
56
    }
57
58
    /**
59
     * Sends calls through itself and counts how many times a specific method was called.
60
     * {@inheritDoc}
61
     */
62
    public function __call(string $name, array $arguments): mixed
63
    {
64
        if ($name === $this->method) {
65
            $this->count = $this->count->increment();
66
        }
67
        return $this->origin->$name(...$arguments);
68
    }
69
}
70