EmptyOptional   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 129
ccs 34
cts 34
cp 1
rs 10
c 1
b 0
f 0
wmc 17

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A __get() 0 3 1
A instance() 0 7 2
A stream() 0 3 1
A orSupply() 0 3 1
A map() 0 3 1
A orThrows() 0 7 2
A apply() 0 2 1
A __isset() 0 3 1
A present() 0 3 1
A __set() 0 2 1
A __call() 0 3 1
A get() 0 3 1
A filter() 0 3 1
A or() 0 3 1
1
<?php
2
3
namespace Bdf\Collection\Util;
4
5
use Bdf\Collection\Stream\EmptyStream;
6
use Bdf\Collection\Stream\StreamInterface;
7
use RuntimeException;
8
9
/**
10
 * Optional without value
11
 *
12
 * @internal
13
 */
14
final class EmptyOptional implements OptionalInterface
15
{
16
    /**
17
     * @var EmptyOptional|null
18
     */
19
    private static $instance;
20
21 1
    private function __construct()
22
    {
23 1
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function filter(callable $predicate): OptionalInterface
29
    {
30 1
        return $this;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function map(callable $transformer): OptionalInterface
37
    {
38 1
        return $this;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function apply(callable $consumer): void
45
    {
46 1
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function or($value)
52
    {
53 1
        return $value;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function orSupply(callable $supplier)
60
    {
61 1
        return $supplier();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    public function orThrows($exception = RuntimeException::class)
68
    {
69 2
        if (is_string($exception)) {
70 1
            $exception = new $exception;
71
        }
72
73 2
        throw $exception;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function present(): bool
80
    {
81 2
        return false;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function get()
88
    {
89 1
        return null;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function __call(string $name, array $arguments): OptionalInterface
96
    {
97 1
        return $this;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function __get(string $name): OptionalInterface
104
    {
105 1
        return $this;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function __isset(string $name): bool
112
    {
113 1
        return false;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1
    public function __set(string $name, $value): void
120
    {
121 1
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 1
    public function stream(): StreamInterface
127
    {
128 1
        return EmptyStream::instance();
129
    }
130
131
    /**
132
     * Get the EmptyOptional instance
133
     *
134
     * @return EmptyOptional
135
     */
136 27
    public static function instance(): EmptyOptional
137
    {
138 27
        if (self::$instance) {
139 26
            return self::$instance;
140
        }
141
142 1
        return self::$instance = new EmptyOptional();
143
    }
144
}
145