|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug15430; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +/** @template T */ |
| 10 | +final class Box |
| 11 | +{ |
| 12 | + /** @param T $value */ |
| 13 | + public function __construct(private mixed $value) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + /** @return T */ |
| 18 | + public function get(): mixed |
| 19 | + { |
| 20 | + return $this->value; |
| 21 | + } |
| 22 | + |
| 23 | + /** @param T $value */ |
| 24 | + public function set(mixed $value): void |
| 25 | + { |
| 26 | + $this->value = $value; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @template U |
| 31 | + * @param U $value |
| 32 | + * @return self<U> |
| 33 | + */ |
| 34 | + public static function of(mixed $value): self |
| 35 | + { |
| 36 | + return new self($value); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** @param callable(): void $callback */ |
| 41 | +function repeat(int $times, callable $callback): void |
| 42 | +{ |
| 43 | + for ($i = 0; $i < $times; $i++) { |
| 44 | + $callback(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @template U |
| 50 | + * @param U $value |
| 51 | + * @return Box<U> |
| 52 | + */ |
| 53 | +function wrap(mixed $value): Box |
| 54 | +{ |
| 55 | + return new Box($value); |
| 56 | +} |
| 57 | + |
| 58 | +function test(): void |
| 59 | +{ |
| 60 | + $count = new Box(0); |
| 61 | + |
| 62 | + repeat(5, function () use ($count): void { |
| 63 | + $count->set($count->get() + 1); |
| 64 | + }); |
| 65 | + |
| 66 | + assertType('Bug15430\Box<int>', $count); |
| 67 | + |
| 68 | + if ($count->get() === 5) { |
| 69 | + echo 'reached at runtime'; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function straight(): void |
| 74 | +{ |
| 75 | + $count = new Box(0); |
| 76 | + $count->set($count->get() + 1); |
| 77 | + assertType('Bug15430\Box<int>', $count); |
| 78 | +} |
| 79 | + |
| 80 | +function loop(): void |
| 81 | +{ |
| 82 | + $count = new Box(0); |
| 83 | + for ($i = 0; $i < 5; $i++) { |
| 84 | + $count->set($count->get() + 1); |
| 85 | + } |
| 86 | + assertType('Bug15430\Box<int>', $count); |
| 87 | +} |
| 88 | + |
| 89 | +function arrowFunction(): void |
| 90 | +{ |
| 91 | + $count = new Box(0); |
| 92 | + repeat(5, fn () => $count->set($count->get() + 1)); |
| 93 | + assertType('Bug15430\Box<int>', $count); |
| 94 | +} |
| 95 | + |
| 96 | +function staticFactory(): void |
| 97 | +{ |
| 98 | + $count = Box::of(0); |
| 99 | + repeat(5, function () use ($count): void { |
| 100 | + $count->set($count->get() + 1); |
| 101 | + }); |
| 102 | + assertType('Bug15430\Box<int>', $count); |
| 103 | +} |
| 104 | + |
| 105 | +function genericFunction(): void |
| 106 | +{ |
| 107 | + $count = wrap(0); |
| 108 | + repeat(5, function () use ($count): void { |
| 109 | + $count->set($count->get() + 1); |
| 110 | + }); |
| 111 | + assertType('Bug15430\Box<int>', $count); |
| 112 | +} |
| 113 | + |
| 114 | +function concat(): void |
| 115 | +{ |
| 116 | + $s = new Box('a'); |
| 117 | + repeat(5, function () use ($s): void { |
| 118 | + $s->set($s->get() . 'b'); |
| 119 | + }); |
| 120 | + assertType('Bug15430\Box<string>', $s); |
| 121 | +} |
| 122 | + |
| 123 | +function append(): void |
| 124 | +{ |
| 125 | + $s = new Box([]); |
| 126 | + repeat(5, function () use ($s): void { |
| 127 | + $a = $s->get(); |
| 128 | + $a[] = 1; |
| 129 | + $s->set($a); |
| 130 | + }); |
| 131 | + assertType('Bug15430\Box<list<int>>', $s); |
| 132 | +} |
| 133 | + |
| 134 | +function shape(): void |
| 135 | +{ |
| 136 | + $s = new Box(['a' => 1]); |
| 137 | + repeat(5, function () use ($s): void { |
| 138 | + $s->set(['a' => $s->get()['a'] + 1]); |
| 139 | + }); |
| 140 | + assertType('Bug15430\Box<non-empty-array<string, int>>', $s); |
| 141 | +} |
| 142 | + |
| 143 | +function writtenBackUnchanged(): void |
| 144 | +{ |
| 145 | + $b = new Box(0); |
| 146 | + $b->set($b->get()); |
| 147 | + assertType('Bug15430\Box<0>', $b); |
| 148 | +} |
0 commit comments