Passed
Push — master ( 4863aa...b97084 )
by Max
49s queued 14s
created

ContextVeil::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 6
rs 10
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
     * @phpstan-param Closure(T): T        $context
21
     * @phpstan-param array<string, mixed> $methods
22
     * @param object               $origin  original element.
23
     * @param Closure              $context context for the element.
24
     * @param array<string, mixed> $methods methods on which the element must
25
     * be modified.
26
     */
27
    public function __construct(
28
        /**
29
         * Original element.
30
         *
31
         * @phpstan-var T
32
         * @var object
33
         */
34
        private object $origin,
35
36
        /**
37
         * Context for the element.
38
         * Modifies the element and returns the result.
39
         *
40
         * @phpstan-var Closure(T): T
41
         * @var Closure
42
         */
43
        private Closure $context,
44
45
        /**
46
         * Methods on which the element must be modified.\
47
         * Does not accept nulls as values.
48
         *
49
         * @var array<string, mixed>
50
         */
51
        private array $methods
52
    ) {
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function __call(string $name, array $arguments): mixed
59
    {
60
        if (isset($this->methods[$name])) {
61
            $this->origin = ($this->context)($this->origin);
62
        }
63
        return $this->origin->$name(...$arguments);
64
    }
65
}
66