Skip to content

Commit

Permalink
Fix issue 8467 ShowCommand regression
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Dec 6, 2022
1 parent 024e98e commit 2ca6118
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -3992,16 +3992,18 @@ static function (): void {
}
}

$constantArrays = $iterateeType->getConstantArrays();
if (
$stmt->getDocComment() === null
&& $iterateeType instanceof ConstantArrayType
&& $iterateeType->isConstantArray()->yes()
&& count($constantArrays) === 1
&& $stmt->valueVar instanceof Variable && is_string($stmt->valueVar->name)
&& $stmt->keyVar instanceof Variable && is_string($stmt->keyVar->name)
) {
$valueConditionalHolders = [];
$arrayDimFetchConditionalHolders = [];
foreach ($iterateeType->getKeyTypes() as $i => $keyType) {
$valueType = $iterateeType->getValueTypes()[$i];
foreach ($constantArrays[0]->getKeyTypes() as $i => $keyType) {
$valueType = $constantArrays[0]->getValueTypes()[$i];
$holder = new ConditionalExpressionHolder([
'$' . $stmt->keyVar->name => ExpressionTypeHolder::createYes(new Variable($stmt->keyVar->name), $keyType),
], new ExpressionTypeHolder($stmt->valueVar, $valueType, TrinaryLogic::createYes()));
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -1134,6 +1134,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-8389.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8421.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/imagick-pixel.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-8467a.php');
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-8467a.php
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace Bug8467a;

use function PHPStan\Testing\assertType;

/**
* @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>}
*/
interface CompletePackageInterface {
/**
* Returns an associative array of autoloading rules
*
* {"<type>": {"<namespace": "<directory>"}}
*
* Type is either "psr-4", "psr-0", "classmap" or "files". Namespaces are mapped to
* directories for autoloading using the type specified.
*
* @return array Mapping of autoloading rules
* @phpstan-return AutoloadRules
*/
public function getAutoload(): array;
}

class Test {
public function foo (CompletePackageInterface $package): void {
if (\count($package->getAutoload()) > 0) {
$autoloadConfig = $package->getAutoload();
foreach ($autoloadConfig as $type => $autoloads) {
assertType('array<int<0, max>|string, array<string>|string>', $autoloadConfig[$type]);
if ($type === 'psr-0' || $type === 'psr-4') {

} elseif ($type === 'classmap') {
assertType('list<string>', $autoloadConfig[$type]);
implode(', ', $autoloadConfig[$type]);
}
}
}
}
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Functions/ImplodeFunctionRuleTest.php
Expand Up @@ -53,4 +53,9 @@ public function testBug6000(): void
$this->analyse([__DIR__ . '/../Arrays/data/bug-6000.php'], []);
}

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

}

0 comments on commit 2ca6118

Please sign in to comment.