Completed
Push — master ( 9b44fb...735dd1 )
by Paweł
12s
created

Option::iterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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