Skip to content

Commit

Permalink
Merge pull request #9673 from whizsid/array_splice_multi_literal
Browse files Browse the repository at this point in the history
Fixed crash issue when using multiple literals for array_splice
  • Loading branch information
orklah committed Apr 18, 2023
2 parents e2957a1 + 782735e commit 958eb5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use UnexpectedValueException;

use function array_filter;
use function array_merge;
use function array_pop;
use function array_shift;
use function array_unshift;
Expand Down Expand Up @@ -395,7 +396,7 @@ public static function handleSplice(
$offset_arg_is_zero = false;

if (($offset_arg_type = $statements_analyzer->node_data->getType($offset_arg))
&& $offset_arg_type->hasLiteralValue()
&& $offset_arg_type->hasLiteralValue() && $offset_arg_type->isSingleLiteral()
) {
$offset_literal_value = $offset_arg_type->getSingleLiteral()->value;
$offset_arg_is_zero = is_numeric($offset_literal_value) && ((int) $offset_literal_value)===0;
Expand Down Expand Up @@ -451,13 +452,27 @@ public static function handleSplice(
if (($length_arg_type = $statements_analyzer->node_data->getType($length_arg))
&& $length_arg_type->hasLiteralValue()
) {
$length_literal = $length_arg_type->getSingleLiteral();
if ($length_literal->isNumericType()) {
$length_value = (int)$length_literal->value;
if ($length_value>=$array_size) {
$cover_whole_arr = true;
$length_min = null;
if ($length_arg_type->isSingleLiteral()) {
$length_literal = $length_arg_type->getSingleLiteral();
if ($length_literal->isNumericType()) {
$length_min = (int) $length_literal->value;
}
} else {
$literals = array_merge(
$length_arg_type->getLiteralStrings(),
$length_arg_type->getLiteralInts(),
$length_arg_type->getLiteralFloats(),
);
foreach ($literals as $literal) {
if ($literal->isNumericType()
&& ($literal_val = (int) $literal->value)
&& ((isset($length_min) && $length_min> $literal_val) || !isset($length_min))) {
$length_min = $literal_val;
}
}
}
$cover_whole_arr = isset($length_min) && $length_min>= $array_size;
} elseif ($length_arg_type&& $length_arg_type->isNull()) {
$cover_whole_arr = true;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/ArrayFunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,40 @@ function (int $carry, int $item) {
'$a' => 'array<never, never>',
],
],
'arraySpliceWithBothMultipleLiterals' => [
'code' => '<?php
$a = array( "hello" );
/** @var 1|2|0 **/
$b = 1;
/** @var 4|5 **/
$c = 4;
$_d = array_splice( $a, $b, $c );',
'assertions' => [
'$a' => 'list<string>',
],
],
'arraySpliceWithLengthMultipleLiterals' => [
'code' => '<?php
$a = array( "hello", "world" );
$b = 0;
/** @var 2|3 **/
$c = 3;
array_splice( $a, $b, $c );',
'assertions' => [
'$a' => 'array<never, never>',
],
],
'arraySpliceWithLengthMultipleLiteralsIntersect' => [
'code' => '<?php
$a = array( "hello", "world", "world" );
$b = 0;
/** @var 2|6 **/
$c = 3;
array_splice( $a, $b, $c );',
'assertions' => [
'$a' => 'list<string>',
],
],
'ksortPreserveShape' => [
'code' => '<?php
$a = ["a" => 3, "b" => 4];
Expand Down

0 comments on commit 958eb5a

Please sign in to comment.