Skip to content

Commit 58f9959

Browse files
committed
up: update some code syntax, update readme
1 parent 15029e4 commit 58f9959

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ $loader->addClassMap([
105105

106106
### Optional
107107

108+
它旨在消除过多的if判断
109+
108110
Not use Optional:
109111

110112
```php
@@ -124,9 +126,20 @@ Use Optional:
124126
```php
125127
use 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

src/Util/Optional.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use RuntimeException;
77
use Throwable;
88
use Toolkit\Stdlib\Helper\Valid;
9-
use Toolkit\Stdlib\Obj;
109
use Toolkit\Stdlib\Util\Stream\DataStream;
1110
use UnexpectedValueException;
1211
use function gettype;

src/Util/Pipeline.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff 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();

test/Util/OptionalTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)