1
0
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:
Brown 2019-01-25 10:23:50 -05:00
parent bed1f15e09
commit c9e1df510e
2 changed files with 21 additions and 1 deletions

View File

@ -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);
}

View File

@ -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',
],
];
}
}