Test Failed
Pull Request — main (#31)
by Max
02:35
created

TimesCalled::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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