Skip to content

Ignore negative keys on checking whether a ConstantArray is a list #3870

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 4 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,13 @@ parameters:
-
message: '#^PHPDoc tag @var with type float\|int is not subtype of native type int\.$#'
identifier: varTag.nativeType
count: 2
count: 1
path: src/Type/Constant/ConstantArrayTypeBuilder.php

-
message: '#^PHPDoc tag @var with type float\|int is not subtype of native type int\<1, max\>\.$#'
identifier: varTag.nativeType
count: 1
path: src/Type/Constant/ConstantArrayTypeBuilder.php

-
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
$this->keyTypes[] = $offsetType;
$this->valueTypes[] = $valueType;

if ($offsetType instanceof ConstantIntegerType) {
if ($offsetType instanceof ConstantIntegerType && $offsetType->getValue() >= 0) {
$min = min($this->nextAutoIndexes);
$max = max($this->nextAutoIndexes);
if ($offsetType->getValue() > $min) {
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/VarTagChangedExpressionTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,26 @@ public function testBug10130(): void
]);
}

public function testBug12708(): void
{
$this->analyse([__DIR__ . '/data/bug-12708.php'], [
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{1: 'b', 2: 'c'}.",
12,
],
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{0: 'a', 2: 'c'}.",
18,
],
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{-1: 'z', 0: 'a', 1: 'b', 2: 'c'}.",
24,
],
[
"PHPDoc tag @var with type list<string> is not subtype of native type array{0: 'a', -1: 'z', 1: 'b', 2: 'c'}.",
30,
],
]);
}

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

function do0()
{
/** @var list<string> */
return [0 => 'a', 1 => 'b', 2 => 'c'];
}

function do1()
{
/** @var list<string> */
return [1 => 'b', 2 => 'c'];
}

function do2()
{
/** @var list<string> */
return [0 => 'a', 2 => 'c'];
}

function do3()
{
/** @var list<string> */
return [-1 => 'z', 0 => 'a', 1 => 'b', 2 => 'c'];
}

function do4()
{
/** @var list<string> */
return [0 => 'a', -1 => 'z', 1 => 'b', 2 => 'c'];
}
Loading