Skip to content

Commit

Permalink
NonexistentOffsetInArrayDimFetchCheck - be less strict about Benevole…
Browse files Browse the repository at this point in the history
…ntUnionType
  • Loading branch information
ondrejmirtes committed Jun 14, 2021
1 parent b0cc11f commit 2abb92e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
Expand Down Expand Up @@ -68,7 +69,12 @@ static function (Type $type) use ($dimType): bool {
}

if (!$report && $this->reportMaybes) {
foreach (TypeUtils::flattenTypes($type) as $innerType) {
if ($type instanceof BenevolentUnionType) {
$flattenedTypes = [$type];
} else {
$flattenedTypes = TypeUtils::flattenTypes($type);
}
foreach ($flattenedTypes as $innerType) {
if ($dimType instanceof UnionType) {
if ($innerType->hasOffsetValueType($dimType)->no()) {
$report = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,14 @@ public function testBug2689(): void
]);
}

public function testBug5169(): void
{
$this->analyse([__DIR__ . '/data/bug-5169.php'], [
[
'Cannot access offset mixed on (float|int).',
29,
],
]);
}

}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-5169.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Bug5169;

class HelloWorld
{
/**
* @param array<mixed> $configs
*
* @return array<mixed>
*/
protected function merge(array $configs): array
{
$result = [];
foreach ($configs as $config) {
$result += $config;

foreach ($config as $name => $dto) {
$result[$name] += $dto;
}
}

return $result;
}

public function merge2($mixed): void
{
$f = $mixed - $mixed;
$f[$mixed] = true;
}
}

0 comments on commit 2abb92e

Please sign in to comment.