Option::flatMap()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scalp;
6
7
use function Scalp\Conversion\AnyToString;
8
use Scalp\PatternMatching\CaseClass;
9
use Scalp\PatternMatching\Deconstruction;
10
use function Scalp\Type\restrictCallableReturnType;
0 ignored issues
show
Bug introduced by
The type Scalp\Type\restrictCallableReturnType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use function Scalp\Utils\type;
0 ignored issues
show
Bug introduced by
The type Scalp\Utils\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
abstract class Option implements CaseClass
14
{
15
    use Deconstruction;
16
17
    abstract public function isEmpty(): bool;
18
19
    final public function isDefined(): bool
20
    {
21
        return !$this->isEmpty();
22
    }
23
24
    abstract public function get();
25
26
    final public function getOrElse($default)
27
    {
28
        return $this->isEmpty() ? $default : $this->get();
29
    }
30
31
    final public function orNull()
32
    {
33
        return $this->getOrElse(null);
34
    }
35
36
    final public function map(callable $f): Option
37
    {
38
        return $this->isEmpty() ? $this : Some($f($this->get()));
39
    }
40
41
    final public function fold(callable $ifEmpty, callable $f)
42
    {
43
        return $this->isEmpty() ? $ifEmpty() : $f($this->get());
44
    }
45
46
    final public function flatMap($f): Option
47
    {
48
        restrictCallableReturnType($f, self::class);
49
50
        return $this->isEmpty() ? $this : $f($this->get());
51
    }
52
53
    final public function flatten(): Option
54
    {
55
        return $this->isDefined() && ($this->get() instanceof self) ? $this->get()->flatten() : $this;
56
    }
57
58
    final public function filter(callable $p): Option
59
    {
60
        return ($this->isEmpty() || $p($this->get())) ? $this : None();
61
    }
62
63
    final public function filterNot(callable $p): Option
64
    {
65
        return ($this->isEmpty() || !$p($this->get())) ? $this : None();
66
    }
67
68
    final public function contains($elem): bool
69
    {
70
        return !$this->isEmpty() && $this->get() === $elem;
71
    }
72
73
    final public function exists(callable $p): bool
74
    {
75
        return !$this->isEmpty() && $p($this->get());
76
    }
77
78
    final public function forall(callable $p): bool
79
    {
80
        return $this->isEmpty() || $p($this->get());
81
    }
82
83
    final public function foreach(callable $f): void
84
    {
85
        $this->isEmpty() ?: $f($this->get());
86
    }
87
88
    /**
89
     * This method should be implemented with PartialFunction
90
     * It should return the result of applying `pf` if it's
91
     * defined at option's value, None in other case.
92
     * In scala pf: PartialFunction[A,B] is lifted into A => Option[B].
93
     *
94
     * @param callable $pf
95
     *
96
     * @return Option
97
     */
98
    final public function collect(callable $pf): Option
99
    {
100
        return $this->flatMap($pf);
101
    }
102
103
    final public function orElse(Option $alternative): Option
104
    {
105
        return $this->isEmpty() ? $alternative : $this;
106
    }
107
108
    final public function iterator(): \Iterator
109
    {
110
        return $this->isEmpty() ? new \EmptyIterator() : new \ArrayIterator([$this->get()]);
111
    }
112
113
    /*
114
     * final public function toList(): List
115
     * final public function toRight($left)
116
     * final public function toLeft($right)
117
     */
118
119
    final public function toString(): string
120
    {
121
        return $this->isEmpty() ? 'None' : sprintf('Some[%s](%s)', type($this->get()), AnyToString($this->get()));
122
    }
123
124
    final public function __toString(): string
125
    {
126
        return $this->toString();
127
    }
128
}
129