Skip to content

Commit

Permalink
add regression tests for phpstan/phpstan#7000
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed May 11, 2022
1 parent 43cb6ab commit 9381526
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -879,6 +879,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3853.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/conditional-types.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/constant-array-optional-set.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7000.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6383.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-3284.php');

Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7000.php
@@ -0,0 +1,20 @@
<?php

namespace Bug7000;

use function PHPStan\Testing\assertType;

class Foo
{
public function doBar(): void
{
/** @var array{require?: array<string, string>, require-dev?: array<string, string>} $composer */
$composer = array();
foreach (array('require', 'require-dev') as $linkType) {
if (isset($composer[$linkType])) {
assertType('array{require?: array<string, string>, require-dev?: array<string, string>}&non-empty-array', $composer);
foreach ($composer[$linkType] as $x) {}
}
}
}
}
Expand Up @@ -385,4 +385,9 @@ public function testBug4885(): void
$this->analyse([__DIR__ . '/data/bug-4885.php'], []);
}

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

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-7000.php
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace Bug7000;

class Foo
{
public function doBar(): void
{
/** @var array{require?: array<string, string>, require-dev?: array<string, string>} $composer */
$composer = array();
/** @var 'require'|'require-dev' $foo */
$foo = '';
foreach (array('require', 'require-dev') as $linkType) {
if (isset($composer[$linkType])) {
foreach ($composer[$linkType] as $x) {} // should not report error
foreach ($composer[$foo] as $x) {} // should report error. It can be $linkType = 'require', $foo = 'require-dev'
}
}
}
}

0 comments on commit 9381526

Please sign in to comment.