Skip to content

Commit 2ca6118

Browse files
authored
Fix issue 8467 ShowCommand regression
1 parent 024e98e commit 2ca6118

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,16 +3992,18 @@ static function (): void {
39923992
}
39933993
}
39943994

3995+
$constantArrays = $iterateeType->getConstantArrays();
39953996
if (
39963997
$stmt->getDocComment() === null
3997-
&& $iterateeType instanceof ConstantArrayType
3998+
&& $iterateeType->isConstantArray()->yes()
3999+
&& count($constantArrays) === 1
39984000
&& $stmt->valueVar instanceof Variable && is_string($stmt->valueVar->name)
39994001
&& $stmt->keyVar instanceof Variable && is_string($stmt->keyVar->name)
40004002
) {
40014003
$valueConditionalHolders = [];
40024004
$arrayDimFetchConditionalHolders = [];
4003-
foreach ($iterateeType->getKeyTypes() as $i => $keyType) {
4004-
$valueType = $iterateeType->getValueTypes()[$i];
4005+
foreach ($constantArrays[0]->getKeyTypes() as $i => $keyType) {
4006+
$valueType = $constantArrays[0]->getValueTypes()[$i];
40054007
$holder = new ConditionalExpressionHolder([
40064008
'$' . $stmt->keyVar->name => ExpressionTypeHolder::createYes(new Variable($stmt->keyVar->name), $keyType),
40074009
], new ExpressionTypeHolder($stmt->valueVar, $valueType, TrinaryLogic::createYes()));

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@ public function dataFileAsserts(): iterable
11341134
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-8389.php');
11351135
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8421.php');
11361136
yield from $this->gatherAssertTypes(__DIR__ . '/data/imagick-pixel.php');
1137+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-8467a.php');
11371138
}
11381139

11391140
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug8467a;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @phpstan-type AutoloadRules array{psr-0?: array<string, string|string[]>, psr-4?: array<string, string|string[]>, classmap?: list<string>, files?: list<string>, exclude-from-classmap?: list<string>}
9+
*/
10+
interface CompletePackageInterface {
11+
/**
12+
* Returns an associative array of autoloading rules
13+
*
14+
* {"<type>": {"<namespace": "<directory>"}}
15+
*
16+
* Type is either "psr-4", "psr-0", "classmap" or "files". Namespaces are mapped to
17+
* directories for autoloading using the type specified.
18+
*
19+
* @return array Mapping of autoloading rules
20+
* @phpstan-return AutoloadRules
21+
*/
22+
public function getAutoload(): array;
23+
}
24+
25+
class Test {
26+
public function foo (CompletePackageInterface $package): void {
27+
if (\count($package->getAutoload()) > 0) {
28+
$autoloadConfig = $package->getAutoload();
29+
foreach ($autoloadConfig as $type => $autoloads) {
30+
assertType('array<int<0, max>|string, array<string>|string>', $autoloadConfig[$type]);
31+
if ($type === 'psr-0' || $type === 'psr-4') {
32+
33+
} elseif ($type === 'classmap') {
34+
assertType('list<string>', $autoloadConfig[$type]);
35+
implode(', ', $autoloadConfig[$type]);
36+
}
37+
}
38+
}
39+
}
40+
}

tests/PHPStan/Rules/Functions/ImplodeFunctionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ public function testBug6000(): void
5353
$this->analyse([__DIR__ . '/../Arrays/data/bug-6000.php'], []);
5454
}
5555

56+
public function testBug8467a(): void
57+
{
58+
$this->analyse([__DIR__ . '/../Arrays/data/bug-8467a.php'], []);
59+
}
60+
5661
}

0 commit comments

Comments
 (0)