mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Fix #1241 - allow negative string offsets on a string
This commit is contained in:
parent
bed1f15e09
commit
c9e1df510e
@ -646,7 +646,7 @@ class ArrayFetchAnalyzer
|
||||
} elseif ($type instanceof TLiteralString) {
|
||||
$valid_offsets = [];
|
||||
|
||||
for ($i = 0, $l = strlen($type->value); $i < $l; $i++) {
|
||||
for ($i = -strlen($type->value), $l = strlen($type->value); $i < $l; $i++) {
|
||||
$valid_offsets[] = new TLiteralInt($i);
|
||||
}
|
||||
|
||||
|
@ -319,6 +319,20 @@ class ArrayAccessTest extends TestCase
|
||||
'assertions' => [],
|
||||
'error_levels' => ['MixedTypeCoercion'],
|
||||
],
|
||||
'allowNegativeStringOffset' => [
|
||||
'<?php
|
||||
$a = "hello";
|
||||
echo $a[-5];
|
||||
echo $a[-4];
|
||||
echo $a[-3];
|
||||
echo $a[-2];
|
||||
echo $a[-1];
|
||||
echo $a[0];
|
||||
echo $a[1];
|
||||
echo $a[2];
|
||||
echo $a[3];
|
||||
echo $a[4];',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -531,6 +545,12 @@ class ArrayAccessTest extends TestCase
|
||||
}',
|
||||
'error_message' => 'TypeDoesNotContainType',
|
||||
],
|
||||
'forbidNegativeStringOffsetOutOfRange' => [
|
||||
'<?php
|
||||
$a = "hello";
|
||||
echo $a[-6];',
|
||||
'error_message' => 'InvalidArrayOffset',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user