|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bug12355; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +/** |
| 8 | + * @phpstan-type AnimalData array{ |
| 9 | + * animalSpecificData: ValidType, |
| 10 | + * habitat?: string, |
| 11 | + * } |
| 12 | + * @template ValidType of array{ |
| 13 | + * name: string, |
| 14 | + * ..., |
| 15 | + * } |
| 16 | + */ |
| 17 | +abstract class Animal |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @param AnimalData $arg |
| 21 | + */ |
| 22 | + public function __construct(array $arg) { |
| 23 | + assertType('ValidType of array{name: string} (class Bug12355\Animal, argument)', $arg['animalSpecificData']); |
| 24 | + if (isset($arg['habitat'])) { |
| 25 | + //do things |
| 26 | + } |
| 27 | + assertType('ValidType of array{name: string} (class Bug12355\Animal, argument)', $arg['animalSpecificData']); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * @template T of array{name: string, ...} |
| 33 | + * @param array{first: T, second: T, flag?: bool} $arg |
| 34 | + */ |
| 35 | +function testMergeWithDifferentObjects(array $arg): void |
| 36 | +{ |
| 37 | + assertType('T of array{name: string} (function Bug12355\testMergeWithDifferentObjects(), argument)', $arg['first']); |
| 38 | + |
| 39 | + // Modifying $arg in one branch causes different ConstantArrayType objects |
| 40 | + if (isset($arg['flag'])) { |
| 41 | + $arg['extra'] = true; |
| 42 | + } |
| 43 | + |
| 44 | + // After scope merge, $arg's value types for 'first' and 'second' go through |
| 45 | + // ConstantArrayType::mergeWith() which uses new self() — stripping TemplateConstantArrayType |
| 46 | + assertType('T of array{name: string} (function Bug12355\testMergeWithDifferentObjects(), argument)', $arg['first']); |
| 47 | + assertType('T of array{name: string} (function Bug12355\testMergeWithDifferentObjects(), argument)', $arg['second']); |
| 48 | +} |
0 commit comments