File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed
Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -502,6 +502,69 @@ $variance->getCount();
502502// int(100)
503503```
504504
505+ ## Type Safety
506+
507+ The library provides extensive generic type tracking, whether you prefer method chaining or not.
508+
509+ Given the following type:
510+
511+ ``` php
512+ class Foo
513+ {
514+ public function __construct(
515+ public int $n,
516+ ) {}
517+
518+ public function bar(): string
519+ {
520+ return "{$this->n}\n";
521+ }
522+ }
523+ ```
524+
525+ A static analyzer will correctly infer that these examples are sound:
526+
527+ ``` php
528+ $pipeline = Pipeline\take(['a' => 1, 'b' => 2, 'c' => 3])
529+ ->map(fn(int $n): int => $n * 2)
530+ ->cast(fn(int $n): Foo => new Foo($n));
531+
532+ foreach ($pipeline as $value) {
533+ echo $value->bar();
534+ }
535+
536+ $pipeline = Pipeline\take(['a' => 1, 'b' => 2, 'c' => 3]);
537+ $pipeline->map(fn(int $n): int => $n * 2);
538+ $pipeline->cast(fn(int $n): FooB => new FooB($n));
539+
540+ foreach ($pipeline as $value) {
541+ echo $value->bar();
542+ }
543+ ```
544+
545+ But if you were to change the signature of the class:
546+
547+ ``` diff
548+ class Foo
549+ {
550+ public function __construct(
551+ - public int $n,
552+ + public string $n,
553+ ) {}
554+
555+ - public function bar(): string
556+ + public function baz(): string
557+ {
558+ return "{$this->n}\n";
559+ }
560+ }
561+ ```
562+
563+ PHPStan will correctly note that:
564+
565+ - The first parameter of class ` Foo ` constructor expects ` string ` but ` int ` given.
566+ - There is a call to an undefined method ` Foo::bar() ` .
567+
505568# Contributions
506569
507570Contributions to documentation and test cases are welcome. Bug reports are welcome too.
You can’t perform that action at this time.
0 commit comments