You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?phpdeclare(strict_types = 1);
/** * Abbreviate an address - always show the first line, then work backwards from the most generic at the end * @param array{a?: string, * b?: string, * c?: string, * d?: string, * e?: string, * } $array array of things to show * @param int $count how many of them to show, the first one and $count-1 * @return string; */functionabbrev($array, $count){
if ($count < 1){
return'';
}
$a = [];
if (isset($array['a'])) $a[] = $array['a'];
if (isset($array['b'])) $a[] = $array['b'];
if (isset($array['c'])) $a[] = $array['c'];
if (isset($array['d'])) $a[] = $array['d'];
if (isset($array['e'])) $a[] = $array['e'];
if (count($a) >= $count){ // count is > 0, so $a[0] must exist...$ret = implode(', ', array_merge([$a[0]], array_slice($a, -($count-1)))); // line 24
}else{
$ret = implode(', ', $a);
}
return$ret;
}
$address = [
'a' => 'a',
'b' => 'b',
// c omitted'd' => 'd',
'e' => 'e',
];
echo abbrev($address, 2)."\n";
echo abbrev($address, 5)."\n";
echo abbrev($address, 7)."\n";
$address = [
];
echo abbrev($address, 2)."\n";
echo abbrev($address, 5)."\n";
echo abbrev($address, 7)."\n";
Actual output
24 | Offset 0 does not exist on array()\|array(0 => string, ?1 => string, ?2 => string, ?3 => string, ?4 => string).
Expected output
No errors!
The text was updated successfully, but these errors were encountered:
Bug report
.. or at least I think that may be what's wrong here...
If
count($a)
on an array assigned using multiple$a[] = ...
returns more than zero items, then$a[0]
should exist.Code snippet that reproduces the problem
https://phpstan.org/r/bfc2aa88-7001-4b48-8b96-1ed4e72600e7
Actual output
Expected output
The text was updated successfully, but these errors were encountered: