Issues (50)

src/Util/OptionalInterface.php (2 issues)

1
<?php
2
3
namespace Bdf\Collection\Util;
4
5
use Bdf\Collection\Stream\Streamable;
6
use RuntimeException;
7
use Throwable;
8
9
/**
10
 * Wrap an optional (nullable) value into an object
11
 * Optional is a way for handling null values, and create simple null objects
12
 *
13
 * @template T
14
 * @extends Streamable<T, int>
15
 */
16
interface OptionalInterface extends Streamable
17
{
18
    /**
19
     * Filter the optional value
20
     *
21
     * <code>
22
     * $opt
23
     *     ->filter(function ($e) { return $e[0] === 'A'; })
24
     *     ->apply(function ($e) { ... })
25
     * ;
26
     * </code>
27
     *
28
     * @param callable(T):bool $predicate A predicate function which contains the element as parameter, and return true or false
29
     *
30
     * @return OptionalInterface<T>
31
     */
32
    public function filter(callable $predicate): OptionalInterface;
33
34
    /**
35
     * Transform the element if it's present
36
     *
37
     * - If the current Optional is empty, will return an empty Optional
38
     * - If the transformer returns null, will also return an empty Optional
39
     * - Else wrap the result value into an Optional
40
     *
41
     * The map() method can be chained, also with first() method
42
     *
43
     * <code>
44
     * $opt->map(function ($obj) {
45
     *     return $obj->get();
46
     * });
47
     * </code>
48
     *
49
     * @template R
50
     * @param callable(T):R $transformer The transformer function. Take as parameter the element and return the transformed value
51
     *
52
     * @return OptionalInterface<R> The result wrap into an optional
53
     */
54
    public function map(callable $transformer): OptionalInterface;
55
56
    /**
57
     * Apply the consumer on the element if it's present
58
     *
59
     * <code>
60
     * $opt->apply(function ($e) {
61
     *     // $e is present and not null
62
     * });
63
     * </code>
64
     *
65
     * @param callable(T):void $consumer
66
     *
67
     * @return void
68
     */
69
    public function apply(callable $consumer): void;
70
71
    /**
72
     * Get the current Optional value if it's present, or the parameter value if not present
73
     *
74
     * <code>
75
     * Optional::empty()->or(123); // Return 123
76
     * Optional::of(456)->or(123); // Return 456
77
     * </code>
78
     *
79
     * @param T $value The default value
0 ignored issues
show
The type Bdf\Collection\Util\T 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...
80
     *
81
     * @return T
82
     */
83
    public function or($value);
84
85
    /**
86
     * Get the current value if present, or throws an exception
87
     *
88
     * @param class-string<Throwable>|Throwable $exception The exception instance or class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Throwable>|Throwable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Throwable>|Throwable.
Loading history...
89
     *
90
     * @return T The stored value
91
     *
92
     * @throws Throwable
93
     */
94
    public function orThrows($exception = RuntimeException::class);
95
96
    /**
97
     * Get the current value if present, or return the supplier result
98
     *
99
     * <code>
100
     * Optional::of(456)->orSupply('rand'); // Return 456
101
     * Optional::empty()->orSupply('rand'); // Return random number
102
     * </code>
103
     *
104
     * @param callable():T $supplier Generate the default value
105
     *
106
     * @return T
107
     */
108
    public function orSupply(callable $supplier);
109
110
    /**
111
     * Check if the Optional value is present or not
112
     *
113
     * @return boolean
114
     */
115
    public function present(): bool;
116
117
    /**
118
     * Get the current stored value
119
     *
120
     * @return T|null
121
     */
122
    public function get();
123
124
    /**
125
     * Delegate call to contained object if present, and wrap the return value into an Optional
126
     *
127
     * @param string $name The method name
128
     * @param array $arguments The method parameters
129
     *
130
     * @return OptionalInterface
131
     */
132
    public function __call(string $name, array $arguments): OptionalInterface;
133
134
    /**
135
     * Get a property from the contained object if present, and wrap the value into an Optional
136
     *
137
     * @param string $name The property name
138
     *
139
     * @return OptionalInterface
140
     */
141
    public function __get(string $name): OptionalInterface;
142
143
    /**
144
     * Check if the contained object contains the given property
145
     * If the Optional is empty will always return false
146
     *
147
     * @param string $name
148
     *
149
     * @return boolean
150
     */
151
    public function __isset(string $name): bool;
152
153
    /**
154
     * Set the contains object property value if it's present
155
     *
156
     * @param string $name The property name
157
     * @param mixed $value The new value
158
     *
159
     * @return void
160
     */
161
    public function __set(string $name, $value): void;
162
}
163