TryCatch
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 206
c 0
b 0
f 0
wmc 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scalp\Utils;
6
7
use Scalp\Option;
8
9
abstract class TryCatch
10
{
11
    /**
12
     * Returns `true` if the `TryCatch` is a `Failure`, `false` otherwise.
13
     *
14
     * def isFailure: Boolean
15
     *
16
     * @return bool
17
     */
18
    abstract public function isFailure(): bool;
19
20
    /**
21
     * Returns `true` if the `TryCatch` is a `Success`, `false` otherwise.
22
     *
23
     * def isSuccess: Boolean
24
     *
25
     * @return bool
26
     */
27
    abstract public function isSuccess(): bool;
28
29
    /**
30
     * Returns the value from this `Success` or the given `default` argument if this is a `Failure`.
31
     *
32
     * ''Note:'': This will throw an exception if it is not a success and default throws an exception.
33
     *
34
     * def getOrElse[U >: T](default: => U): U
35
     *
36
     * @param mixed $default
37
     *
38
     * @return mixed
39
     */
40
    abstract public function getOrElse($default);
41
42
    /**
43
     * Returns this `TryCatch` if it's a `Success` or the given `default` argument if this is a `Failure`.
44
     *
45
     * def orElse[U >: T](default: => Try[U]): Try[U]
46
     *
47
     * @param TryCatch $default
48
     *
49
     * @return TryCatch
50
     */
51
    abstract public function orElse(TryCatch $default): TryCatch;
52
53
    /**
54
     * Returns the value from this `Success` or throws the exception if this is a `Failure`.
55
     *
56
     * def get: T
57
     *
58
     * @return mixed
59
     */
60
    abstract public function get();
61
62
    /**
63
     * Applies the given function `f` if this is a `Success`, otherwise returns `Unit` if this is a `Failure`.
64
     *
65
     * ''Note:'' If `f` throws, then this method may throw an exception.
66
     *
67
     * def foreach[U](f: T => U): Unit
68
     *
69
     * @param callable $f
70
     */
71
    abstract public function foreach(callable $f): void;
72
73
    /**
74
     * Returns the given function applied to the value from this `Success` or returns this if this is a `Failure`.
75
     *
76
     * def flatMap[U](f: T => Try[U]): Try[U]
77
     *
78
     * @param callable $f
79
     *
80
     * @return TryCatch
81
     */
82
    abstract public function flatMap(callable $f): TryCatch;
83
84
    /*
85
     * Maps the given function to the value from this `Success` or returns this if this is a `Failure`.
86
     *
87
     * def map[U](f: T => U): Try[U]
88
     *
89
     * @param callable $f
90
     * @return TryCatch
91
     */
92
    abstract public function map(callable $f): TryCatch;
93
94
    /*
95
     * Applies the given partial function to the value from this `Success` or returns this if this is a `Failure`.
96
     *
97
     * def collect[U](pf: PartialFunction[T, U]): Try[U]
98
     *
99
     * @param PartialFunction $f
100
     * @return TryCatch
101
     */
102
//    abstract public function collect(PartialFunction $pf): TryCatch;
103
104
    /*
105
     * Converts this to a `Failure` if the predicate is not satisfied.
106
     *
107
     * def filter(p: T => Boolean): Try[T]
108
     *
109
     * @param callable $p
110
     * @return TryCatch
111
     */
112
    abstract public function filter(callable $p): TryCatch;
113
114
    /* Creates a non-strict filter, which eventually converts this to a `Failure`
115
     *  if the predicate is not satisfied.
116
     *
117
     *  Note: unlike filter, withFilter does not create a new Try.
118
     *        Instead, it restricts the domain of subsequent
119
     *        `map`, `flatMap`, `foreach`, and `withFilter` operations.
120
     *
121
     * As Try is a one-element collection, this may be a bit overkill,
122
     * but it's consistent with withFilter on Option and the other collections.
123
     *
124
     *  [@]param p the predicate used to test elements.
125
     *  [@]return  an object of class `WithFilter`, which supports
126
     *             `map`, `flatMap`, `foreach`, and `withFilter` operations.
127
     *             All these operations apply to those elements of this Try
128
     *             which satisfy the predicate `p`.
129
     *
130
     * [@]inline final def withFilter(p: T => Boolean): WithFilter = new WithFilter(p)
131
     *
132
     * @param callable $p
133
     * @return WithFilter
134
     */
135
//    abstract public function withFilter(callable $p): WithFilter;
136
137
    /*
138
     * Applies the given function `f` if this is a `Failure`, otherwise returns this if this is a `Success`.
139
     * This is like `flatMap` for the exception.
140
     *
141
     * def recoverWith[U >: T](@deprecatedName('f) pf: PartialFunction[Throwable, Try[U]]): Try[U]
0 ignored issues
show
Bug introduced by
The type Scalp\Utils\PartialFunction 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...
142
     *
143
     * @param PartialFunction $pf
144
     * @return TryCatch
145
     */
146
//    abstract public function recoverWith(PartialFunction $pf): TryCatch;
147
    abstract public function recoverWith(callable $pf): TryCatch;
148
149
    /*
150
     * Applies the given function `f` if this is a `Failure`, otherwise returns this if this is a `Success`.
151
     * This is like map for the exception.
152
     *
153
     * def recover[U >: T](@deprecatedName('f) pf: PartialFunction[Throwable, U]): Try[U]
154
     *
155
     * @param PartialFunction $pf
156
     * @return TryCatch
157
     */
158
//    abstract public function recover(PartialFunction $pf): TryCatch;
159
    abstract public function recover(callable $pf): TryCatch;
160
161
    /*
162
     * Returns `None` if this is a `Failure` or a `Some` containing the value if this is a `Success`.
163
     *
164
     * def toOption: Option[T]
165
     */
166
    abstract public function toOption(): Option;
167
168
    /*
169
     * Transforms a nested `Try`, ie, a `Try` of type `Try[Try[T]]`,
170
     * into an un-nested `Try`, ie, a `Try` of type `Try[T]`.
171
     *
172
     * def flatten[U](implicit ev: T <:< Try[U]): Try[U]
173
     */
174
    abstract public function flatten(): TryCatch;
175
176
    /*
177
     * Inverts this `Try`. If this is a `Failure`, returns its exception wrapped in a `Success`.
178
     * If this is a `Success`, returns a `Failure` containing an `UnsupportedOperationException`.
179
     *
180
     * def failed: Try[Throwable]
181
     */
182
    abstract public function failed(): TryCatch;
183
184
    /*
185
     * Completes this `Try` by applying the function `f` to this if this is of type `Failure`, or conversely, by applying
186
     * `s` if this is a `Success`.
187
     *
188
     * def transform[U](s: T => Try[U], f: Throwable => Try[U]): Try[U]
189
     */
190
    abstract public function transform(callable $s, callable $f): TryCatch;
191
192
    /*
193
     * Returns `Left` with `Throwable` if this is a `Failure`, otherwise returns `Right` with `Success` value.
194
     *
195
     * def toEither: Either[Throwable, T]
196
     */
197
//    abstract public function toEither(): Either;
198
199
    /*
200
     * Applies `fa` if this is a `Failure` or `fb` if this is a `Success`.
201
     * If `fb` is initially applied and throws an exception,
202
     * then `fa` is applied with this exception.
203
     *
204
     * @example {{{
205
     * val result: Try[Throwable, Int] = Try { string.toInt }
206
     * log(result.fold(
207
     *   ex => "Operation failed with " + ex,
208
     *   v => "Operation produced value: " + v
209
     * ))
210
     * }}}
211
     *
212
     * def fold[U](fa: Throwable => U, fb: T => U): U
213
     */
214
    abstract public function fold(callable $fa, callable $fb);
215
}
216