Skip to content

Commit 9852694

Browse files
phpstan-botclaudestaabm
authored
Fix phpstan/phpstan#14458: Regression: never defined variable
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Markus Staab <markus.staab@redaxo.de>
1 parent 4e15f5e commit 9852694

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,9 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
32383238

32393239
// Pass 2: Supertype match. Only runs when Pass 1 found no exact match for this expression.
32403240
foreach ($conditionalExpressions as $conditionalExpression) {
3241+
if ($conditionalExpression->getTypeHolder()->getCertainty()->no()) {
3242+
continue;
3243+
}
32413244
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
32423245
if (
32433246
!array_key_exists($holderExprString, $specifiedExpressions)

tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ public function testBug4846(): void
387387
]);
388388
}
389389

390+
public function testBug14458(): void
391+
{
392+
$this->analyse([__DIR__ . '/data/bug-14458.php'], []);
393+
}
394+
390395
public function testBug14393(): void
391396
{
392397
$this->analyse([__DIR__ . '/data/bug-14393.php'], [
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14458;
4+
5+
use DateTime;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param array<mixed> $payload
12+
* @return array<mixed>
13+
*/
14+
public function doFoo(array $payload): array
15+
{
16+
if ($payload['a'] !== 'b') {
17+
throw new \Exception();
18+
}
19+
20+
if (isset($payload['c'])) {
21+
$c = array_values(array_map(static fn (array $cd) => $cd[0], $payload['c']));
22+
}
23+
24+
$convertedPriceWithVat = null;
25+
if (array_key_exists('cpwv', $payload)) {
26+
$convertedAmount = (float) $payload['cpwv']['awv'];
27+
28+
}
29+
30+
return [
31+
$payload['cf'],
32+
$payload['n'],
33+
$payload['d'] ?? null,
34+
$payload['mb'] ?? null,
35+
$payload['cr'],
36+
new DateTime($payload['p']),
37+
$payload['pn'],
38+
$payload['pd'] ?? null,
39+
$payload['piu'] ?? null,
40+
$convertedPriceWithVat,
41+
$payload['vi'],
42+
$payload['pi'],
43+
$payload['user']['name'] ?? null,
44+
$payload['user']['phone'] ?? null,
45+
$payload['ac'] ?? null,
46+
$c ?? null,
47+
];
48+
}
49+
50+
}

0 commit comments

Comments
 (0)