File tree 4 files changed +32
-10
lines changed
4 files changed +32
-10
lines changed Original file line number Diff line number Diff line change @@ -105,6 +105,8 @@ $loader->addClassMap([
105
105
106
106
### Optional
107
107
108
+ 它旨在消除过多的if判断
109
+
108
110
Not use Optional:
109
111
110
112
``` php
@@ -124,9 +126,20 @@ Use Optional:
124
126
``` php
125
127
use Toolkit\Stdlib\Util\Optional;
126
128
127
- $username = Optional::ofNullable($userModel)->map(function ($userModel) {
128
- return $userModel->name;
129
- })->orElse('unknown');
129
+ $username = Optional::ofNullable($userModel)
130
+ ->map(function ($userModel) {
131
+ return $userModel->name;
132
+ })->orElse('unknown');
133
+ ```
134
+
135
+ Use arrow syntax:
136
+
137
+ ``` php
138
+ use Toolkit\Stdlib\Util\Optional;
139
+
140
+ $username = Optional::ofNullable($userModel)
141
+ ->map(fn($userModel) => $userModel->name)
142
+ ->orElse('unknown');
130
143
```
131
144
132
145
### PhpDotEnv
Original file line number Diff line number Diff line change 6
6
use RuntimeException ;
7
7
use Throwable ;
8
8
use Toolkit \Stdlib \Helper \Valid ;
9
- use Toolkit \Stdlib \Obj ;
10
9
use Toolkit \Stdlib \Util \Stream \DataStream ;
11
10
use UnexpectedValueException ;
12
11
use function gettype ;
Original file line number Diff line number Diff line change @@ -37,9 +37,7 @@ public function __construct()
37
37
public function add (callable $ stage ): PipelineInterface
38
38
{
39
39
if ($ stage instanceof $ this ) {
40
- $ stage ->add (function ($ payload ) {
41
- return $ this ->invokeStage ($ payload );
42
- });
40
+ $ stage ->add (fn ($ payload ) => $ this ->invokeStage ($ payload ));
43
41
}
44
42
45
43
$ this ->stages ->attach ($ stage );
@@ -49,7 +47,7 @@ public function add(callable $stage): PipelineInterface
49
47
/**
50
48
* {@inheritdoc}
51
49
*/
52
- public function run (mixed $ payload )
50
+ public function run (mixed $ payload ): mixed
53
51
{
54
52
$ this ->stages ->rewind ();
55
53
@@ -59,12 +57,17 @@ public function run(mixed $payload)
59
57
/**
60
58
* {@inheritdoc}
61
59
*/
62
- public function __invoke (mixed $ payload )
60
+ public function __invoke (mixed $ payload ): mixed
63
61
{
64
62
return $ this ->run ($ payload );
65
63
}
66
64
67
- private function invokeStage ($ payload )
65
+ /**
66
+ * @param mixed $payload
67
+ *
68
+ * @return mixed
69
+ */
70
+ private function invokeStage (mixed $ payload ): mixed
68
71
{
69
72
$ stage = $ this ->stages ->current ();
70
73
$ this ->stages ->next ();
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ public function testOptionalBasic(): void
23
23
})->orElse (25 );
24
24
25
25
$ this ->assertEquals (25 , $ val );
26
+
27
+ // use arrow syntax:
28
+ $ val = $ o ->filter (fn ($ val ) => $ val > 25 )->orElse (25 );
29
+ $ this ->assertEquals (25 , $ val );
26
30
}
27
31
28
32
public function testOptional_or (): void
@@ -36,7 +40,10 @@ public function testOptional_or(): void
36
40
$ val = $ o ->or (function () {
37
41
return Optional::of (23 );
38
42
})->get ();
43
+ $ this ->assertEquals (23 , $ val );
39
44
45
+ // use arrow syntax:
46
+ $ val = $ o ->or (fn () => Optional::of (23 ))->get ();
40
47
$ this ->assertEquals (23 , $ val );
41
48
}
42
49
}
You can’t perform that action at this time.
0 commit comments