1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Detect trying to access to a list with a negative offset (#4552)

This commit is contained in:
orklah 2020-11-16 02:26:50 +01:00 committed by Daniil Gentili
parent 8b56e5eede
commit 2f368244a4
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 22 additions and 2 deletions

View File

@ -797,9 +797,16 @@ class ArrayFetchAnalyzer
$statements_analyzer $statements_analyzer
); );
} }
$has_valid_offset = true;
} elseif (count($key_values) === 1
&& is_int($key_values[0])
&& $key_values[0] < 0
) {
$expected_offset_types[] = Type::getPositiveInt();
$has_valid_offset = false;
} else {
$has_valid_offset = true;
} }
$has_valid_offset = true;
} }
if ($in_assignment && $type instanceof Type\Atomic\TNonEmptyList && $type->count !== null) { if ($in_assignment && $type instanceof Type\Atomic\TNonEmptyList && $type->count !== null) {

View File

@ -1270,6 +1270,19 @@ class ArrayAccessTest extends TestCase
[$width, $height, $depth] = size();', [$width, $height, $depth] = size();',
'error_message' => 'InvalidArrayOffset', 'error_message' => 'InvalidArrayOffset',
], ],
'negativeListAccess' => [
'<?php
class HelloWorld
{
public function sayHello(): void
{
$a = explode("/", "a/b/c");
$x = $a[-3];
echo $x;
}
}',
'error_message' => 'InvalidArrayOffset'
]
]; ];
} }
} }