Skip to content

non-empty-array<mixed> does not accept array with hasOffsetValue #3924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2053,4 +2053,35 @@ public function testBug7522(): void
$this->analyse([__DIR__ . '/data/bug-7522.php'], []);
}

public function testBug12847(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;

$this->analyse([__DIR__ . '/data/bug-12847.php'], [
[
'Parameter #1 $array of function Bug12847\doSomething expects non-empty-array<mixed>, mixed given.',
32,
'mixed is empty.',
],
[
'Parameter #1 $array of function Bug12847\doSomething expects non-empty-array<mixed>, mixed given.',
39,
'mixed is empty.',
],
[
'Parameter #1 $array of function Bug12847\doSomethingWithInt expects non-empty-array<int>, non-empty-array given.',
61,
],
[
'Parameter #1 $array of function Bug12847\doSomethingWithInt expects non-empty-array<int>, non-empty-array given.',
67,
],
]);
}

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

namespace Bug12847;

function doBar():void {
/**
* @var array<mixed> $array
*/
$array = [
'abc' => 'def'
];

if (isset($array['def'])) {
doSomething($array);
}
}

function doFoo(array $array):void {
if (isset($array['def'])) {
doSomething($array);
}
}

function doFooBar(array $array):void {
if (array_key_exists('foo', $array) && $array['foo'] === 17) {
doSomething($array);
}
}

function doImplicitMixed($mixed):void {
if (isset($mixed['def'])) {
doSomething($mixed);
}
}

function doExplicitMixed(mixed $mixed): void
{
if (isset($mixed['def'])) {
doSomething($mixed);
}
}

/**
* @param non-empty-array<mixed> $array
*/
function doSomething(array $array): void
{

}

/**
* @param non-empty-array<int> $array
*/
function doSomethingWithInt(array $array): void
{

}

function doFooBarInt(array $array):void {
if (array_key_exists('foo', $array) && $array['foo'] === 17) {
doSomethingWithInt($array); // expect error, because our array is not sealed
}
}

function doFooBarString(array $array):void {
if (array_key_exists('foo', $array) && $array['foo'] === "hello") {
doSomethingWithInt($array); // expect error, because our array is not sealed
}
}
Loading