|
| 1 | +<?php declare(strict_types = 0); // lint >= 8.1 |
| 2 | + |
| 3 | +namespace Bug12902; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertNativeType; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +class NarrowsNativeConstantValue |
| 9 | +{ |
| 10 | + private readonly int|float $i; |
| 11 | + |
| 12 | + public function __construct() |
| 13 | + { |
| 14 | + $this->i = 1; |
| 15 | + } |
| 16 | + |
| 17 | + public function doFoo(): void |
| 18 | + { |
| 19 | + assertType('1', $this->i); |
| 20 | + assertNativeType('1', $this->i); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class NarrowsNativeReadonlyUnion { |
| 25 | + private readonly int|float $i; |
| 26 | + |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + $this->i = getInt(); |
| 30 | + assertType('int', $this->i); |
| 31 | + assertNativeType('int', $this->i); |
| 32 | + } |
| 33 | + |
| 34 | + public function doFoo(): void { |
| 35 | + assertType('int', $this->i); |
| 36 | + assertNativeType('int', $this->i); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class NarrowsNativeUnion { |
| 41 | + private int|float $i; |
| 42 | + |
| 43 | + public function __construct() |
| 44 | + { |
| 45 | + $this->i = getInt(); |
| 46 | + assertType('int', $this->i); |
| 47 | + assertNativeType('int', $this->i); |
| 48 | + |
| 49 | + $this->impureCall(); |
| 50 | + assertType('float|int', $this->i); |
| 51 | + assertNativeType('float|int', $this->i); |
| 52 | + } |
| 53 | + |
| 54 | + public function doFoo(): void { |
| 55 | + assertType('float|int', $this->i); |
| 56 | + assertNativeType('float|int', $this->i); |
| 57 | + } |
| 58 | + |
| 59 | + /** @phpstan-impure */ |
| 60 | + public function impureCall(): void {} |
| 61 | +} |
| 62 | + |
| 63 | +class NarrowsStaticNativeUnion { |
| 64 | + private static int|float $i; |
| 65 | + |
| 66 | + public function __construct() |
| 67 | + { |
| 68 | + self::$i = getInt(); |
| 69 | + assertType('int', self::$i); |
| 70 | + assertNativeType('int', self::$i); |
| 71 | + |
| 72 | + $this->impureCall(); |
| 73 | + assertType('int', self::$i); // should be float|int |
| 74 | + assertNativeType('int', self::$i); // should be float|int |
| 75 | + } |
| 76 | + |
| 77 | + public function doFoo(): void { |
| 78 | + assertType('float|int', self::$i); |
| 79 | + assertNativeType('float|int', self::$i); |
| 80 | + } |
| 81 | + |
| 82 | + /** @phpstan-impure */ |
| 83 | + public function impureCall(): void {} |
| 84 | +} |
| 85 | + |
| 86 | +function getInt(): int { |
| 87 | + return 1; |
| 88 | +} |
0 commit comments