TimesCalled   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 56
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 3 1
A __call() 0 6 2
A __construct() 0 23 1
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