Skip to content

Commit 0b95c5a

Browse files
committed
Add usage examples to inline documentation for ArrayOfStrings, OptionalFail, EarlyFail, and LateFail classes
1 parent c1fdbe8 commit 0b95c5a

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/Usage/Example/ArrayOfStrings.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
* items via `value()`, supports iteration, and can be converted to a plain
2020
* array or JSON.
2121
*
22+
* Example
23+
* - $a = ArrayOfStrings::fromArray(['foo', 'bar']);
24+
* $a->toArray(); // ['foo', 'bar']
25+
* foreach ($a as $item) { $item->toString(); // 'foo', then 'bar' }
26+
* - ArrayOfStrings::fromArray([]); // throws TypeException('Expected non-empty array')
27+
* - ArrayOfStrings::fromArray([123, 45.6]); // casts to strings → ['123', '45.6']
28+
*
2229
* @internal
2330
*
2431
* @psalm-internal PhpTypedValues

src/Usage/Example/EarlyFail.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
* All fields must be valid on creation time. Any invalid input immediately
2020
* raises a domain exception from the underlying typed values.
2121
*
22+
* Example
23+
* - $p = EarlyFail::fromScalars(id: 1, firstName: 'Alice', height: 170.5);
24+
* $p->getFirstName()->toString(); // 'Alice'
25+
* - EarlyFail::fromScalars(id: 0, firstName: 'Alice', height: 170); // throws IntegerTypeException
26+
* - EarlyFail::fromScalars(id: 1, firstName: '', height: 170); // throws StringTypeException
27+
* - EarlyFail::fromScalars(id: 1, firstName: 'Alice', height: -1); // throws FloatTypeException
28+
*
2229
* @internal
2330
*
2431
* @psalm-internal PhpTypedValues

src/Usage/Example/LateFail.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
* - `firstName` and `height` accept mixed inputs and may become `Undefined`;
2020
* accessing their string/primitive values may fail later.
2121
*
22+
* Example
23+
* - $p = LateFail::fromScalars(id: 1, firstName: 'Bob', height: '170.5');
24+
* $p->getHeight()->toString(); // '170.5'
25+
* - $p = LateFail::fromScalars(id: 1, firstName: '', height: null);
26+
* $p->getFirstName()->toString(); // late fail (UndefinedTypeException)
27+
* $p->getHeight()->toString(); // late fail (UndefinedTypeException)
28+
*
2229
* @internal
2330
*
2431
* @psalm-internal PhpTypedValues

src/Usage/Example/OptionalFail.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
* - `firstName` uses `tryFromMixed` and may be `Undefined` (late fail on access).
2323
* - `height` fails early only when provided; `null` becomes `Undefined` (late fail).
2424
*
25+
* Example
26+
* - $p = OptionalFail::fromScalars(id: 1, firstName: 'Alice', height: 170);
27+
* $p->jsonSerialize(); // ['id' => '1', 'firstName' => 'Alice', 'height' => '170']
28+
* - $p = OptionalFail::fromScalars(id: 1, firstName: '', height: null);
29+
* $p->getFirstName()->toString(); // late fail (UndefinedTypeException)
30+
* $p->getHeight()->toString(); // late fail (UndefinedTypeException)
31+
*
2532
* @internal
2633
*
2734
* @psalm-internal PhpTypedValues

0 commit comments

Comments
 (0)