File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug14553 ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ class Foo {
8+
9+ const FORMAT_HTML = 'html ' ;
10+
11+ const FORMAT_MARKDOWN = 'markdown ' ;
12+
13+ /**
14+ * @param list<self::FORMAT_*> $formats
15+ * @return array<self::FORMAT_*, string>|string
16+ * @phpstan-return ($formats is array{} ? string : ($formats is array{self::FORMAT_MARKDOWN} ? string : ($formats is array{self::FORMAT_HTML} ? string : non-empty-array<self::FORMAT_*, string>)))
17+ */
18+ public function getMessage (array $ formats = [])
19+ {
20+ $ message = '<h1>title</h1> ' ;
21+
22+ if (!$ formats ) {
23+ return $ message ;
24+ }
25+
26+ if (1 === count ($ formats )) {
27+ return self ::formatMessage ($ message , array_first ($ formats ));
28+ }
29+
30+ $ formatted = [];
31+ foreach ($ formats as $ format ) {
32+ $ formatted [$ format ] = self ::formatMessage ($ message , $ format );
33+ }
34+
35+ assertType ('non-empty-array{html?: string, markdown?: string} ' , $ formatted );
36+
37+ return $ formatted ;
38+ }
39+
40+ private function formatMessage (string $ message , string $ format ): string
41+ {
42+ if ($ format === self ::FORMAT_HTML ) {
43+ return $ message ;
44+ }
45+
46+ return '# title ' ;
47+ }
48+
49+ }
Original file line number Diff line number Diff line change @@ -1341,4 +1341,10 @@ public function testBug12653(): void
13411341 $ this ->analyse ([__DIR__ . '/data/bug-12653.php ' ], []);
13421342 }
13431343
1344+ #[RequiresPhp('>= 8.5.0 ' )]
1345+ public function testBug14553 (): void
1346+ {
1347+ $ this ->analyse ([__DIR__ . '/../../Analyser/nsrt/bug-14553.php ' ], []);
1348+ }
1349+
13441350}
You can’t perform that action at this time.
0 commit comments