mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Detect trying to access to a list with a negative offset (#4552)
This commit is contained in:
parent
8b56e5eede
commit
2f368244a4
@ -797,9 +797,16 @@ class ArrayFetchAnalyzer
|
||||
$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) {
|
||||
|
@ -1270,6 +1270,19 @@ class ArrayAccessTest extends TestCase
|
||||
[$width, $height, $depth] = size();',
|
||||
'error_message' => 'InvalidArrayOffset',
|
||||
],
|
||||
'negativeListAccess' => [
|
||||
'<?php
|
||||
class HelloWorld
|
||||
{
|
||||
public function sayHello(): void
|
||||
{
|
||||
$a = explode("/", "a/b/c");
|
||||
$x = $a[-3];
|
||||
echo $x;
|
||||
}
|
||||
}',
|
||||
'error_message' => 'InvalidArrayOffset'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user