|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bug13828; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class FooBar |
| 8 | +{ |
| 9 | + const FOO_BAR = 'foo'; |
| 10 | + |
| 11 | + /** @return static::FOO_BAR */ |
| 12 | + public function test(): string |
| 13 | + { |
| 14 | + return static::FOO_BAR; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +class BarBaz extends FooBar |
| 19 | +{ |
| 20 | + const FOO_BAR = 'bar'; |
| 21 | +} |
| 22 | + |
| 23 | +function test(FooBar $foo, BarBaz $bar): void |
| 24 | +{ |
| 25 | + assertType("'foo'", $foo->test()); |
| 26 | + assertType("'bar'", $bar->test()); |
| 27 | +} |
| 28 | + |
| 29 | +final class FinalFoo |
| 30 | +{ |
| 31 | + const FOO_BAR = 'foo'; |
| 32 | + |
| 33 | + /** @return static::FOO_BAR */ |
| 34 | + public function test(): string |
| 35 | + { |
| 36 | + return static::FOO_BAR; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function testFinal(FinalFoo $foo): void |
| 41 | +{ |
| 42 | + assertType("'foo'", $foo->test()); |
| 43 | +} |
| 44 | + |
| 45 | +class WithNativeType |
| 46 | +{ |
| 47 | + const string FOO_BAR = 'foo'; |
| 48 | + |
| 49 | + /** @return static::FOO_BAR */ |
| 50 | + public function test(): string |
| 51 | + { |
| 52 | + return static::FOO_BAR; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +class WithNativeTypeChild extends WithNativeType |
| 57 | +{ |
| 58 | + const string FOO_BAR = 'bar'; |
| 59 | +} |
| 60 | + |
| 61 | +function testNativeType(WithNativeType $foo, WithNativeTypeChild $bar): void |
| 62 | +{ |
| 63 | + assertType('string', $foo->test()); |
| 64 | + assertType('string', $bar->test()); |
| 65 | +} |
| 66 | + |
| 67 | +class WithPhpDocType |
| 68 | +{ |
| 69 | + /** @var non-empty-string */ |
| 70 | + const FOO_BAR = 'foo'; |
| 71 | + |
| 72 | + /** @return static::FOO_BAR */ |
| 73 | + public function test(): string |
| 74 | + { |
| 75 | + return static::FOO_BAR; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class WithPhpDocTypeChild extends WithPhpDocType |
| 80 | +{ |
| 81 | + /** @var non-empty-string */ |
| 82 | + const FOO_BAR = 'bar'; |
| 83 | +} |
| 84 | + |
| 85 | +function testPhpDocType(WithPhpDocType $foo, WithPhpDocTypeChild $bar): void |
| 86 | +{ |
| 87 | + assertType('non-empty-string', $foo->test()); |
| 88 | + assertType('non-empty-string', $bar->test()); |
| 89 | +} |
| 90 | + |
| 91 | +class WithBothTypes |
| 92 | +{ |
| 93 | + /** @var non-empty-string */ |
| 94 | + const string FOO_BAR = 'foo'; |
| 95 | + |
| 96 | + /** @return static::FOO_BAR */ |
| 97 | + public function test(): string |
| 98 | + { |
| 99 | + return static::FOO_BAR; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class WithBothTypesChild extends WithBothTypes |
| 104 | +{ |
| 105 | + /** @var non-empty-string */ |
| 106 | + const string FOO_BAR = 'bar'; |
| 107 | +} |
| 108 | + |
| 109 | +function testBothTypes(WithBothTypes $foo, WithBothTypesChild $bar): void |
| 110 | +{ |
| 111 | + assertType('non-empty-string', $foo->test()); |
| 112 | + assertType('non-empty-string', $bar->test()); |
| 113 | +} |
| 114 | + |
| 115 | +class WithFinalConstant |
| 116 | +{ |
| 117 | + final const FOO_BAR = 'foo'; |
| 118 | + |
| 119 | + /** @return static::FOO_BAR */ |
| 120 | + public function test(): string |
| 121 | + { |
| 122 | + return static::FOO_BAR; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class WithFinalConstantChild extends WithFinalConstant |
| 127 | +{ |
| 128 | +} |
| 129 | + |
| 130 | +function testFinalConstant(WithFinalConstant $foo, WithFinalConstantChild $bar): void |
| 131 | +{ |
| 132 | + assertType("'foo'", $foo->test()); |
| 133 | + assertType("'foo'", $bar->test()); |
| 134 | +} |
| 135 | + |
| 136 | +class WithUntypedConstant |
| 137 | +{ |
| 138 | + const FOO_BAR = 'foo'; |
| 139 | + |
| 140 | + /** @return static::FOO_BAR */ |
| 141 | + public function test(): string |
| 142 | + { |
| 143 | + return static::FOO_BAR; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function testUntypedConstant(WithUntypedConstant $foo): void |
| 148 | +{ |
| 149 | + assertType("'foo'", $foo->test()); |
| 150 | +} |
| 151 | + |
| 152 | +final class FinalChild extends FooBar |
| 153 | +{ |
| 154 | + const FOO_BAR = 'baz'; |
| 155 | +} |
| 156 | + |
| 157 | +function testFinalChild(FinalChild $foo): void |
| 158 | +{ |
| 159 | + assertType("'baz'", $foo->test()); |
| 160 | +} |
| 161 | + |
| 162 | +class WithFinalTypedConstant |
| 163 | +{ |
| 164 | + /** @var non-empty-string */ |
| 165 | + final const string FOO_BAR = 'foo'; |
| 166 | + |
| 167 | + /** @return static::FOO_BAR */ |
| 168 | + public function test(): string |
| 169 | + { |
| 170 | + return static::FOO_BAR; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +function testFinalTypedConstant(WithFinalTypedConstant $foo): void |
| 175 | +{ |
| 176 | + assertType('non-empty-string', $foo->test()); |
| 177 | +} |
0 commit comments