Skip to content

Commit 2415e7d

Browse files
committed
more tests
1 parent c3c2c70 commit 2415e7d

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPStan\Type\VerbosityLevel;
1515
use function count;
1616
use function in_array;
17+
use function is_string;
1718
use function sprintf;
1819

1920
/**
@@ -106,7 +107,11 @@ public function processNode(Node $node, Scope $scope): array
106107
$arrayArg = $node->dim->getArgs()[0]->value;
107108
$arrayType = $scope->getType($arrayArg);
108109
if (
109-
$arrayType->isArray()->yes()
110+
$arrayArg instanceof Node\Expr\Variable
111+
&& $node->var instanceof Node\Expr\Variable
112+
&& is_string($arrayArg->name)
113+
&& $arrayArg->name === $node->var->name
114+
&& $arrayType->isArray()->yes()
110115
&& $arrayType->isIterableAtLeastOnce()->yes()
111116
) {
112117
return [];
@@ -125,7 +130,11 @@ public function processNode(Node $node, Scope $scope): array
125130
$arrayArg = $node->dim->left->getArgs()[0]->value;
126131
$arrayType = $scope->getType($arrayArg);
127132
if (
128-
$arrayType->isList()->yes()
133+
$arrayArg instanceof Node\Expr\Variable
134+
&& $node->var instanceof Node\Expr\Variable
135+
&& is_string($arrayArg->name)
136+
&& $arrayArg->name === $node->var->name
137+
&& $arrayType->isList()->yes()
129138
&& $arrayType->isIterableAtLeastOnce()->yes()
130139
) {
131140
return [];

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,14 @@ public function testArrayDimFetchOnArrayKeyFirsOrLastOrCount(): void
838838
'Offset 0|null might not exist on list<string>.',
839839
12,
840840
],
841+
[
842+
'Offset (int|string) might not exist on non-empty-list<string>.',
843+
16,
844+
],
845+
[
846+
'Offset int<-1, max> might not exist on non-empty-list<string>.',
847+
45,
848+
],
841849
]);
842850
}
843851

tests/PHPStan/Rules/Arrays/data/array-dim-fetch-on-array-key-first-last.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ class Hello {
66
/**
77
* @param list<string> $hellos
88
*/
9-
public function first(array $hellos): string
9+
public function first(array $hellos, array $anotherArray): string
1010
{
1111
if (rand(0,1)) {
1212
return $hellos[array_key_first($hellos)];
1313
}
1414
if ($hellos !== []) {
15+
if ($anotherArray !== []) {
16+
return $hellos[array_key_first($anotherArray)];
17+
}
18+
1519
return $hellos[array_key_first($hellos)];
1620
}
1721
return '';
@@ -31,12 +35,16 @@ public function last(array $hellos): string
3135
/**
3236
* @param list<string> $hellos
3337
*/
34-
public function works(array $hellos): string
38+
public function countOnArray(array $hellos, array $anotherArray): string
3539
{
3640
if ($hellos === []) {
3741
return 'nothing';
3842
}
3943

44+
if (rand(0,1)) {
45+
return $hellos[count($anotherArray) - 1];
46+
}
47+
4048
return $hellos[count($hellos) - 1];
4149
}
4250
}

0 commit comments

Comments
 (0)