Success::flatMap()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scalp\Utils;
6
7
use Scalp\Exception\NoSuchElementException;
8
use Scalp\Exception\UnsupportedOperationException;
9
use function Scalp\Conversion\AnyToString;
10
use Scalp\Option;
11
use function Scalp\Some;
12
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...
13
14
final class Success extends TryCatch
15
{
16
    private $value;
17
18
    public function __construct($value)
19
    {
20
        $this->value = $value;
21
    }
22
23
    public function isFailure(): bool
24
    {
25
        return false;
26
    }
27
28
    public function isSuccess(): bool
29
    {
30
        return true;
31
    }
32
33
    public function getOrElse($default)
34
    {
35
        return $this->value;
36
    }
37
38
    public function orElse(TryCatch $default): TryCatch
39
    {
40
        return $this;
41
    }
42
43
    public function get()
44
    {
45
        return $this->value;
46
    }
47
48
    public function foreach(callable $f): void
49
    {
50
        $f($this->value);
51
    }
52
53
    public function flatMap(callable $f): TryCatch
54
    {
55
        restrictCallableReturnType($f, TryCatch::class);
56
57
        try {
58
            return $f($this->value);
59
        } catch (\Throwable $error) {
60
            return Failure($error);
61
        }
62
    }
63
64
    public function map(callable $f): TryCatch
65
    {
66
        return TryCatch(delay($f, $this->value));
67
    }
68
69
    public function filter(callable $p): TryCatch
70
    {
71
        try {
72
            return ($p($this->value)) ? $this : Failure(new NoSuchElementException("Predicate does not hold for {$this->value}"));
73
        } catch (\Throwable $error) {
74
            return Failure($error);
75
        }
76
    }
77
78
    public function recoverWith(callable $pf): TryCatch
79
    {
80
        return $this;
81
    }
82
83
    public function recover(callable $pf): TryCatch
84
    {
85
        return $this;
86
    }
87
88
    public function toOption(): Option
89
    {
90
        return Some($this->get());
91
    }
92
93
    public function flatten(): TryCatch
94
    {
95
        return ($this->value instanceof TryCatch)
96
            ? $this->value->flatten()
97
            : $this;
98
    }
99
100
    public function failed(): TryCatch
101
    {
102
        return Failure(new UnsupportedOperationException('Success::failed'));
103
    }
104
105
    public function transform(callable $s, callable $f): TryCatch
106
    {
107
        return $this->flatMap($s);
108
    }
109
110
    public function fold(callable $fa, callable $fb)
111
    {
112
        try {
113
            return $fb($this->value);
114
        } catch (\Throwable $error) {
115
            return $fa($error);
116
        }
117
    }
118
119
    public function __toString(): string
120
    {
121
        return sprintf(
122
            'Success[%s](%s)',
123
            type($this->value),
124
            AnyToString($this->value)
125
        );
126
    }
127
}
128