File tree Expand file tree Collapse file tree 4 files changed +32
-10
lines changed Expand file tree Collapse file tree 4 files changed +32
-10
lines changed Original file line number Diff line number Diff line change @@ -105,6 +105,8 @@ $loader->addClassMap([
105105
106106### Optional
107107
108+ 它旨在消除过多的if判断
109+
108110Not use Optional:
109111
110112``` php
@@ -124,9 +126,20 @@ Use Optional:
124126``` php
125127use Toolkit\Stdlib\Util\Optional;
126128
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');
130143```
131144
132145### PhpDotEnv
Original file line number Diff line number Diff line change 66use RuntimeException ;
77use Throwable ;
88use Toolkit \Stdlib \Helper \Valid ;
9- use Toolkit \Stdlib \Obj ;
109use Toolkit \Stdlib \Util \Stream \DataStream ;
1110use UnexpectedValueException ;
1211use function gettype ;
Original file line number Diff line number Diff line change @@ -37,9 +37,7 @@ public function __construct()
3737 public function add (callable $ stage ): PipelineInterface
3838 {
3939 if ($ stage instanceof $ this ) {
40- $ stage ->add (function ($ payload ) {
41- return $ this ->invokeStage ($ payload );
42- });
40+ $ stage ->add (fn ($ payload ) => $ this ->invokeStage ($ payload ));
4341 }
4442
4543 $ this ->stages ->attach ($ stage );
@@ -49,7 +47,7 @@ public function add(callable $stage): PipelineInterface
4947 /**
5048 * {@inheritdoc}
5149 */
52- public function run (mixed $ payload )
50+ public function run (mixed $ payload ): mixed
5351 {
5452 $ this ->stages ->rewind ();
5553
@@ -59,12 +57,17 @@ public function run(mixed $payload)
5957 /**
6058 * {@inheritdoc}
6159 */
62- public function __invoke (mixed $ payload )
60+ public function __invoke (mixed $ payload ): mixed
6361 {
6462 return $ this ->run ($ payload );
6563 }
6664
67- private function invokeStage ($ payload )
65+ /**
66+ * @param mixed $payload
67+ *
68+ * @return mixed
69+ */
70+ private function invokeStage (mixed $ payload ): mixed
6871 {
6972 $ stage = $ this ->stages ->current ();
7073 $ this ->stages ->next ();
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ public function testOptionalBasic(): void
2323 })->orElse (25 );
2424
2525 $ this ->assertEquals (25 , $ val );
26+
27+ // use arrow syntax:
28+ $ val = $ o ->filter (fn ($ val ) => $ val > 25 )->orElse (25 );
29+ $ this ->assertEquals (25 , $ val );
2630 }
2731
2832 public function testOptional_or (): void
@@ -36,7 +40,10 @@ public function testOptional_or(): void
3640 $ val = $ o ->or (function () {
3741 return Optional::of (23 );
3842 })->get ();
43+ $ this ->assertEquals (23 , $ val );
3944
45+ // use arrow syntax:
46+ $ val = $ o ->or (fn () => Optional::of (23 ))->get ();
4047 $ this ->assertEquals (23 , $ val );
4148 }
4249}
You can’t perform that action at this time.
0 commit comments