Passed
Pull Request — master (#116)
by Max
02:38
created

ContextVeil   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 1
A __call() 0 6 2
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use Closure;
6
7
/**
8
 * Veil which applies context to origin if the condition is met.
9
 * @template T of object origin type.
10
 * @implements Indifferent<T>
11
 * @mixin T
12
 */
13
final class ContextVeil implements Indifferent
14
{
15
16
    /**
17
     * Ctor.
18
     * 
19
     * @phpstan-param T $origin
20
     * @param object $origin original element.
21
     */
22
    public function __construct(
23
        /**
24
         * Original element.
25
         *
26
         * @phpstan-var T
27
         * @var object
28
         */
29
        private object $origin,
30
31
        /**
32
         * Context for the element.
33
         * Modifies the element and returns the result.
34
         *
35
         * @phpstan-var Closure(T): T
36
         * @var Closure
37
         */
38
        private Closure $context,
39
40
        /**
41
         * Methods on which the element must be modified.\
42
         * Does not accept nulls as values.
43
         *
44
         * @var array<string, mixed>
45
         */
46
        private array $methods
47
    ) {
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function __call(string $name, array $arguments): mixed
54
    {
55
        if (isset($this->methods[$name])) {
56
            $this->origin = ($this->context)($this->origin);
57
        }
58
        return $this->origin->$name(...$arguments);
59
    }
60
}
61