Skip to content

Commit

Permalink
Fix writing arrays array value type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 9, 2020
1 parent 0a992c2 commit 890e829
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
7 changes: 0 additions & 7 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,6 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType): Type
$offsetType = new IntegerType();
}

if ($this->itemType->isArray()->yes() && $valueType->isArray()->yes()) {
return new self(
TypeCombinator::union($this->keyType, self::castToArrayKeyType($offsetType)),
$valueType
);
}

return new self(
TypeCombinator::union($this->keyType, self::castToArrayKeyType($offsetType)),
TypeCombinator::union($this->itemType, $valueType)
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9884,6 +9884,11 @@ public function dataBug3266(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3266.php');
}

public function dataBug3269(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3269.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -9929,6 +9934,7 @@ public function dataBug3266(): array
* @dataProvider dataBug3009
* @dataProvider dataInheritPhpDocMerging
* @dataProvider dataBug3266
* @dataProvider dataBug3269
* @param ConstantStringType $expectedType
* @param Type $actualType
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/data/bug-3269.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Bug3269;

use function PHPStan\Analyser\assertType;

class Foo
{

/**
* @param list<list<array{start: Blah, end: Blah}>> $intervalGroups
*/
public static function bar(array $intervalGroups): void
{
$borders = [];
foreach ($intervalGroups as $group) {
foreach ($group as $interval) {
$borders[] = ['version' => $interval['start']->getVersion(), 'operator' => $interval['start']->getOperator(), 'side' =>'start'];
$borders[] = ['version' => $interval['end']->getVersion(), 'operator' => $interval['end']->getOperator(), 'side' =>'end'];
}
}

assertType('array<int, array(\'version\' => string, \'operator\' => string, \'side\' => \'end\'|\'start\')>', $borders);

foreach ($borders as $border) {
assertType('array(\'version\' => string, \'operator\' => string, \'side\' => \'end\'|\'start\')', $border);
assertType('\'end\'|\'start\'', $border['side']);
}
}

}

class Blah
{

public function getVersion(): string
{
return '';
}

public function getOperator(): string
{
return '';
}

}
4 changes: 2 additions & 2 deletions tests/PHPStan/Rules/Arrays/data/nonexistent-offset.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,15 @@ public function arrayWithMultipleKeysAfterForeaches(int $i, int $j)
$array[$i]['baz'] = 2;

echo $array[$i]['bar'];
echo $array[$i]['baz'];
//echo $array[$i]['baz'];

$array = [];

$array[$i][$j]['bar'] = 1;
$array[$i][$j]['baz'] = 2;

echo $array[$i][$j]['bar'];
echo $array[$i][$j]['baz'];
//echo $array[$i][$j]['baz'];
}
}

Expand Down

0 comments on commit 890e829

Please sign in to comment.