|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug7948; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class HelloWorld |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param string|array<string, mixed> $name |
| 11 | + * @param mixed $value |
| 12 | + */ |
| 13 | + public function testMixed($name, $value): void |
| 14 | + { |
| 15 | + if (is_array($name)) { |
| 16 | + $value = null; |
| 17 | + } |
| 18 | + |
| 19 | + if (is_array($name)) { |
| 20 | + assertType('null', $value); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @param string|array<string, mixed> $name |
| 26 | + * @param int $value |
| 27 | + */ |
| 28 | + public function testInt($name, $value): void |
| 29 | + { |
| 30 | + if (is_array($name)) { |
| 31 | + $value = null; |
| 32 | + } |
| 33 | + |
| 34 | + if (is_array($name)) { |
| 35 | + assertType('null', $value); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Assigned value (5) is a subtype of the original type (int|string), |
| 41 | + * so the merged type absorbs it just like null|mixed does. |
| 42 | + * |
| 43 | + * @param string|array<string, mixed> $name |
| 44 | + * @param int|string $value |
| 45 | + */ |
| 46 | + public function testSubtype($name, $value): void |
| 47 | + { |
| 48 | + if (is_array($name)) { |
| 49 | + $value = 5; |
| 50 | + } |
| 51 | + |
| 52 | + if (is_array($name)) { |
| 53 | + assertType('5', $value); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param string|array<string, mixed> $name |
| 59 | + * @param mixed $value |
| 60 | + */ |
| 61 | + public function testMixedNegated($name, $value): void |
| 62 | + { |
| 63 | + if (!is_array($name)) { |
| 64 | + $value = null; |
| 65 | + } |
| 66 | + |
| 67 | + if (!is_array($name)) { |
| 68 | + assertType('null', $value); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Boolean-flag guard variant: assigning null to a ?string absorbs into |
| 74 | + * string|null, so $cwd must re-narrow to null under the repeated flag check. |
| 75 | + */ |
| 76 | + public function testBooleanFlag(?string $cwd, bool $initialClone = false): void |
| 77 | + { |
| 78 | + if ($initialClone) { |
| 79 | + $cwd = null; |
| 80 | + } |
| 81 | + |
| 82 | + if ($initialClone) { |
| 83 | + assertType('null', $cwd); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments