ValidTernary   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 6
c 2
b 0
f 1
dl 0
loc 48
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 1
A value() 0 7 2
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use Closure;
6
use Iterator;
7
8
/**
9
 * Class which checks whether the given iterator is valid and calls consequent
10
 * path if it is true and alternate path otherwise.
11
 * @template T
12
 * @implements Scalar<T>
13
 */
14
final class ValidTernary implements Scalar
15
{
16
    /**
17
     * Ctor.
18
     *
19
     * @template TKey
20
     * @template TValue
21
     * @phpstan-param Iterator<TKey, TValue> $origin
22
     * @phpstan-param Closure(Iterator<TKey, TValue>):T $cons
23
     * @phpstan-param Closure(Iterator<TKey, TValue>):T $alter
24
     * @param Iterator $origin iterator which validity is checked.
25
     * @param Closure $cons    consequent path if iterator is valid.
26
     * @param Closure $alter   alternative path if iterator is not valid.
27
     */
28
    public function __construct(
29
        /**
30
         * Iterator which validity is checked.
31
         *
32
         * @phpstan-var Iterator<TKey, TValue>
33
         * @var Iterator
34
         */
35
        private Iterator $origin,
36
37
        /**
38
         * Consequent path if iterator is valid.
39
         *
40
         * @phpstan-var Closure(Iterator<TKey, TValue>):T
41
         * @var Closure
42
         */
43
        private Closure $cons,
44
45
        /**
46
         * Alternate path if iterator is not valid.
47
         *
48
         * @phpstan-var Closure(Iterator<TKey, TValue>):T
49
         * @var Closure
50
         */
51
        private Closure $alter
52
    ) {
53
    }
54
55
    public function value(): mixed
56
    {
57
        return (
58
            ($this->origin->valid()) ?
59
            $this->cons :
60
            $this->alter
61
        )($this->origin);
62
    }
63
}
64