Skip to content

Commit 543ac61

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents 70ebcb1 + 0e5b460 commit 543ac61

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)