Skip to content

Commit b27796a

Browse files
committed
documentation: Add examples to Result methods
1 parent 66f23f4 commit b27796a

File tree

7 files changed

+331
-62
lines changed

7 files changed

+331
-62
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function writeInFile(string $filepath, string $data): Result {
104104
$res = file_put_contents($filepath, $data);
105105

106106
if ($res === false) {
107-
return Result\err("failed to write in $path");
107+
return Result\err("failed to write in $filepath");
108108
}
109109

110110
return Result\ok($res);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"vimeo/psalm": "^4.22",
3333
"symfony/finder": "^6.1",
3434
"symfony/console": "^6.1",
35-
"texthtml/doctest": "^0.1.0"
35+
"texthtml/doctest": "^0.2"
3636
},
3737
"config": {
3838
"allow-plugins": {

composer.lock

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Option.php

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
* # Examples
1313
*
1414
* ```
15-
* use TH\Maybe\Option;
16-
*
1715
* // @return Option<float>
1816
* function divide(float $numerator, float $denominator): Option {
1917
* if ($denominator === 0.0) {
@@ -50,15 +48,11 @@ interface Option extends \IteratorAggregate
5048
* # Examples
5149
*
5250
* ```
53-
* use TH\Maybe\Option;
54-
*
5551
* $x = Option\some("value");
5652
* self::assertSame($x->expect("fruits are healthy"), "value");
5753
* ```
5854
*
5955
* ```
60-
* use TH\Maybe\Option;
61-
*
6256
* // @var Option<string> $x
6357
* $x = Option\none();
6458
*
@@ -78,15 +72,11 @@ public function expect(string $message): mixed;
7872
* # Examples
7973
*
8074
* ```
81-
* use TH\Maybe\Option;
82-
*
8375
* $x = Option\some("value");
8476
* self::assertSame($x->unwrap(), "value");
8577
* ```
8678
*
8779
* ```
88-
* use TH\Maybe\Option;
89-
*
9080
* // @var Option<string> $x
9181
* $x = Option\none();
9282
*
@@ -105,8 +95,6 @@ public function unwrap(): mixed;
10595
* # Examples
10696
*
10797
* ```
108-
* use TH\Maybe\Option;
109-
*
11098
* self::assertSame(Option\some("car")->unwrapOr("bike"), "car");
11199
* self::assertSame(Option\none()->unwrapOr("bike"), "bike");
112100
* ```
@@ -122,8 +110,6 @@ public function unwrapOr(mixed $default): mixed;
122110
* # Examples
123111
*
124112
* ```
125-
* use TH\Maybe\Option;
126-
*
127113
* $k = 10;
128114
* self::assertSame(Option\some(4)->unwrapOrElse(fn () => 2 * $k), 4);
129115
* self::assertSame(Option\none()->unwrapOrElse(fn () => 2 * $k), 20);
@@ -141,8 +127,6 @@ public function unwrapOrElse(callable $default): mixed;
141127
* # Examples
142128
*
143129
* ```
144-
* use TH\Maybe\Option;
145-
*
146130
* $option = Option\some(4);
147131
* self::assertSame($option->inspect(fn (int $n) => printf("got: %d", $n)), $option); // @prints got: 4
148132
* // @var Option<int> $option
@@ -161,8 +145,6 @@ public function inspect(callable $callback): self;
161145
* # Examples
162146
*
163147
* ```
164-
* use TH\Maybe\Option;
165-
*
166148
* $x = Option\some(2);
167149
* // @var Option<string> $y
168150
* $y = Option\none();
@@ -193,8 +175,6 @@ public function and(Option $right): Option;
193175
* # Examples
194176
*
195177
* ```
196-
* use TH\Maybe\Option;
197-
*
198178
* // @return Option<int>
199179
* function to_exact_int(float $f): Option {
200180
* $i = (int) $f;
@@ -218,8 +198,6 @@ public function andThen(callable $right): Option;
218198
* # Examples
219199
*
220200
* ```
221-
* use TH\Maybe\Option;
222-
*
223201
* $x = Option\some(2);
224202
* // @var Option<int> $y
225203
* $y = Option\none();
@@ -252,8 +230,6 @@ public function or(Option $right): Option;
252230
* # Examples
253231
*
254232
* ```
255-
* use TH\Maybe\Option;
256-
*
257233
* // @return Option<string>
258234
* function nobody(): Option {
259235
* return Option\none();
@@ -280,8 +256,6 @@ public function orElse(callable $right): Option;
280256
* # Examples
281257
*
282258
* ```
283-
* use TH\Maybe\Option;
284-
*
285259
* $x = Option\some(2);
286260
* // @var Option<int> $y
287261
* $y = Option\none();
@@ -311,8 +285,6 @@ public function xor(Option $right): Option;
311285
* # Examples
312286
*
313287
* ```
314-
* use TH\Maybe\Option;
315-
*
316288
* $x = Option\some(2);
317289
* self::assertTrue($x->contains(2));
318290
* $x = Option\some(3);
@@ -332,8 +304,6 @@ public function contains(mixed $value, bool $strict = true): bool;
332304
* # Examples
333305
*
334306
* ```
335-
* use TH\Maybe\Option;
336-
*
337307
* $isEven = fn(int $n) => $n % 2 === 0;
338308
*
339309
* self::assertSame(Option\none()->filter($isEven), Option\none());
@@ -352,8 +322,6 @@ public function filter(callable $predicate): Option;
352322
* # Examples
353323
*
354324
* ```
355-
* use TH\Maybe\Option;
356-
*
357325
* $maybeSomeString = Option\some("Hello, World!");
358326
* $maybeSomeLen = $maybeSomeString->map(strlen(...));
359327
* self::assertEq($maybeSomeLen, Option\some(13));
@@ -372,8 +340,6 @@ public function map(callable $callback): Option;
372340
* # Examples
373341
*
374342
* ```
375-
* use TH\Maybe\Option;
376-
*
377343
* $x = Option\some("foo");
378344
* self::assertSame($x->mapOr(strlen(...), 42), 3);
379345
* // @var Option<string> $x
@@ -395,8 +361,6 @@ public function mapOr(callable $callback, mixed $default): mixed;
395361
* # Examples
396362
*
397363
* ```
398-
* use TH\Maybe\Option;
399-
*
400364
* $k = 21;
401365
* $x = Option\some("foo");
402366
* self::assertSame($x->mapOrElse(strlen(...), fn () => 2 * $k), 3);
@@ -421,8 +385,6 @@ public function mapOrElse(callable $callback, callable $default): mixed;
421385
* # Examples
422386
*
423387
* ```
424-
* use TH\Maybe\Option;
425-
*
426388
* $x = Option\some(1);
427389
* $y = Option\some("hi");
428390
* // @var Option<int> $z
@@ -443,7 +405,7 @@ public function zip(Option $option): Option;
443405
* # Examples
444406
*
445407
* ```
446-
* use TH\Maybe\{Option, Result};
408+
* use TH\Maybe\Result;
447409
*
448410
* self::assertEq(Option\some("foo")->okOr(0), Result\ok("foo"));
449411
* self::assertEq(Option\none()->okOr(0), Result\err(0));
@@ -462,7 +424,7 @@ public function okOr(mixed $err): Result;
462424
* # Examples
463425
*
464426
* ```
465-
* use TH\Maybe\{Option, Result};
427+
* use TH\Maybe\Result;
466428
*
467429
* self::assertEq(Option\some("foo")->okOrElse(fn () => 0), Result\ok("foo"));
468430
* self::assertEq(Option\none()->okOrElse(fn () => 0), Result\err(0));

0 commit comments

Comments
 (0)