Failure::toOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
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\Utils;
6
7
use function Scalp\None;
8
use Scalp\Option;
9
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...
10
11
final class Failure extends TryCatch
12
{
13
    private $error;
14
15
    public function __construct(\Throwable $error)
16
    {
17
        $this->error = $error;
18
    }
19
20
    public function isFailure(): bool
21
    {
22
        return true;
23
    }
24
25
    public function isSuccess(): bool
26
    {
27
        return false;
28
    }
29
30
    public function getOrElse($default)
31
    {
32
        return $default;
33
    }
34
35
    public function orElse(TryCatch $default): TryCatch
36
    {
37
        return $default;
38
    }
39
40
    public function get(): void
41
    {
42
        throw $this->error;
43
    }
44
45
    public function foreach(callable $f): void
46
    {
47
    }
48
49
    public function flatMap(callable $f): TryCatch
50
    {
51
        return $this;
52
    }
53
54
    public function map(callable $f): TryCatch
55
    {
56
        return $this;
57
    }
58
59
    public function filter(callable $p): TryCatch
60
    {
61
        return $this;
62
    }
63
64
    public function recoverWith(callable $pf): TryCatch
65
    {
66
        restrictCallableReturnType($pf, TryCatch::class);
67
68
        try {
69
            return $pf($this->error);
70
        } catch (\Throwable $error) {
71
            return Failure($error);
72
        }
73
    }
74
75
    public function recover(callable $pf): TryCatch
76
    {
77
        try {
78
            return Success($pf($this->error));
79
        } catch (\Throwable $error) {
80
            return Failure($error);
81
        }
82
    }
83
84
    public function toOption(): Option
85
    {
86
        return None();
87
    }
88
89
    public function flatten(): TryCatch
90
    {
91
        return $this;
92
    }
93
94
    public function failed(): TryCatch
95
    {
96
        return Success($this->error);
97
    }
98
99
    public function transform(callable $s, callable $f): TryCatch
100
    {
101
        return $this->recoverWith($f);
102
    }
103
104
    public function fold(callable $fa, callable $fb)
105
    {
106
        return $fa($this->error);
107
    }
108
109
    public function __toString(): string
110
    {
111
        return sprintf('Failure[%s]("%s")', get_class($this->error), $this->error->getMessage());
112
    }
113
}
114