2020-01-31 19:58:02 +01:00
< ? php
2021-12-15 04:58:32 +01:00
2020-01-31 19:58:02 +01:00
namespace Psalm\Tests ;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait ;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait ;
2020-01-31 19:58:02 +01:00
use const DIRECTORY_SEPARATOR ;
class ArrayFunctionCallTest extends TestCase
{
2021-12-04 21:55:53 +01:00
use InvalidCodeAnalysisTestTrait ;
use ValidCodeAnalysisTestTrait ;
2020-01-31 19:58:02 +01:00
2020-09-12 17:24:05 +02:00
public function providerValidCodeParse () : iterable
2020-01-31 19:58:02 +01:00
{
return [
'arrayFilter' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-14 19:31:53 +02:00
$d = array_filter ([ " a " => rand ( 0 , 10 ), " b " => rand ( 0 , 10 ), " c " => null ]);
2020-01-31 19:58:02 +01:00
$e = array_filter (
2020-09-14 19:31:53 +02:00
[ " a " => rand ( 0 , 10 ), " b " => rand ( 0 , 10 ), " c " => null ],
2020-01-31 19:58:02 +01:00
function ( ? int $i ) : bool {
return true ;
}
); ' ,
'assertions' => [
2022-11-12 02:14:21 +01:00
'$d' => 'array{a?: int<1, 10>, b?: int<1, 10>}' ,
2021-08-21 00:52:16 +02:00
'$e' => 'array<string, int<0, 10>|null>' ,
2020-01-31 19:58:02 +01:00
],
],
2021-10-16 04:40:41 +02:00
'positiveIntArrayFilter' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-16 04:40:41 +02:00
/**
* @ param numeric $a
* @ param positive - int $positiveOne
* @ param int < 0 , 12 > $d
* @ param int < 1 , 12 > $f
2022-11-12 02:14:21 +01:00
* @ psalm - return array { a : numeric , b ? : int , c : positive - int , d ? : int < 0 , 12 > , f : int < 1 , 12 > }
2021-10-16 04:40:41 +02:00
*/
function makeAList ( $a , int $anyInt , int $positiveOne , int $d , int $f ) : array {
return array_filter ([ " a " => " 1 " , " b " => $anyInt , " c " => $positiveOne , " d " => $d , " f " => $f ]);
2022-12-18 17:15:15 +01:00
} ' ,
2021-10-16 04:40:41 +02:00
],
2020-01-31 19:58:02 +01:00
'arrayFilterAdvanced' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$f = array_filter ([ " a " => 5 , " b " => 12 , " c " => null ], function ( ? int $val , string $key ) : bool {
return true ;
}, ARRAY_FILTER_USE_BOTH );
$g = array_filter ([ " a " => 5 , " b " => 12 , " c " => null ], function ( string $val ) : bool {
return true ;
}, ARRAY_FILTER_USE_KEY );
$bar = " bar " ;
$foo = [
$bar => function () : string {
return " baz " ;
},
];
$foo = array_filter (
$foo ,
function ( string $key ) : bool {
return $key === " bar " ;
},
ARRAY_FILTER_USE_KEY
); ' ,
'assertions' => [
'$f' => 'array<string, int|null>' ,
'$g' => 'array<string, int|null>' ,
],
],
'arrayFilterIgnoreNullable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
class A {
/**
* @ return array < int , self | null >
*/
public function getRows () : array {
return [ new self , null ];
}
public function filter () : void {
$arr = array_filter (
static :: getRows (),
function ( self $row ) : bool {
return is_a ( $row , static :: class );
}
);
}
} ' ,
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'PossiblyInvalidArgument' ],
2020-01-31 19:58:02 +01:00
],
'arrayFilterAllowTrim' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$foo = array_filter ([ " hello " , " " ], " trim " ); ' ,
],
'arrayFilterAllowNull' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function foo () : array {
return array_filter (
array_map (
/** @return null */
function ( int $arg ) {
return null ;
},
[ 1 , 2 , 3 ]
)
);
} ' ,
],
'arrayFilterNamedFunction' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ param array < int , DateTimeImmutable | null > $a
* @ return array < int , DateTimeImmutable >
*/
function foo ( array $a ) : array {
return array_filter ( $a , " is_object " );
} ' ,
],
2020-07-31 20:56:29 +02:00
'arrayFilterFleshOutType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-31 20:56:29 +02:00
class Baz {
public const STATUS_FOO = " foo " ;
public const STATUS_BAR = " bar " ;
public const STATUS_QUX = " qux " ;
/**
* @ psalm - param self :: STATUS_ * $role
*/
public static function isStatus ( string $role ) : bool
{
return ! \in_array ( $role , [ self :: STATUS_BAR , self :: STATUS_QUX ], true );
}
}
/** @psalm-var array<Baz::STATUS_*> $statusList */
$ statusList = [ Baz :: STATUS_FOO , Baz :: STATUS_QUX ];
2022-12-18 17:15:15 +01:00
$statusList = array_filter ( $statusList , [ Baz :: class , " isStatus " ]); ' ,
2020-07-31 20:56:29 +02:00
],
2020-04-18 19:02:55 +02:00
'arrayKeysNonEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = array_keys ([ " a " => 1 , " b " => 2 ]); ' ,
'assertions' => [
2020-04-18 19:02:55 +02:00
'$a' => 'non-empty-list<string>' ,
2020-01-31 19:58:02 +01:00
],
],
'arrayKeysMixed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array */
$b = [ " a " => 5 ];
$a = array_keys ( $b ); ' ,
'assertions' => [
'$a' => 'list<array-key>' ,
],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MixedArgument' ],
2020-01-31 19:58:02 +01:00
],
'arrayValues' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$b = array_values ([ " a " => 1 , " b " => 2 ]);
$c = array_values ([ " a " => " hello " , " b " => " jello " ]); ' ,
'assertions' => [
'$b' => 'non-empty-list<int>' ,
'$c' => 'non-empty-list<string>' ,
],
],
'arrayCombine' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$c = array_combine ([ " a " , " b " , " c " ], [ 1 , 2 , 3 ]); ' ,
'assertions' => [
2022-12-13 22:29:35 +01:00
'$c===' => 'array{a: 1, b: 2, c: 3}' ,
2020-01-31 19:58:02 +01:00
],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [],
'php_version' => '7.4' ,
2020-01-31 19:58:02 +01:00
],
2022-12-13 22:29:35 +01:00
'arrayCombineDynamicParams' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-12-13 22:29:35 +01:00
/** @return array<string> */
function getStrings () : array { return []; }
/** @return array<int> */
function getInts () : array { return []; }
$c = array_combine ( getStrings (), getInts ()); ' ,
2021-03-16 18:43:49 +01:00
'assertions' => [
2022-12-13 22:29:35 +01:00
'$c' => 'array<string, int>|false' ,
2021-03-16 18:43:49 +01:00
],
],
2022-12-13 22:29:35 +01:00
'arrayCombineDynamicParamsNonEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-12-13 22:29:35 +01:00
/** @return non-empty-array<string> */
function getStrings () : array { return [ " test " ]; }
/** @return non-empty-array<int> */
function getInts () : array { return [ 123 , 321 ]; }
$c = array_combine ( getStrings (), getInts ()); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2021-03-15 02:18:19 +01:00
'$c' => 'false|non-empty-array<string, int>' ,
2020-01-31 19:58:02 +01:00
],
2021-03-16 18:43:49 +01:00
],
2022-12-13 22:29:35 +01:00
'arrayCombineDynamicParamsPHP8' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-12-13 22:29:35 +01:00
/** @return non-empty-array<string> */
function getStrings () : array { return [ " test " ]; }
/** @return non-empty-array<int> */
function getInts () : array { return [ 123 ]; }
2021-03-16 18:43:49 +01:00
$c = array_combine ( getStrings (), getInts ()); ' ,
'assertions' => [
2022-12-13 22:29:35 +01:00
'$c' => 'non-empty-array<string, int>' ,
2021-03-16 18:43:49 +01:00
],
2022-12-13 22:29:35 +01:00
'ignored_issues' => [],
'php_version' => '8.0' ,
2020-01-31 19:58:02 +01:00
],
2022-12-19 14:01:00 +01:00
'arrayMergeOverWrite' => [
'code' => ' < ? php
$a1 = [ " a " => " a1 " ];
$a2 = [ " a " => " a2 " ];
$result = array_merge ( $a1 , $a2 );
' ,
'assertions' => [
2022-12-19 18:31:43 +01:00
'$result===' => " array { a: 'a2'} " ,
],
2022-12-19 14:01:00 +01:00
],
2022-12-16 18:10:46 +01:00
'arrayMergeListOfShapes' => [
'code' => ' < ? php
/** @var list<array{a: int}> */
$a = [];
$b = array_merge ( ... $a );
/** @var non-empty-list<array{a: int}> */
$c = [];
$d = array_merge ( ... $c );
' ,
'assertions' => [
'$b' => 'array{a?: int}' ,
'$d' => 'array{a: int}' ,
2022-12-18 17:15:15 +01:00
],
2022-12-16 18:10:46 +01:00
],
2020-05-20 02:10:01 +02:00
'arrayMergeIntArrays' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-01-25 12:43:28 +01:00
$d = array_merge ([ " a " , " b " , " c " , " d " ], [ 1 , 2 , 3 ]); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-11-12 02:14:21 +01:00
'$d===' => " list { 'a', 'b', 'c', 'd', 1, 2, 3} " ,
2020-01-31 19:58:02 +01:00
],
],
2020-05-20 02:10:01 +02:00
'arrayMergePossiblyUndefined' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-05-20 02:10:01 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { host ? : string } $opts
* @ return array { host : string | int }
2020-05-20 02:10:01 +02:00
*/
function b ( array $opts ) : array {
return array_merge ([ " host " => 5 ], $opts );
} ' ,
],
2020-09-14 19:06:15 +02:00
'arrayMergeListResultWithArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
2020-09-14 19:06:15 +02:00
* @ param array < int , string > $list
2020-01-31 19:58:02 +01:00
* @ return list < string >
*/
2020-09-14 19:06:15 +02:00
function bar ( array $list ) : array {
2020-01-31 19:58:02 +01:00
return array_merge ( $list , [ " test " ]);
2020-09-14 19:06:15 +02:00
} ' ,
],
'arrayMergeListResultWithList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
2020-09-14 19:06:15 +02:00
* @ param list < string > $list
2020-01-31 19:58:02 +01:00
* @ return list < string >
*/
2020-09-14 19:06:15 +02:00
function foo ( array $list ) : array {
2020-01-31 19:58:02 +01:00
return array_merge ( $list , [ " test " ]);
} ' ,
],
2021-10-15 12:06:19 +02:00
'arrayMergeTypes' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ psalm - type A = array { name : string }
* @ psalm - type B = array { age : int }
2021-10-15 12:06:19 +02:00
*/
class Demo
{
/**
* @ param A $a
* @ param B $b
* @ return A & B
*/
public function merge ( $a , $b ) : array
{
return array_merge ( $a , $b );
}
} ' ,
],
2022-12-13 21:40:19 +01:00
'arrayMergeLists' => [
'code' => ' < ? php
/** @var list<int> */
$a = [];
/** @var non-empty-list<string> */
$b = [];
$c = array_merge ( $a , $b );
$d = array_merge ( $b , $a ); ' ,
'assertions' => [
// todo: this first type is not entirely correct
//'$c===' => "list{int|string, ...<int<0, max>, int|string>}",
2023-04-21 15:58:05 +02:00
'$c===' => " list { string, ...<int|string>} " ,
'$d===' => " list { string, ...<int|string>} " ,
2022-12-13 21:40:19 +01:00
],
],
2022-12-18 14:37:19 +01:00
'arrayMergeEmpty' => [
'code' => ' < ? php
$test = [[]];
$a = array_merge ( ... $test );
$test = [[], [ " test " => 0 ]];
$b = array_merge ( ... $test );
' ,
'assertions' => [
'$a===' => 'array<never, never>' ,
'$b===' => 'array{test: 0}' ,
2022-12-18 17:15:15 +01:00
],
2022-12-18 14:37:19 +01:00
],
2021-10-15 12:06:19 +02:00
'arrayReplaceIntArrays' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-01-25 12:43:28 +01:00
$d = array_replace ([ " a " , " b " , " c " , " d " ], [ 1 , 2 , 3 ]); ' ,
2021-10-15 12:06:19 +02:00
'assertions' => [
2022-11-12 02:14:21 +01:00
'$d===' => " list { 1, 2, 3, 'd'} " ,
2021-10-15 12:06:19 +02:00
],
],
'arrayReplacePossiblyUndefined' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { host ? : string } $opts
* @ return array { host : string | int }
2021-10-15 12:06:19 +02:00
*/
function b ( array $opts ) : array {
return array_replace ([ " host " => 5 ], $opts );
} ' ,
],
'arrayReplaceListResultWithArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
* @ param array < int , string > $list
* @ return list < string >
*/
function bar ( array $list ) : array {
return array_replace ( $list , [ " test " ]);
} ' ,
],
'arrayReplaceListResultWithList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
* @ param list < string > $list
* @ return list < string >
*/
function foo ( array $list ) : array {
return array_replace ( $list , [ " test " ]);
} ' ,
],
2021-10-15 12:06:19 +02:00
'arrayReplaceTypes' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ psalm - type A = array { name : string }
* @ psalm - type B = array { age : int }
2021-10-15 12:06:19 +02:00
*/
class Demo
{
/**
* @ param A $a
* @ param B $b
* @ return A & B
*/
public function replace ( $a , $b ) : array
{
return array_replace ( $a , $b );
}
} ' ,
],
2020-01-31 19:58:02 +01:00
'arrayReverseDontPreserveKey' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$d = array_reverse ([ " a " , " b " , 1 , " d " => 4 ]); ' ,
'assertions' => [
'$d' => 'non-empty-array<int|string, int|string>' ,
],
],
'arrayReverseDontPreserveKeyExplicitArg' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$d = array_reverse ([ " a " , " b " , 1 , " d " => 4 ], false ); ' ,
'assertions' => [
'$d' => 'non-empty-array<int|string, int|string>' ,
],
],
'arrayReversePreserveKey' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$d = array_reverse ([ " a " , " b " , 1 ], true ); ' ,
'assertions' => [
2022-12-13 21:40:19 +01:00
'$d' => 'array{0: string, 1: string, 2: int}' ,
2020-01-31 19:58:02 +01:00
],
],
'arrayDiff' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$d = array_diff ([ " a " => 5 , " b " => 12 ], [ 5 ]); ' ,
'assertions' => [
'$d' => 'array<string, int>' ,
],
],
'arrayDiffIsVariadic' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
array_diff ([], [], [], [], []); ' ,
'assertions' => [],
],
'arrayDiffKeyIsVariadic' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
array_diff_key ([], [], [], [], []); ' ,
'assertions' => [],
],
'arrayDiffAssoc' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ var array < string , int > $a
* @ var array $b
* @ var array $c
*/
$r = array_diff_assoc ( $a , $b , $c ); ' ,
'assertions' => [
'$r' => 'array<string, int>' ,
],
],
'arrayPopMixed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var mixed */
$b = [ " a " => 5 , " c " => 6 ];
$a = array_pop ( $b ); ' ,
'assertions' => [
'$a' => 'mixed' ,
'$b' => 'mixed' ,
],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MixedAssignment' , 'MixedArgument' ],
2020-01-31 19:58:02 +01:00
],
'arrayPopNonEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( $a ) {
$b = array_pop ( $a );
}
$c = array_pop ( $a ); ' ,
'assertions' => [
'$b' => 'int' ,
'$c' => 'int|null' ,
],
],
'arrayPopNonEmptyAfterIsset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( isset ( $a [ " a " ])) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCount' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( count ( $a )) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayShiftNonEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @param non-empty-list $arr */
function type_of_array_shift ( array $arr ) : int {
if ( \is_int ( $arr [ 0 ])) {
return \array_shift ( $arr );
}
return 0 ;
} ' ,
],
2020-08-30 17:44:14 +02:00
'arrayShiftFunkyTKeyedArrayList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-22 05:59:11 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param non - empty - list < string >| array { null } $arr
2020-07-22 05:59:11 +02:00
* @ return array < int , string >
*/
function foo ( array $arr ) {
array_shift ( $arr );
return $arr ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-07-22 05:59:11 +02:00
],
2020-01-31 19:58:02 +01:00
'arrayPopNonEmptyAfterCountEqualsOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( count ( $a ) === 1 ) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCountSoftEqualsOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( count ( $a ) == 1 ) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCountGreaterThanOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( count ( $a ) > 0 ) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCountGreaterOrEqualsOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( count ( $a ) >= 1 ) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCountEqualsOneReversed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( 1 === count ( $a )) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCountSoftEqualsOneReversed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( 1 == count ( $a )) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCountGreaterThanOneReversed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( 0 < count ( $a )) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'arrayPopNonEmptyAfterCountGreatorOrEqualToOneReversed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$b = 5 ;
if ( 1 <= count ( $a )) {
$b = array_pop ( $a );
} ' ,
'assertions' => [
'$b' => 'int' ,
],
],
2022-12-13 21:40:19 +01:00
'arrayNotEmptyArrayAfterCountBiggerThanEqualToOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 17:33:26 +02:00
/** @var list<int> */
2020-09-12 22:13:13 +02:00
$leftCount = [ 1 , 2 , 3 ];
2022-12-13 21:40:19 +01:00
if ( count ( $leftCount ) >= 1 ) {
2020-09-12 22:13:13 +02:00
echo $leftCount [ 0 ];
}
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
2022-12-13 21:40:19 +01:00
if ( 1 <= count ( $rightCount )) {
2020-09-12 22:13:13 +02:00
echo $rightCount [ 0 ];
} ' ,
],
2022-12-13 21:40:19 +01:00
'arrayNotEmptyArrayAfterCountBiggerThanTwo' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 22:13:13 +02:00
/** @var list<int> */
$leftCount = [ 1 , 2 , 3 ];
2022-12-13 21:40:19 +01:00
if ( count ( $leftCount ) > 2 ) {
2020-09-12 22:13:13 +02:00
echo $leftCount [ 0 ];
}
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
2022-12-13 21:40:19 +01:00
if ( 2 < count ( $rightCount )) {
2020-09-12 22:13:13 +02:00
echo $rightCount [ 0 ];
2020-09-12 17:33:26 +02:00
} ' ,
],
2020-09-12 22:13:13 +02:00
'arrayEmptyArrayAfterCountLessThanOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 22:13:13 +02:00
/** @var list<int> */
$leftCount = [ 1 , 2 , 3 ];
assert ( count ( $leftCount ) < 1 );
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
assert ( 1 > count ( $rightCount )); ' ,
'assertions' => [
2021-10-13 19:37:47 +02:00
'$leftCount' => 'array<never, never>' ,
'$rightCount' => 'array<never, never>' ,
2020-09-12 22:13:13 +02:00
],
],
'arrayEmptyArrayAfterCountLessThanEqualToZero' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 22:13:13 +02:00
/** @var list<int> */
$leftCount = [ 1 , 2 , 3 ];
assert ( count ( $leftCount ) <= 0 );
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
assert ( 0 >= count ( $rightCount )); ' ,
'assertions' => [
2021-10-13 19:37:47 +02:00
'$leftCount' => 'array<never, never>' ,
'$rightCount' => 'array<never, never>' ,
2020-09-12 22:13:13 +02:00
],
],
'arrayNotNonEmptyArrayAfterCountGreaterThanEqualToZero' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 22:13:13 +02:00
/** @var list<int> */
$leftCount = [ 1 , 2 , 3 ];
assert ( count ( $leftCount ) >= 0 );
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
assert ( 0 <= count ( $rightCount )); ' ,
'assertions' => [
'$leftCount' => 'list<int>' ,
'$rightCount' => 'list<int>' ,
],
],
'arrayNotNonEmptyArrayAfterCountGreaterThanMinusOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 22:13:13 +02:00
/** @var list<int> */
$leftCount = [ 1 , 2 , 3 ];
assert ( count ( $leftCount ) > - 1 );
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
assert ( - 1 < count ( $rightCount )); ' ,
'assertions' => [
'$leftCount' => 'list<int>' ,
'$rightCount' => 'list<int>' ,
],
],
'arrayNonEmptyArrayAfterCountGreaterThanEqualToOne' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 22:13:13 +02:00
/** @var list<int> */
$leftCount = [ 1 , 2 , 3 ];
assert ( count ( $leftCount ) >= 1 );
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
assert ( 1 <= count ( $rightCount )); ' ,
'assertions' => [
'$leftCount' => 'non-empty-list<int>' ,
'$rightCount' => 'non-empty-list<int>' ,
],
],
'arrayNonEmptyArrayAfterCountGreaterThanZero' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 22:13:13 +02:00
/** @var list<int> */
$leftCount = [ 1 , 2 , 3 ];
assert ( count ( $leftCount ) > 0 );
/** @var list<int> */
$rightCount = [ 1 , 2 , 3 ];
assert ( 0 < count ( $rightCount )); ' ,
'assertions' => [
'$leftCount' => 'non-empty-list<int>' ,
'$rightCount' => 'non-empty-list<int>' ,
],
],
2020-01-31 19:58:02 +01:00
'arrayPopNonEmptyAfterArrayAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, int> */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$a [ " foo " ] = 10 ;
$b = array_pop ( $a ); ' ,
'assertions' => [
'$b' => 'int' ,
],
],
2022-12-13 21:40:19 +01:00
'SKIPPED-arrayPopNonEmptyAfterMixedArrayAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array */
$a = [ " a " => 5 , " b " => 6 , " c " => 7 ];
$a [] = " hello " ;
$b = array_pop ( $a ); ' ,
'assertions' => [
'$b' => 'mixed|string' ,
],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [
2020-01-31 19:58:02 +01:00
'MixedAssignment' ,
],
],
'uasort' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-04-28 17:18:27 +02:00
function foo ( int $a , int $b ) : int {
return $a > $b ? 1 : - 1 ;
}
2020-01-31 19:58:02 +01:00
$manifest = [ " a " => 1 , " b " => 2 ];
uasort (
$manifest ,
2022-04-28 17:18:27 +02:00
" foo "
);
$emptyManifest = [];
uasort (
$emptyManifest ,
" foo "
);
' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-04-28 17:18:27 +02:00
'$manifest' => 'non-empty-array<string, int>' ,
2022-05-28 22:19:49 +02:00
'$emptyManifest' => 'array<never, never>' ,
2020-01-31 19:58:02 +01:00
],
],
'uksort' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-04-28 17:33:08 +02:00
function foo ( string $a , string $b ) : int {
return $a <=> $b ;
}
2022-05-28 20:49:12 +02:00
2020-01-31 19:58:02 +01:00
$array = [ " b " => 1 , " a " => 2 ];
uksort (
$array ,
2022-04-28 17:33:08 +02:00
" foo "
);
$emptyArray = [];
uksort (
$emptyArray ,
" foo "
2020-01-31 19:58:02 +01:00
); ' ,
'assertions' => [
2022-04-28 17:33:08 +02:00
'$array' => 'non-empty-array<string, int>' ,
2022-05-28 22:19:49 +02:00
'$emptyArray' => 'array<never, never>' ,
2020-01-31 19:58:02 +01:00
],
],
2020-08-30 17:44:14 +02:00
'arrayMergeTKeyedArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ param array < string , int > $a
* @ return array < string , int >
*/
function foo ( $a )
{
return $a ;
}
$a1 = [ " hi " => 3 ];
$a2 = [ " bye " => 5 ];
$a3 = array_merge ( $a1 , $a2 );
foo ( $a3 ); ' ,
'assertions' => [
2022-11-12 02:14:21 +01:00
'$a3' => 'array{bye: int, hi: int}' ,
2020-01-31 19:58:02 +01:00
],
],
2021-10-15 12:06:19 +02:00
'arrayReplaceTKeyedArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
* @ param array < string , int > $a
* @ return array < string , int >
*/
function foo ( $a )
{
return $a ;
}
$a1 = [ " hi " => 3 ];
$a2 = [ " bye " => 5 ];
$a3 = array_replace ( $a1 , $a2 );
foo ( $a3 ); ' ,
'assertions' => [
2022-11-12 02:14:21 +01:00
'$a3' => 'array{bye: int, hi: int}' ,
2021-10-15 12:06:19 +02:00
],
],
2020-01-31 19:58:02 +01:00
'arrayRand' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$vars = [ " x " => " a " , " y " => " b " ];
$c = array_rand ( $vars );
$d = $vars [ $c ];
$more_vars = [ " a " , " b " ];
$e = array_rand ( $more_vars ); ' ,
'assertions' => [
2022-11-12 02:14:21 +01:00
'$vars' => 'array{x: string, y: string}' ,
2020-01-31 19:58:02 +01:00
'$c' => 'string' ,
'$d' => 'string' ,
2022-11-12 02:14:21 +01:00
'$more_vars' => 'list{string, string}' ,
2022-12-13 21:40:19 +01:00
'$e' => 'int<0, 1>' ,
2020-01-31 19:58:02 +01:00
],
],
'arrayRandMultiple' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$vars = [ " x " => " a " , " y " => " b " ];
$b = 3 ;
$c = array_rand ( $vars , 1 );
$d = array_rand ( $vars , 2 );
$e = array_rand ( $vars , 3 );
$f = array_rand ( $vars , $b ); ' ,
'assertions' => [
2022-11-12 02:14:21 +01:00
'$vars' => 'array{x: string, y: string}' ,
2020-01-31 19:58:02 +01:00
'$c' => 'string' ,
'$e' => 'list<string>' ,
2022-12-19 22:23:31 +01:00
'$f' => 'list<string>' ,
2020-01-31 19:58:02 +01:00
],
],
'arrayKeysNoEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function expect_string ( string $x ) : void {
echo $x ;
}
function test () : void {
foreach ( array_keys ([]) as $key ) {
expect_string ( $key );
}
} ' ,
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MixedAssignment' , 'MixedArgument' , 'MixedArgumentTypeCoercion' , 'NoValue' ],
2020-01-31 19:58:02 +01:00
],
'arrayPopNotNullable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function expectsInt ( int $a ) : void {}
/**
2022-11-12 02:14:21 +01:00
* @ param array < array - key , array { item : int } > $list
2020-01-31 19:58:02 +01:00
*/
function test ( array $list ) : void
{
while ( ! empty ( $list )) {
$tmp = array_pop ( $list );
expectsInt ( $tmp [ " item " ]);
}
} ' ,
],
'arrayFilterWithAssert' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = array_filter (
[ 1 , " hello " , 6 , " goodbye " ],
function ( $s ) : bool {
return is_string ( $s );
}
); ' ,
'assertions' => [
2022-12-13 21:40:19 +01:00
'$a' => 'array<int<0, 3>, string>' ,
2020-01-31 19:58:02 +01:00
],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [
2020-01-31 19:58:02 +01:00
'MissingClosureParamType' ,
],
],
'arrayFilterUseKey' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$bar = " bar " ;
$foo = [
$bar => function () : string {
return " baz " ;
},
];
$foo = array_filter (
$foo ,
function ( string $key ) : bool {
return $key === " bar " ;
},
ARRAY_FILTER_USE_KEY
); ' ,
'assertions' => [
2022-01-16 16:07:56 +01:00
'$foo' => 'array<string, pure-Closure():string>' ,
2020-01-31 19:58:02 +01:00
],
],
'ignoreFalsableCurrent' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @param string[] $arr */
function foo ( array $arr ) : string {
return current ( $arr );
}
/** @param string[] $arr */
function bar ( array $arr ) : string {
$a = current ( $arr );
if ( $a === false ) {
return " hello " ;
}
return $a ;
}
/**
* @ param string [] $arr
* @ return false | string
*/
function bat ( array $arr ) {
return current ( $arr );
} ' ,
],
'arraySumEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$foo = array_sum ([]) + 1 ; ' ,
2020-05-18 23:23:21 +02:00
'assertions' => [
'$foo' => 'int' ,
],
],
'arraySumOnlyInt' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-05-18 23:23:21 +02:00
$foo = array_sum ([ 5 , 18 ]); ' ,
'assertions' => [
'$foo' => 'int' ,
],
],
'arraySumOnlyFloat' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-05-18 23:23:21 +02:00
$foo = array_sum ([ 5.1 , 18.2 ]); ' ,
'assertions' => [
'$foo' => 'float' ,
],
],
'arraySumNumeric' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-05-18 23:23:21 +02:00
$foo = array_sum ([ " 5 " , " 18 " ]); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
'$foo' => 'float|int' ,
],
],
2020-05-18 23:23:21 +02:00
'arraySumMix' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-05-18 23:23:21 +02:00
$foo = array_sum ([ 5 , 18.5 ]); ' ,
'assertions' => [
'$foo' => 'float' ,
],
],
2020-01-31 19:58:02 +01:00
'arrayMapWithArrayAndCallable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ psalm - return array < array - key , int >
*/
function foo ( array $v ) : array {
$r = array_map ( " intval " , $v );
return $r ;
} ' ,
],
2020-08-30 17:44:14 +02:00
'arrayMapTKeyedArrayAndCallable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
2022-11-12 02:14:21 +01:00
* @ psalm - return array { key1 : int , key2 : int }
2020-01-31 19:58:02 +01:00
*/
function foo () : array {
$v = [ " key1 " => 1 , " key2 " => " 2 " ];
$r = array_map ( " intval " , $v );
return $r ;
} ' ,
],
2020-08-30 17:44:14 +02:00
'arrayMapTKeyedArrayListAndCallable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @param list<int> $list */
function takesList ( array $list ) : void {}
takesList (
array_map (
" intval " ,
[ " 1 " , " 2 " , " 3 " ]
)
); ' ,
],
2020-08-30 17:44:14 +02:00
'arrayMapTKeyedArrayAndClosure' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
2022-11-12 02:14:21 +01:00
* @ psalm - return array { key1 : int , key2 : int }
2020-01-31 19:58:02 +01:00
*/
function foo () : array {
$v = [ " key1 " => 1 , " key2 " => " 2 " ];
$r = array_map ( function ( $i ) : int { return intval ( $i );}, $v );
return $r ;
} ' ,
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [
2022-12-18 17:15:15 +01:00
'MissingClosureParamType' ,
2020-01-31 19:58:02 +01:00
],
],
2020-08-30 17:44:14 +02:00
'arrayMapTKeyedArrayListAndClosure' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @param list<string> $list */
function takesList ( array $list ) : void {}
takesList (
array_map (
function ( string $str ) : string { return $str . " x " ; },
[ " foo " , " bar " , " baz " ]
)
); ' ,
],
'arrayMapUntypedCallable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ var callable $callable
* @ var array < string , int > $array
*/
$a = array_map ( $callable , $array );
/**
* @ var callable $callable
* @ var array < string , int > $array
*/
$b = array_map ( $callable , $array , $array );
/**
* @ var callable $callable
* @ var list < string > $list
*/
$c = array_map ( $callable , $list );
/**
* @ var callable $callable
* @ var list < string > $list
*/
$d = array_map ( $callable , $list , $list ); ' ,
'assertions' => [
'$a' => 'array<string, mixed>' ,
'$b' => 'list<mixed>' ,
'$c' => 'list<mixed>' ,
'$d' => 'list<mixed>' ,
],
],
'arrayFilterGoodArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function fooFoo ( int $i ) : bool {
return true ;
}
class A {
public static function barBar ( int $i ) : bool {
return true ;
}
}
array_filter ([ 1 , 2 , 3 ], " fooFoo " );
array_filter ([ 1 , 2 , 3 ], " foofoo " );
array_filter ([ 1 , 2 , 3 ], " FOOFOO " );
array_filter ([ 1 , 2 , 3 ], " A::barBar " );
array_filter ([ 1 , 2 , 3 ], " A::BARBAR " );
array_filter ([ 1 , 2 , 3 ], " A::barbar " ); ' ,
],
'arrayFilterIgnoreMissingClass' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
array_filter ([ 1 , 2 , 3 ], " A::bar " ); ' ,
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'UndefinedClass' ],
2020-01-31 19:58:02 +01:00
],
'arrayFilterIgnoreMissingMethod' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
class A {
public static function bar ( int $i ) : bool {
return true ;
}
}
array_filter ([ 1 , 2 , 3 ], " A::foo " ); ' ,
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'UndefinedMethod' ],
2020-01-31 19:58:02 +01:00
],
'arrayMapParamDefault' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$arr = [ " a " , " b " ];
array_map ( " mapdef " , $arr , array_fill ( 0 , count ( $arr ), 1 ));
function mapdef ( string $_a , int $_b = 0 ) : string {
return " a " ;
} ' ,
],
2021-05-15 02:12:39 +02:00
'arrayFillZeroLength' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-05-15 02:12:39 +02:00
count ( array_fill ( 0 , 0 , 0 )) === 0 ; ' ,
],
2022-12-13 22:29:35 +01:00
'arrayFillLiteral' => [
'code' => ' < ? php
$a = array_fill ( 0 , 3 , 0 );
$b = array_fill ( - 1 , 3 , 0 );
$c = array_fill ( - 2 , 3 , 0 );
' ,
'assertions' => [
'$a===' => 'list{0, 0, 0}' ,
// Techinically this doesn't cover the case of running on 8.0 but nvm
'$b===' => 'array{-1: 0, 0: 0, 1: 0}' ,
'$c===' => 'array{-2: 0, 0: 0, 1: 0}' ,
],
'ignored_issues' => [],
2022-12-18 17:15:15 +01:00
'php_version' => '7.4' ,
2022-12-13 22:29:35 +01:00
],
'arrayFillLiteral80' => [
'code' => ' < ? php
$a = array_fill ( 0 , 3 , 0 );
$b = array_fill ( - 1 , 3 , 0 );
$c = array_fill ( - 2 , 3 , 0 );
' ,
'assertions' => [
'$a===' => 'list{0, 0, 0}' ,
'$b===' => 'array{-1: 0, 0: 0, 1: 0}' ,
'$c===' => 'array{-1: 0, -2: 0, 0: 0}' ,
],
'ignored_issues' => [],
2022-12-18 17:15:15 +01:00
'php_version' => '8.0' ,
2022-12-13 22:29:35 +01:00
],
2020-01-31 19:58:02 +01:00
'implodeMultiDimensionalArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$urls = array_map ( " implode " , [[ " a " , " b " ]]); ' ,
],
2020-05-15 16:18:05 +02:00
'implodeNonEmptyArrayAndString' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-05-15 16:18:05 +02:00
$l = [ " a " , " b " ];
2021-06-15 05:24:09 +02:00
$k = [ 1 , 2 , 3 ];
$a = implode ( " : " , $l );
$b = implode ( " : " , $k ); ' ,
2022-01-13 19:49:37 +01:00
'assertions' => [
2021-06-14 22:03:37 +02:00
'$a===' => 'non-empty-literal-string' ,
2021-06-15 05:24:09 +02:00
'$b===' => 'non-empty-literal-string' ,
2022-12-18 17:15:15 +01:00
],
2020-05-15 16:18:05 +02:00
],
2022-05-14 16:36:51 +02:00
'implodeArrayOfNonEmptyStringAndEmptyString' => [
2022-05-28 20:49:12 +02:00
'code' => ' < ? php
2022-05-14 16:36:51 +02:00
class Foo {
const DIR = __DIR__ ;
}
$l = [ " a " , " b " ];
$k = [ Foo :: DIR ];
$a = implode ( " " , $l );
$b = implode ( " " , $k ); ' ,
2022-05-28 20:49:12 +02:00
'assertions' => [
2022-05-14 16:36:51 +02:00
'$a===' => 'non-empty-literal-string' ,
'$b===' => 'non-empty-string' ,
2022-12-18 17:15:15 +01:00
],
2022-05-14 16:36:51 +02:00
],
'implodeEmptyArrayAndString' => [
2022-05-28 20:49:12 +02:00
'code' => ' < ? php
2022-05-14 16:36:51 +02:00
$l = [ " " ];
$k = [];
$a = implode ( " " , $l );
$b = implode ( " " , $k ); ' ,
2022-05-28 20:49:12 +02:00
'assertions' => [
2022-05-14 16:36:51 +02:00
'$a===' => 'string' ,
'$b===' => 'string' ,
2022-12-18 17:15:15 +01:00
],
2022-05-14 16:36:51 +02:00
],
2020-01-31 19:58:02 +01:00
'key' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [ " one " => 1 , " two " => 3 ];
2020-07-15 07:53:31 +02:00
$b = key ( $a ); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-10-14 00:58:12 +02:00
'$b' => 'string' ,
2020-07-20 10:49:36 +02:00
],
],
'keyEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-20 10:49:36 +02:00
$a = [];
$b = key ( $a ); ' ,
'assertions' => [
'$b' => 'null' ,
2020-01-31 19:58:02 +01:00
],
],
2020-07-15 15:49:30 +02:00
'keyNonEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-15 15:49:30 +02:00
/**
* @ param non - empty - array $arr
2022-10-14 00:58:12 +02:00
* @ return array - key
2020-07-15 15:49:30 +02:00
*/
function foo ( array $arr ) {
return key ( $arr );
} ' ,
],
2022-10-14 00:58:12 +02:00
'current' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
$a = [ " one " => 1 , " two " => 3 ];
$b = current ( $a ); ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'currentEmptyArray' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
$a = [];
$b = current ( $a ); ' ,
'assertions' => [
'$b' => 'false' ,
],
],
'currentNonEmptyArray' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
/**
* @ param non - empty - array < int > $arr
* @ return int
*/
function foo ( array $arr ) {
return current ( $arr );
} ' ,
],
'reset' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
$a = [ " one " => 1 , " two " => 3 ];
$b = reset ( $a ); ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'resetEmptyArray' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
$a = [];
$b = reset ( $a ); ' ,
'assertions' => [
'$b' => 'false' ,
],
],
'resetNonEmptyArray' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
/**
* @ param non - empty - array < int > $arr
* @ return int
*/
function foo ( array $arr ) {
return reset ( $arr );
} ' ,
],
'end' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
$a = [ " one " => 1 , " two " => 3 ];
$b = end ( $a ); ' ,
'assertions' => [
'$b' => 'int' ,
],
],
'endEmptyArray' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
$a = [];
$b = end ( $a ); ' ,
'assertions' => [
'$b' => 'false' ,
],
],
'endNonEmptyArray' => [
2022-11-08 10:45:21 +01:00
'code' => ' < ? php
2022-10-14 00:58:12 +02:00
/**
* @ param non - empty - array < int > $arr
* @ return int
*/
function foo ( array $arr ) {
return end ( $arr );
} ' ,
],
2020-07-14 23:43:26 +02:00
'arrayKeyFirst' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-14 23:43:26 +02:00
/** @return array<string, int> */
function makeArray () : array { return [ " one " => 1 , " two " => 3 ]; }
$a = makeArray ();
$b = array_key_first ( $a );
$c = null ;
if ( $b !== null ) {
$c = $a [ $b ];
} ' ,
'assertions' => [
'$b' => 'null|string' ,
'$c' => 'int|null' ,
],
],
'arrayKeyFirstNonEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [ " one " => 1 , " two " => 3 ];
$b = array_key_first ( $a );
$c = $a [ $b ]; ' ,
'assertions' => [
2020-07-14 23:43:26 +02:00
'$b' => 'string' ,
2020-01-31 19:58:02 +01:00
'$c' => 'int' ,
],
],
2020-07-14 23:43:26 +02:00
'arrayKeyFirstEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-14 23:43:26 +02:00
$a = [];
$b = array_key_first ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'null' ,
2020-07-14 23:43:26 +02:00
],
],
'arrayKeyLast' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-14 23:43:26 +02:00
/** @return array<string, int> */
function makeArray () : array { return [ " one " => 1 , " two " => 3 ]; }
$a = makeArray ();
$b = array_key_last ( $a );
$c = null ;
if ( $b !== null ) {
$c = $a [ $b ];
} ' ,
'assertions' => [
'$b' => 'null|string' ,
'$c' => 'int|null' ,
],
],
'arrayKeyLastNonEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [ " one " => 1 , " two " => 3 ];
$b = array_key_last ( $a );
$c = $a [ $b ]; ' ,
'assertions' => [
2020-07-14 23:43:26 +02:00
'$b' => 'string' ,
2020-01-31 19:58:02 +01:00
'$c' => 'int' ,
],
],
2020-07-14 23:43:26 +02:00
'arrayKeyLastEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-14 23:43:26 +02:00
$a = [];
$b = array_key_last ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'null' ,
2020-07-14 23:43:26 +02:00
],
],
2020-08-07 18:23:20 +02:00
'arrayResetNonEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return non-empty-array<string, int> */
function makeArray () : array { return [ " one " => 1 , " two " => 3 ]; }
$a = makeArray ();
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'int' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayResetNonEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return non-empty-list<int> */
function makeArray () : array { return [ 1 , 3 ]; }
$a = makeArray ();
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'int' ,
2020-08-07 18:23:20 +02:00
],
],
2020-08-30 17:44:14 +02:00
'arrayResetNonEmptyTKeyedArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
$a = [ " one " => 1 , " two " => 3 ];
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'int' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayResetEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
$a = [];
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayResetEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-12-15 15:29:06 +01:00
/** @return list<never> */
2020-08-07 18:23:20 +02:00
function makeArray () : array { return []; }
$a = makeArray ();
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayResetMaybeEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return array<string, int> */
function makeArray () : array { return [ " one " => 1 , " two " => 3 ]; }
$a = makeArray ();
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false|int' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayResetMaybeEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return list<int> */
function makeArray () : array { return []; }
$a = makeArray ();
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false|int' ,
2020-08-07 18:23:20 +02:00
],
],
2020-08-30 17:44:14 +02:00
'arrayResetMaybeEmptyTKeyedArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-11-12 02:14:21 +01:00
/** @return array{foo?: int} */
2020-08-07 18:23:20 +02:00
function makeArray () : array { return []; }
$a = makeArray ();
$b = reset ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false|int' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayEndNonEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return non-empty-array<string, int> */
function makeArray () : array { return [ " one " => 1 , " two " => 3 ]; }
$a = makeArray ();
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'int' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayEndNonEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return non-empty-list<int> */
function makeArray () : array { return [ 1 , 3 ]; }
$a = makeArray ();
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'int' ,
2020-08-07 18:23:20 +02:00
],
],
2020-08-30 17:44:14 +02:00
'arrayEndNonEmptyTKeyedArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
$a = [ " one " => 1 , " two " => 3 ];
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'int' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayEndEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
$a = [];
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayEndEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-12-15 15:29:06 +01:00
/** @return list<never> */
2020-08-07 18:23:20 +02:00
function makeArray () : array { return []; }
$a = makeArray ();
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayEndMaybeEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return array<string, int> */
function makeArray () : array { return [ " one " => 1 , " two " => 3 ]; }
$a = makeArray ();
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false|int' ,
2020-08-07 18:23:20 +02:00
],
],
'arrayEndMaybeEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-07 18:23:20 +02:00
/** @return list<int> */
function makeArray () : array { return []; }
$a = makeArray ();
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false|int' ,
2020-08-07 18:23:20 +02:00
],
],
2020-08-30 17:44:14 +02:00
'arrayEndMaybeEmptyTKeyedArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-11-12 02:14:21 +01:00
/** @return array{foo?: int} */
2020-08-07 18:23:20 +02:00
function makeArray () : array { return []; }
$a = makeArray ();
$b = end ( $a ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$b' => 'false|int' ,
2020-08-07 18:23:20 +02:00
],
],
2020-01-31 19:58:02 +01:00
'arrayColumnInference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function makeMixedArray () : array { return []; }
/** @return array<array<int,bool>> */
function makeGenericArray () : array { return []; }
2022-11-12 02:14:21 +01:00
/** @return array<array{0:string}> */
2020-01-31 19:58:02 +01:00
function makeShapeArray () : array { return []; }
2022-11-12 02:14:21 +01:00
/** @return array<array{0:string}|int> */
2020-01-31 19:58:02 +01:00
function makeUnionArray () : array { return []; }
2022-11-12 02:14:21 +01:00
/** @return array<string, array{x?:int, y?:int, width?:int, height?:int}> */
2020-11-27 23:05:54 +01:00
function makeKeyedArray () : array { return []; }
2020-01-31 19:58:02 +01:00
$a = array_column ([[ 1 ], [ 2 ], [ 3 ]], 0 );
$b = array_column ([[ " a " => 1 ], [ " a " => 2 ], [ " a " => 3 ]], " a " );
2022-04-27 07:36:17 +02:00
$c = array_column ([[ " a " => 1 ], [ " a " => 2 ], [ " a " => 3 ]], null , " a " );
$d = array_column ([[ " a " => 1 ], [ " a " => 2 ], [ " a " => 3 ]], null , " b " );
$e = array_column ([[ " a " => 1 ], [ " a " => 2 ], [ " a " => 3 ]], rand ( 0 , 1 ) ? " a " : " b " , " b " );
$f = array_column ([[ " k " => " a " , " v " => 1 ], [ " k " => " b " , " v " => 2 ]], " v " , " k " );
$g = array_column ([], 0 );
$h = array_column ( makeMixedArray (), 0 );
$i = array_column ( makeMixedArray (), 0 , " k " );
$j = array_column ( makeMixedArray (), 0 , null );
$k = array_column ( makeGenericArray (), 0 );
$l = array_column ( makeShapeArray (), 0 );
$m = array_column ( makeUnionArray (), 0 );
$n = array_column ([[ 0 => " test " ]], 0 );
$o = array_column ( makeKeyedArray (), " y " );
$p_prepare = makeKeyedArray ();
assert ( $p_prepare !== []);
$p = array_column ( $p_prepare , " y " );
2020-01-31 19:58:02 +01:00
' ,
'assertions' => [
2022-12-13 22:29:35 +01:00
'$a===' => 'list{1, 2, 3}' ,
'$b===' => 'list{1, 2, 3}' ,
'$c' => 'array{1: array{a: int}, 2: array{a: int}, 3: array{a: int}}' ,
2022-11-12 02:14:21 +01:00
'$d' => 'array<array-key, array{a: int}>' ,
2022-04-27 07:36:17 +02:00
'$e' => 'array<array-key, mixed>' ,
2022-12-13 22:29:35 +01:00
'$f' => 'array{a: int, b: int}' ,
2020-01-31 19:58:02 +01:00
'$g' => 'list<mixed>' ,
'$h' => 'list<mixed>' ,
2022-04-27 07:36:17 +02:00
'$i' => 'array<array-key, mixed>' ,
2020-01-31 19:58:02 +01:00
'$j' => 'list<mixed>' ,
2022-04-27 07:36:17 +02:00
'$k' => 'list<mixed>' ,
'$l' => 'list<string>' ,
'$m' => 'list<mixed>' ,
2022-12-13 22:29:35 +01:00
'$n' => 'list{string}' ,
2022-04-27 07:36:17 +02:00
'$o' => 'list<int>' ,
'$p' => 'list<int>' ,
2020-01-31 19:58:02 +01:00
],
],
2022-12-13 22:29:35 +01:00
'arrayColumnExactInference' => [
'code' => ' < ? php
$a = array_column ([
[ " v " => " a " ],
[ " v " => " b " ],
[ " v " => " c " ],
[ " v " => " d " ],
], " v " );
$b = array_column ([
[ " v " => " a " ],
[],
[ " v " => " c " ],
[ " v " => " d " ],
], " v " );
$c = array_column ([
[ " v " => " a " ],
123 ,
[ " v " => " c " ],
[ " v " => " d " ],
], " v " );
$d = array_column ([
[ " v " => " a " , " k " => " A " ],
[ " v " => " b " , " k " => " B " ],
[ " v " => " c " , " k " => " C " ],
[ " v " => " d " , " k " => " D " ],
], " v " , " k " );
$e = array_column ([
[ " v " => " a " , " k " => 0 ],
[ " v " => " b " , " k " => 1 ],
[ " v " => " c " , " k " => 2 ],
[ " v " => " d " , " k " => 3 ],
], " v " , " k " );
$f = array_column ([
[ " v " => " a " , " k " => 3 ],
[ " v " => " b " , " k " => 2 ],
[ " v " => " c " , " k " => 1 ],
[ " v " => " d " , " k " => 0 ],
], " v " , " k " );
$g = array_column ([
[ " v " => " a " , " k " => 0 ],
[ " v " => " b " , " k " => 1 ],
[ " v " => " c " , " k " => 2 ],
[ " v " => " d " , " k " => 3 ],
], null , " k " );
$h = array_column ([
" a " => [ " k " => 0 ],
" b " => [ " k " => 1 ],
" c " => [ " k " => 2 ],
], null , " k " );
/** @var array{a: array{v: 0}, b?: array{v: 1}} */
$aa = [];
$i = array_column ( $aa , " v " );
/** @var array{a: array{v: "a", k: 0}, b?: array{v: "b", k: 1}, c: array{v: "c", k: 2}} */
$aa = [];
$j = array_column ( $aa , null , " k " );
/** @var array{a: array{v: "a", k: 0}, b: array{v: "b", k: 1}, c?: array{v: "c", k: 2}} */
$aa = [];
$k = array_column ( $aa , null , " k " );
2022-12-19 13:49:42 +01:00
$l = array_column ([ " test " => [ " v " => " a " ], " test2 " => [ " v " => " b " ]], " v " );
2022-12-13 22:29:35 +01:00
' ,
'assertions' => [
'$a===' => " list { 'a', 'b', 'c', 'd'} " ,
'$b===' => " list { 'a', 'c', 'd'} " ,
'$c===' => " list { 'a', 'c', 'd'} " ,
'$d===' => " array { A: 'a', B: 'b', C: 'c', D: 'd'} " ,
'$e===' => " list { 'a', 'b', 'c', 'd'} " ,
'$f===' => " array { 0: 'd', 1: 'c', 2: 'b', 3: 'a'} " ,
'$g===' => " list { array { k: 0, v: 'a'}, array { k: 1, v: 'b'}, array { k: 2, v: 'c'}, array { k: 3, v: 'd'}} " ,
'$h===' => " list { array { k: 0}, array { k: 1}, array { k: 2}} " ,
2022-12-19 14:27:41 +01:00
'$i===' => " list { 0: 0, 1?: 1} " ,
2022-12-13 22:29:35 +01:00
'$j===' => " array { 0: array { k: 0, v: 'a'}, 1?: array { k: 1, v: 'b'}, 2: array { k: 2, v: 'c'}} " ,
'$k===' => " list { 0: array { k: 0, v: 'a'}, 1: array { k: 1, v: 'b'}, 2?: array { k: 2, v: 'c'}} " ,
2022-12-19 13:49:42 +01:00
'$l===' => " list { 'a', 'b'} " ,
2022-12-18 17:15:15 +01:00
],
2022-12-13 22:29:35 +01:00
],
2020-01-31 19:58:02 +01:00
'splatArrayIntersect' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$foo = [
[ 1 , 2 , 3 ],
[ 1 , 2 ],
];
$bar = array_intersect ( ... $foo ); ' ,
'assertions' => [
2022-12-13 21:40:19 +01:00
'$bar' => 'array<int<0, 2>, int>' ,
2020-01-31 19:58:02 +01:00
],
],
'arrayIntersectIsVariadic' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
array_intersect ([], [], [], [], []); ' ,
'assertions' => [],
],
'arrayIntersectKeyIsVariadic' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
array_intersect_key ([], [], [], [], []); ' ,
'assertions' => [],
],
'arrayIntersectKeyNoReturnType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ psalm - suppress MissingReturnType
*/
function unknown () {
return [ " x " => " hello " ];
}
class C {
/**
* @ psalm - suppress MissingReturnType
*/
public static function unknownStatic () {
return [ " x " => " hello " ];
}
/**
* @ psalm - suppress MissingReturnType
*/
public static function unknownInstance () {
return [ " x " => " hello " ];
}
}
/**
* @ psalm - suppress MixedArgument
*/
function sdn ( array $s ) : void {
$r = array_intersect_key ( unknown (), array_filter ( $s ));
if ( empty ( $r )) {}
$r = array_intersect_key ( C :: unknownStatic (), array_filter ( $s ));
if ( empty ( $r )) {}
$r = array_intersect_key (( new C ) -> unknownInstance (), array_filter ( $s ));
if ( empty ( $r )) {}
} ' ,
],
'arrayIntersectAssoc' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ var array < string , int > $a
* @ var array $b
* @ var array $c
*/
$r = array_intersect_assoc ( $a , $b , $c ); ' ,
'assertions' => [
'$r' => 'array<string, int>' ,
],
],
'arrayReduce' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$arr = [ 2 , 3 , 4 , 5 ];
function multiply ( int $carry , int $item ) : int {
return $carry * $item ;
}
$f2 = function ( int $carry , int $item ) : int {
return $carry * $item ;
};
$direct_closure_result = array_reduce (
$arr ,
function ( int $carry , int $item ) : int {
return $carry * $item ;
},
1
);
$passed_closure_result = array_reduce (
$arr ,
$f2 ,
1
);
$function_call_result = array_reduce (
$arr ,
" multiply " ,
1
); ' ,
'assertions' => [
'$direct_closure_result' => 'int' ,
'$passed_closure_result' => 'int' ,
'$function_call_result' => 'int' ,
],
],
2021-06-29 19:08:57 +02:00
'arrayReduceStaticMethods' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-06-29 19:08:57 +02:00
$arr = [ 2 , 3 , 4 , 5 ];
class C {
public static function multiply ( int $carry , int $item ) : int {
return $carry * $item ;
}
public static function multiplySelf ( array $arr ) : int {
return array_reduce ( $arr , [ self :: class , " multiply " ], 1 );
}
public static function multiplyStatic ( array $arr ) : int {
return array_reduce ( $arr , [ static :: class , " multiply " ], 1 );
}
}
$self_call_result = C :: multiplySelf ( $arr );
$static_call_result = C :: multiplyStatic ( $arr ); ' ,
'assertions' => [],
],
2020-01-31 19:58:02 +01:00
'arrayReduceMixedReturn' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$arr = [ 2 , 3 , 4 , 5 ];
$direct_closure_result = array_reduce (
$arr ,
function ( int $carry , int $item ) {
2022-09-10 13:06:17 +02:00
return $GLOBALS [ " boo " ];
2020-01-31 19:58:02 +01:00
},
1
); ' ,
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MissingClosureReturnType' , 'MixedAssignment' ],
2020-01-31 19:58:02 +01:00
],
2020-07-22 05:59:11 +02:00
'arraySpliceArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [ 1 , 2 , 3 ];
$c = $a ;
$b = [ " a " , " b " , " c " ];
2020-07-22 05:59:11 +02:00
array_splice ( $a , rand ( - 10 , 0 ), rand ( 0 , 10 ), $b ); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2020-03-09 06:15:52 +01:00
'$a' => 'non-empty-list<int|string>' ,
2022-11-12 02:14:21 +01:00
'$b' => 'list{string, string, string}' ,
'$c' => 'list{int, int, int}' ,
2020-07-22 05:59:11 +02:00
],
],
'arraySpliceReturn' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-22 05:59:11 +02:00
$d = [ 1 , 2 , 3 ];
$e = array_splice ( $d , - 1 , 1 ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$e' => 'list<int>' ,
2020-01-31 19:58:02 +01:00
],
],
'arraySpliceOtherType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$d = [[ " red " ], [ " green " ], [ " blue " ]];
array_splice ( $d , - 1 , 1 , " foo " ); ' ,
'assertions' => [
2022-11-12 02:14:21 +01:00
'$d' => 'array<int, list{string}|string>' ,
2020-01-31 19:58:02 +01:00
],
],
2023-04-12 21:45:09 +02:00
'arraySpliceRefWithoutReplacement' => [
2023-04-12 21:04:29 +02:00
'code' => ' < ? php
$d = [ 1 , 2 ];
2023-04-13 11:07:09 +02:00
$o = 0 ;
array_splice ( $d , $o , 1 ); ' ,
2023-04-12 21:04:29 +02:00
'assertions' => [
'$d' => 'list<int>' ,
],
],
2023-04-12 21:45:09 +02:00
'arraySpliceEmptyRefWithoutReplacement' => [
'code' => ' < ? php
$a = array ( " hello " );
2023-04-13 11:07:09 +02:00
$_b = array_splice ( $a , 0 , 1 ); ' ,
2023-04-12 21:45:09 +02:00
'assertions' => [
2023-04-13 11:07:09 +02:00
'$a' => 'array<never, never>' ,
],
],
'arraySpliceEmptyRefWithEmptyReplacement' => [
'code' => ' < ? php
$a = array ( " hello " );
$_b = array_splice ( $a , 0 , 1 , [] ); ' ,
'assertions' => [
'$a' => 'array<never, never>' ,
2023-04-12 21:45:09 +02:00
],
],
2023-04-18 21:08:35 +02:00
'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>' ,
],
],
2020-01-31 19:58:02 +01:00
'ksortPreserveShape' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [ " a " => 3 , " b " => 4 ];
ksort ( $a );
acceptsAShape ( $a );
/**
2022-11-12 02:14:21 +01:00
* @ param array { a : int , b : int } $a
2020-01-31 19:58:02 +01:00
*/
function acceptsAShape ( array $a ) : void {} ' ,
],
'arraySlicePreserveKeys' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [ " a " => 1 , " b " => 2 , " c " => 3 ];
$b = array_slice ( $a , 1 , 2 , true );
$c = array_slice ( $a , 1 , 2 , false );
$d = array_slice ( $a , 1 , 2 ); ' ,
'assertions' => [
'$b' => 'array<string, int>' ,
'$c' => 'array<string, int>' ,
'$d' => 'array<string, int>' ,
],
],
'arraySliceDontPreserveIntKeys' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [ 1 => " a " , 4 => " b " , 3 => " c " ];
$b = array_slice ( $a , 1 , 2 , true );
$c = array_slice ( $a , 1 , 2 , false );
$d = array_slice ( $a , 1 , 2 ); ' ,
'assertions' => [
'$b' => 'array<int, string>' ,
'$c' => 'list<string>' ,
'$d' => 'list<string>' ,
],
],
'arrayReversePreserveNonEmptiness' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @param string[] $arr */
function getOrderings ( array $arr ) : int {
if ( $arr ) {
$next = null ;
foreach ( array_reverse ( $arr ) as $v ) {
$next = 1 ;
}
return $next ;
}
return 2 ;
} ' ,
],
'inferArrayMapReturnType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @return array<string> */
function Foo ( DateTime ... $dateTimes ) : array {
return array_map (
function ( $dateTime ) {
2021-07-03 07:19:37 +02:00
return ( $dateTime -> format ( " c " ));
2020-01-31 19:58:02 +01:00
},
$dateTimes
);
} ' ,
],
'inferArrayMapArrowFunctionReturnType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @return array<string> */
function Foo ( DateTime ... $dateTimes ) : array {
return array_map (
2021-07-03 07:19:37 +02:00
fn ( $dateTime ) => ( $dateTime -> format ( " c " )),
2020-01-31 19:58:02 +01:00
$dateTimes
);
} ' ,
2020-08-23 16:32:07 +02:00
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [],
2021-07-03 07:19:37 +02:00
'php_version' => '7.4' ,
2020-01-31 19:58:02 +01:00
],
'arrayPad' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = array_pad ([ " foo " => 1 , " bar " => 2 ], 10 , 123 );
$b = array_pad ([ " a " , " b " , " c " ], 10 , " x " );
/** @var list<int> $list */
$c = array_pad ( $list , 10 , 0 );
/** @var array<string, string> $array */
$d = array_pad ( $array , 10 , " " ); ' ,
'assertions' => [
'$a' => 'non-empty-array<int|string, int>' ,
'$b' => 'non-empty-list<string>' ,
'$c' => 'non-empty-list<int>' ,
'$d' => 'non-empty-array<int|string, string>' ,
],
],
'arrayPadDynamicSize' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function getSize () : int { return random_int ( 1 , 10 ); }
$a = array_pad ([ " foo " => 1 , " bar " => 2 ], getSize (), 123 );
$b = array_pad ([ " a " , " b " , " c " ], getSize (), " x " );
/** @var list<int> $list */
$c = array_pad ( $list , getSize (), 0 );
/** @var array<string, string> $array */
$d = array_pad ( $array , getSize (), " " ); ' ,
'assertions' => [
'$a' => 'array<int|string, int>' ,
'$b' => 'list<string>' ,
'$c' => 'list<int>' ,
'$d' => 'array<int|string, string>' ,
],
],
'arrayPadZeroSize' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array $arr */
$result = array_pad ( $arr , 0 , null ); ' ,
'assertions' => [
'$result' => 'array<array-key, mixed|null>' ,
],
],
'arrayPadTypeCombination' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = array_pad ([ " foo " => 1 , " bar " => " two " ], 5 , false );
$b = array_pad ([ " a " , 2 , 3.14 ], 5 , null );
/** @var list<string|bool> $list */
$c = array_pad ( $list , 5 , 0 );
/** @var array<string, string> $array */
$d = array_pad ( $array , 5 , null ); ' ,
'assertions' => [
'$a' => 'non-empty-array<int|string, false|int|string>' ,
'$b' => 'non-empty-list<float|int|null|string>' ,
'$c' => 'non-empty-list<bool|int|string>' ,
'$d' => 'non-empty-array<int|string, null|string>' ,
],
],
'arrayPadMixed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-11-12 02:14:21 +01:00
/** @var array{foo: mixed, bar: mixed} $arr */
2020-01-31 19:58:02 +01:00
$a = array_pad ( $arr , 5 , null );
/** @var mixed $mixed */
$b = array_pad ([ $mixed , $mixed ], 5 , null );
/** @var list $list */
$c = array_pad ( $list , 5 , null );
/** @var mixed[] $array */
$d = array_pad ( $array , 5 , null ); ' ,
'assertions' => [
'$a' => 'non-empty-array<int|string, mixed|null>' ,
'$b' => 'non-empty-list<mixed|null>' ,
'$c' => 'non-empty-list<mixed|null>' ,
'$d' => 'non-empty-array<array-key, mixed|null>' ,
],
],
'arrayPadFallback' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ var mixed $mixed
* @ psalm - suppress MixedArgument
*/
$result = array_pad ( $mixed , $mixed , $mixed ); ' ,
'assertions' => [
'$result' => 'array<array-key, mixed>' ,
],
],
'arrayChunk' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-11-12 02:14:21 +01:00
/** @var array{a: int, b: int, c: int, d: int} $arr */
2020-01-31 19:58:02 +01:00
$a = array_chunk ( $arr , 2 );
/** @var list<string> $list */
$b = array_chunk ( $list , 2 );
/** @var array<string, float> $arr */
$c = array_chunk ( $arr , 2 );
' ,
'assertions' => [
'$a' => 'list<non-empty-list<int>>' ,
'$b' => 'list<non-empty-list<string>>' ,
'$c' => 'list<non-empty-list<float>>' ,
],
],
'arrayChunkPreservedKeys' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-11-12 02:14:21 +01:00
/** @var array{a: int, b: int, c: int, d: int} $arr */
2020-01-31 19:58:02 +01:00
$a = array_chunk ( $arr , 2 , true );
/** @var list<string> $list */
$b = array_chunk ( $list , 2 , true );
/** @var array<string, float> $arr */
$c = array_chunk ( $arr , 2 , true ); ' ,
'assertions' => [
'$a' => 'list<non-empty-array<string, int>>' ,
2022-12-13 21:40:19 +01:00
'$b' => 'list<non-empty-array<int<0, max>, string>>' ,
2020-01-31 19:58:02 +01:00
'$c' => 'list<non-empty-array<string, float>>' ,
],
],
'arrayChunkPreservedKeysExplicitFalse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @var array<string, string> $arr */
$result = array_chunk ( $arr , 2 , false ); ' ,
'assertions' => [
'$result' => 'list<non-empty-list<string>>' ,
],
],
'arrayChunkMixed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-11-12 02:14:21 +01:00
/** @var array{a: mixed, b: mixed, c: mixed} $arr */
2020-01-31 19:58:02 +01:00
$a = array_chunk ( $arr , 2 );
/** @var list<mixed> $list */
$b = array_chunk ( $list , 2 );
/** @var mixed[] $arr */
$c = array_chunk ( $arr , 2 ); ' ,
'assertions' => [
'$a' => 'list<non-empty-list<mixed>>' ,
'$b' => 'list<non-empty-list<mixed>>' ,
'$c' => 'list<non-empty-list<mixed>>' ,
],
],
'arrayChunkFallback' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ var mixed $mixed
* @ psalm - suppress MixedArgument
*/
$result = array_chunk ( $mixed , $mixed , $mixed ); ' ,
'assertions' => [
'$result' => 'list<array<array-key, mixed>>' ,
],
],
'arrayMapPreserveNonEmptiness' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ psalm - param non - empty - list < string > $strings
* @ psalm - return non - empty - list < int >
*/
function foo ( array $strings ) : array {
return array_map ( " intval " , $strings );
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-31 19:58:02 +01:00
],
2020-10-28 18:32:55 +01:00
'SKIPPED-arrayMapZip' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
2022-11-12 02:14:21 +01:00
* @ return array < int , array { string , ? string } >
2020-01-31 19:58:02 +01:00
*/
function getCharPairs ( string $line ) : array {
$chars = str_split ( $line );
return array_map (
null ,
$chars ,
array_slice ( $chars , 1 )
);
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-31 19:58:02 +01:00
],
'arrayFillKeys' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-12-13 22:29:35 +01:00
/** @var list<int> */
2020-01-31 19:58:02 +01:00
$keys = [ 1 , 2 , 3 ];
2022-12-13 22:29:35 +01:00
$a = array_fill_keys ( $keys , true );
$keys = [ 1 , 2 , 3 ];
$b = array_fill_keys ( $keys , true );
$keys = [ 0 , 1 , 2 ];
$c = array_fill_keys ( $keys , true );
$keys = random_int ( 0 , 1 ) ? [ 0 ] : [ 0 , 1 ];
$d = array_fill_keys ( $keys , true );
$keys = random_int ( 0 , 1 ) ? [ " a " ] : [ " a " , " b " ];
$e = array_fill_keys ( $keys , true );
' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-12-13 22:29:35 +01:00
'$a===' => 'array<int, true>' ,
'$b===' => 'array{1: true, 2: true, 3: true}' ,
'$c===' => 'list{true, true, true}' ,
'$d===' => 'list{0: true, 1?: true}' ,
'$e===' => 'array{a: true, b?: true}' ,
2020-01-31 19:58:02 +01:00
],
],
'shuffle' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$array = [ " foo " => 123 , " bar " => 456 ];
2022-04-28 16:18:39 +02:00
shuffle ( $array );
$emptyArray = [];
shuffle ( $emptyArray ); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-04-28 16:18:39 +02:00
'$array' => 'non-empty-list<int>' ,
2022-05-28 22:19:49 +02:00
'$emptyArray' => 'list<never>' ,
2020-01-31 19:58:02 +01:00
],
],
'sort' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$array = [ " foo " => 123 , " bar " => 456 ];
2022-04-23 18:00:38 +02:00
sort ( $array );
$emptyArray = [];
sort ( $emptyArray ); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-04-23 18:00:38 +02:00
'$array' => 'non-empty-list<int>' ,
2022-05-28 22:19:49 +02:00
'$emptyArray' => 'list<never>' ,
2020-01-31 19:58:02 +01:00
],
],
'rsort' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$array = [ " foo " => 123 , " bar " => 456 ];
2022-04-28 16:05:33 +02:00
rsort ( $array );
$emptyArray = [];
rsort ( $emptyArray ); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-04-28 16:05:33 +02:00
'$array' => 'non-empty-list<int>' ,
2022-05-28 22:19:49 +02:00
'$emptyArray' => 'list<never>' ,
2020-01-31 19:58:02 +01:00
],
],
'usort' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-04-28 17:52:37 +02:00
function baz ( int $a , int $b ) : int { return $a <=> $b ; }
2020-01-31 19:58:02 +01:00
$array = [ " foo " => 123 , " bar " => 456 ];
2022-04-28 17:52:37 +02:00
usort ( $array , " baz " );
$emptyArray = [];
usort ( $emptyArray , " baz " ); ' ,
2020-01-31 19:58:02 +01:00
'assertions' => [
2022-04-28 17:52:37 +02:00
'$array' => 'non-empty-list<int>' ,
2022-05-28 22:19:49 +02:00
'$emptyArray' => 'list<never>' ,
2020-01-31 19:58:02 +01:00
],
],
2020-08-16 19:03:30 +02:00
'closureParamConstraintsMet' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-16 19:03:30 +02:00
class A {}
class B {}
$test = [ new A (), new B ()];
usort (
$test ,
/**
* @ param A | B $a
* @ param A | B $b
*/
function ( $a , $b ) : int
{
return $a === $b ? 1 : - 1 ;
}
2022-12-18 17:15:15 +01:00
); ' ,
2020-08-16 19:03:30 +02:00
],
2020-01-31 19:58:02 +01:00
'specialCaseArrayFilterOnSingleEntry' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @psalm-return list<int> */
function makeAList ( int $ofThisInteger ) : array {
return array_filter ([ $ofThisInteger ]);
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-31 19:58:02 +01:00
],
'arrayMapWithEmptyArrayReturn' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
2022-12-18 15:00:15 +01:00
* @ param array < int , array < string >> $elements
2020-01-31 19:58:02 +01:00
* @ return list < string >
*/
function resolvePossibleFilePaths ( $elements ) : array
{
return array_values (
array_filter (
array_merge (
... array_map (
function ( array $element ) : array {
if ( rand ( 0 , 1 ) == 1 ) {
return [];
}
return $element ;
},
$elements
)
)
)
);
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-31 19:58:02 +01:00
],
'arrayFilterArrowFunction' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
class A {}
class B {}
$a = \array_filter (
[ new A (), new B ()],
function ( $x ) {
return $x instanceof B ;
}
);
$b = \array_filter (
[ new A (), new B ()],
fn ( $x ) => $x instanceof B
); ' ,
'assertions' => [
2022-12-13 21:40:19 +01:00
// TODO: improve key type
'$a' => 'array<int<0, 1>, B>' ,
'$b' => 'array<int<0, 1>, B>' ,
2020-01-31 19:58:02 +01:00
],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [],
'php_version' => '7.4' ,
2020-01-31 19:58:02 +01:00
],
'arrayMergeTwoExplicitLists' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/**
* @ param list < int > $foo
*/
function foo ( array $foo ) : void {}
$foo1 = [ 1 , 2 , 3 ];
$foo2 = [ 1 , 4 , 5 ];
2022-12-18 17:15:15 +01:00
foo ( array_merge ( $foo1 , $foo2 )); ' ,
2020-01-31 19:58:02 +01:00
],
'arrayMergeTwoPossiblyFalse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = array_merge (
glob ( __DIR__ . \ ' / stubs /*. php\ ' ),
glob ( __DIR__ . \ ' / stubs / DBAL /*. php\ ' ),
); ' ,
2022-01-13 19:49:37 +01:00
'assertions' => [
2022-12-18 17:15:15 +01:00
'$a' => 'list<string>' ,
2020-01-31 19:58:02 +01:00
],
],
2021-10-15 12:06:19 +02:00
'arrayReplaceTwoExplicitLists' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
* @ param list < int > $foo
*/
function foo ( array $foo ) : void {}
$foo1 = [ 1 , 2 , 3 ];
$foo2 = [ 1 , 4 , 5 ];
2022-12-18 17:15:15 +01:00
foo ( array_replace ( $foo1 , $foo2 )); ' ,
2021-10-15 12:06:19 +02:00
],
'arrayReplaceTwoPossiblyFalse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
$a = array_replace (
glob ( __DIR__ . \ ' / stubs /*. php\ ' ),
glob ( __DIR__ . \ ' / stubs / DBAL /*. php\ ' ),
); ' ,
2022-01-13 19:49:37 +01:00
'assertions' => [
2022-12-18 17:15:15 +01:00
'$a' => 'list<string>' ,
2021-10-15 12:06:19 +02:00
],
],
2020-01-31 19:58:56 +01:00
'arrayMapPossiblyFalseIgnored' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:56 +01:00
function takesString ( string $string ) : void {}
$date = new DateTime ();
$a = [ $date -> format ( " Y-m-d " )];
takesString ( $a [ 0 ]);
array_map ( " takesString " , $a ); ' ,
],
2022-11-05 22:34:42 +01:00
'arrayMapZip' => [
'code' => ' < ? php
$a = [ 1 , 2 , 3 , 4 , 5 ];
$b = [ " one " , " two " , " three " , " four " , " five " ];
$c = [ " uno " , " dos " , " tres " , " cuatro " , " cinco " , " seis " ];
$d = array_map ( null , $a , $b , $c ); ' ,
'assertions' => [
2022-12-18 17:15:15 +01:00
'$d===' => " list { list { 1, 'one', 'uno'}, list { 2, 'two', 'dos'}, list { 3, 'three', 'tres'}, list { 4, 'four', 'cuatro'}, list { 5, 'five', 'cinco'}, list { null, null, 'seis'}} " ,
2022-11-05 22:34:42 +01:00
],
'ignored_issues' => [],
'php_version' => '7.4' ,
],
2022-12-01 20:14:28 +01:00
'arrayMapMoreZip' => [
'code' => ' < ? php
$a = array_map ( null , []);
$b = array_map ( null , [ 1 ]);
$c = array_map ( null , [ " test " => 1 ]);
$d = array_map ( null , [], []);
' ,
'assertions' => [
'$a===' => 'array<never, never>' ,
'$b===' => 'list{1}' ,
'$c===' => 'array{test: 1}' ,
'$d===' => 'array<never, never>' ,
],
'ignored_issues' => [],
2022-11-05 22:34:42 +01:00
'php_version' => '7.4' ,
],
2020-02-08 18:17:57 +01:00
'arrayMapExplicitZip' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-02-08 18:17:57 +01:00
$as = [ " key " ];
$bs = [ " value " ];
2020-08-23 16:32:07 +02:00
return array_map ( fn ( $a , $b ) => [ $a => $b ], $as , $bs ); ' ,
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [],
'php_version' => '7.4' ,
2020-02-08 18:17:57 +01:00
],
2020-03-09 06:15:52 +01:00
'spliceTurnsintKeyedInputToList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-03-09 06:15:52 +01:00
/**
* @ psalm - param list < string > $elements
* @ return list < string >
*/
function bar ( array $elements , int $index , string $element ) : array {
array_splice ( $elements , $index , 0 , [ $element ]);
return $elements ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-03-09 06:15:52 +01:00
],
2020-04-28 04:49:07 +02:00
'arrayChangeKeyCaseWithNonStringKeys' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-04-28 04:49:07 +02:00
$a = [ 42 , " A " => 42 ];
2022-12-18 17:15:15 +01:00
echo array_change_key_case ( $a , CASE_LOWER )[ 0 ]; ' ,
2020-04-28 04:49:07 +02:00
],
2020-05-08 20:52:53 +02:00
'mapInterfaceMethod' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-05-08 20:52:53 +02:00
interface MapperInterface {
public function map ( string $s ) : int ;
}
/**
* @ param list < string > $strings
* @ return list < int >
*/
function mapList ( MapperInterface $m , array $strings ) : array {
return array_map ([ $m , " map " ], $strings );
2022-12-18 17:15:15 +01:00
} ' ,
2020-05-08 20:52:53 +02:00
],
2020-08-05 18:43:27 +02:00
'arrayShiftComplexArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-05 18:43:27 +02:00
/**
* @ param list < string > $slugParts
*/
function foo ( array $slugParts ) : void {
if ( ! $slugParts ) {
$slugParts = [ " " ];
}
array_shift ( $slugParts );
if ( ! empty ( $slugParts )) {}
2022-12-18 17:15:15 +01:00
} ' ,
2020-08-05 18:43:27 +02:00
],
2020-09-14 19:06:15 +02:00
'arrayMergeKeepLastKeysAndType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-14 19:06:15 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { A : int } $a
2020-09-14 19:06:15 +02:00
* @ param array < string , string > $b
*
2022-11-12 02:14:21 +01:00
* @ return array { A : int , ... }
2020-09-14 19:06:15 +02:00
*/
function merger ( array $a , array $b ) : array {
return array_merge ( $b , $a );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-14 19:06:15 +02:00
],
'arrayMergeKeepFirstKeysSameType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-14 19:06:15 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { A : int } $a
2020-09-14 19:06:15 +02:00
* @ param array < string , int > $b
*
2022-11-12 02:14:21 +01:00
* @ return array { A : int , ... }
2020-09-14 19:06:15 +02:00
*/
function merger ( array $a , array $b ) : array {
return array_merge ( $a , $b );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-14 19:06:15 +02:00
],
2021-10-15 12:06:19 +02:00
'arrayReplaceKeepLastKeysAndType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { A : int } $a
2021-10-15 12:06:19 +02:00
* @ param array < string , string > $b
*
2022-11-12 02:14:21 +01:00
* @ return array { A : int , ... }
2021-10-15 12:06:19 +02:00
*/
function merger ( array $a , array $b ) : array {
return array_replace ( $b , $a );
2022-12-18 17:15:15 +01:00
} ' ,
2021-10-15 12:06:19 +02:00
],
'arrayReplaceKeepFirstKeysSameType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { A : int } $a
2021-10-15 12:06:19 +02:00
* @ param array < string , int > $b
*
2022-11-12 02:14:21 +01:00
* @ return array { A : int , ... }
2021-10-15 12:06:19 +02:00
*/
function merger ( array $a , array $b ) : array {
return array_replace ( $a , $b );
2022-12-18 17:15:15 +01:00
} ' ,
2021-10-15 12:06:19 +02:00
],
2020-09-19 21:46:54 +02:00
'filteredArrayCanBeEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-19 21:46:54 +02:00
/**
* @ return string | null
*/
function thing () {
if ( rand ( 0 , 1 ) === 1 ) {
return " data " ;
} else {
return null ;
}
}
$list = [ thing (), thing (), thing ()];
$list = array_filter ( $list );
2022-12-18 17:15:15 +01:00
if ( ! empty ( $list )) {} ' ,
2020-09-19 21:46:54 +02:00
],
2020-10-12 21:16:47 +02:00
'arrayShiftOnMixedOrEmptyArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-10-12 21:16:47 +02:00
/**
2021-10-13 19:37:47 +02:00
* @ param mixed | array < never , never > $lengths
2020-10-12 21:16:47 +02:00
*/
function doStuff ( $lengths ) : void {
/** @psalm-suppress MixedArgument, MixedAssignment */
$length = array_shift ( $lengths );
if ( $length !== null ) {}
2022-12-18 17:15:15 +01:00
} ' ,
2020-10-12 21:16:47 +02:00
],
2020-10-28 18:32:55 +01:00
'countOnListIntoTuple' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2022-11-12 02:14:21 +01:00
/** @param array{string, string} $tuple */
2020-10-28 18:32:55 +01:00
function foo ( array $tuple ) : void {}
/** @param list<string> $list */
function bar ( array $list ) : void {
if ( count ( $list ) === 2 ) {
foo ( $list );
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-10-28 18:32:55 +01:00
],
2020-11-27 23:05:54 +01:00
'arrayColumnwithKeyedArrayWithoutRedundantUnion' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-11-27 23:05:54 +01:00
/**
2022-11-12 02:14:21 +01:00
* @ param array < string , array { x ? : int , y ? : int , width ? : int , height ? : int } > $foos
2020-11-27 23:05:54 +01:00
*/
function foo ( array $foos ) : void {
array_multisort ( $formLayoutFields , SORT_ASC , array_column ( $foos , " y " ));
2022-12-18 17:15:15 +01:00
} ' ,
2020-11-27 23:05:54 +01:00
],
2021-02-08 15:29:41 +01:00
'arrayMapGenericObject' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-02-08 15:29:41 +01:00
/**
* @ template T
*/
interface Container
{
/**
* @ return T
*/
public function get ( string $name );
}
/**
* @ param Container < stdClass > $container
* @ param array < string > $data
* @ return array < stdClass >
*/
function bar ( Container $container , array $data ) : array {
return array_map (
[ $container , " get " ],
$data
);
2022-12-18 17:15:15 +01:00
} ' ,
2021-02-08 15:29:41 +01:00
],
2021-07-03 05:24:18 +02:00
'arrayMapShapeAndGenericArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-07-03 05:24:18 +02:00
/** @return string[] */
function getLine () : array { return [ " a " , " b " ]; }
$line = getLine ();
2022-11-12 02:14:21 +01:00
if ( empty ( $line [ 0 ])) { // converts array<string> to array{0:string}<string>
2021-07-03 05:24:18 +02:00
throw new InvalidArgumentException ;
}
$line = array_map ( // should not destroy <string> part
function ( $val ) { return ( int ) $val ; },
$line
);
' ,
'assertions' => [
2022-11-12 02:14:21 +01:00
'$line===' => 'array{0: int, ...<array-key, int>}' ,
2021-07-03 05:24:18 +02:00
],
],
2021-10-07 11:04:51 +02:00
'arrayUnshiftOnEmptyArrayMeansNonEmptyList' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-07 11:04:51 +02:00
/**
* @ return non - empty - list < string >
*/
function foo () : array
{
$a = [];
array_unshift ( $a , " string " );
return $a ;
} ' ,
],
2022-01-09 15:53:43 +01:00
'keepClassStringInOffsetThroughArrayMerge' => [
2022-01-14 21:13:34 +01:00
'code' => ' < ? php
2022-01-09 15:48:58 +01:00
class A {
/** @var array<class-string, string> */
private array $a ;
public function __construct () {
$this -> a = [];
}
public function handle () : void {
$b = [ A :: class => " d " ];
$this -> a = array_merge ( $this -> a , $b );
}
}
' ,
],
2022-11-30 23:45:55 +01:00
'mergeBetweenSealedArrayWithPossiblyUndefinedAndMixedArrayIsMixedArray' => [
'code' => ' < ? php
function findit ( Closure $x ) : void
{
$closure = new ReflectionFunction ( $x );
$statics = [];
if ( rand ( 0 , 1 )) {
$statics = [ " this " => " a " ];
}
$b = $statics + $closure -> getStaticVariables ();
/** @psalm-check-type $b = array<array-key, mixed> */
$_a = count ( $b );
/** @psalm-check-type $_a = int<0, max> */
}
' ,
],
Better type inference and type checking for large union types used in array keys/values
Fixes #8983
This patch adds a basic test showing that, when reaching a union type with 30 elements
or more, Psalm used to fail with an error, because the large union type got simplified
into a more general type as part of performance optimizations done in `TypeCombiner::combine()`.
This means that a type like `array<1|2|3|(etcetera...)|100, mixed>` was internally
simplified to `array<int, mixed>`, after reaching 30 elements or more, which in turn
led to problems and confusing errors when large union types are in play.
Such union types are relatively common in lookup-table-alike value objects.
By removing the hardcoded call-time limit of `30` types to be combined, we hereby
rely on the default `TypeCombiner::combine()` limit of `500` items, which is more
healthy.
This may come with some performance implications, but it is worth trying out, for
now.
Further parameters passed to `TypeCombiner::combine()` that were already matching
parameter default values were also omitted from the call-sites.
2022-12-29 10:05:23 +01:00
'functionRequiringArrayWithLargeUnionTypeKeyAllowsInputArrayUsingSameUnionForItsKeys' => [
'code' => ' < ? php
/** @psalm-type TLargeUnion = 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31 */
/** @return TLargeUnion */
function makeKey () : int { throw new Exception ( " irrelevant " ); }
/** @param array<TLargeUnion, mixed> $_input */
function consumeArray ( array $_input ) : void {}
consumeArray ([ makeKey () => null ]);
' ,
],
2023-02-20 11:46:14 +01:00
'arrayUniquePreservesNonEmptyInput' => [
'code' => ' < ? php
/** @param non-empty-array<string, object> $input */
function takes_non_empty_array ( array $input ) : void {}
takes_non_empty_array ( array_unique ([ " test " => ( object )[]]));
/** @param non-empty-array<int, object> $input */
function takes_non_empty_int_array ( array $input ) : void {}
takes_non_empty_int_array ( array_unique ([( object )[]]));
' ,
],
2023-04-13 22:19:44 +02:00
'arrayFlipPreservesNonEmptyInput' => [
'code' => ' < ? php
/** @param non-empty-array<string, int> $input */
function takes_non_empty_array ( array $input ) : void {}
$array = [ " hi " , " there " ];
$flipped = array_flip ( $array );
takes_non_empty_array ( $flipped );
' ,
],
2020-01-31 19:58:02 +01:00
];
}
2020-09-12 17:24:05 +02:00
public function providerInvalidCodeParse () : iterable
2020-01-31 19:58:02 +01:00
{
return [
'arrayFilterWithoutTypes' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$e = array_filter (
[ " a " => 5 , " b " => 12 , " c " => null ],
function ( ? int $i ) {
2022-09-10 13:06:17 +02:00
return $GLOBALS [ " a " ];
2020-01-31 19:58:02 +01:00
}
); ' ,
'error_message' => 'MixedArgumentTypeCoercion' ,
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MissingClosureParamType' , 'MissingClosureReturnType' ],
2020-01-31 19:58:02 +01:00
],
'arrayFilterUseMethodOnInferrableInt' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = array_filter ([ 1 , 2 , 3 , 4 ], function ( $i ) { return $i -> foo (); }); ' ,
'error_message' => 'InvalidMethodCall' ,
],
'arrayMapUseMethodOnInferrableInt' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = array_map ( function ( $i ) { return $i -> foo (); }, [ 1 , 2 , 3 , 4 ]); ' ,
'error_message' => 'InvalidMethodCall' ,
],
'arrayMapWithNonCallableStringArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$foo = [ " one " , " two " ];
array_map ( $foo , [ " hello " ]); ' ,
'error_message' => 'InvalidArgument' ,
],
'arrayMapWithNonCallableIntArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$foo = [ 1 , 2 ];
array_map ( $foo , [ " hello " ]); ' ,
'error_message' => 'InvalidArgument' ,
],
'arrayFilterBadArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function foo ( int $i ) : bool {
return true ;
}
array_filter ([ " hello " ], " foo " ); ' ,
'error_message' => 'InvalidScalarArgument' ,
],
2021-05-15 02:12:39 +02:00
'arrayFillPositiveConstantLength' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-05-15 02:12:39 +02:00
count ( array_fill ( 0 , 1 , 0 )) === 0 ; ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'TypeDoesNotContainType' ,
2021-05-15 02:12:39 +02:00
],
2020-01-31 19:58:02 +01:00
'arrayFilterTooFewArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function foo ( int $i , string $s ) : bool {
return true ;
}
array_filter ([ 1 , 2 , 3 ], " foo " ); ' ,
'error_message' => 'TooFewArguments' ,
],
'arrayMapBadArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function foo ( int $i ) : bool {
return true ;
}
array_map ( " foo " , [ " hello " ]); ' ,
'error_message' => 'InvalidScalarArgument' ,
],
'arrayMapTooFewArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function foo ( int $i , string $s ) : bool {
return true ;
}
array_map ( " foo " , [ 1 , 2 , 3 ]); ' ,
'error_message' => 'TooFewArguments' ,
],
'arrayMapTooManyArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function foo () : bool {
return true ;
}
array_map ( " foo " , [ 1 , 2 , 3 ]); ' ,
'error_message' => 'TooManyArguments' ,
],
'arrayReduceInvalidClosureTooFewArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$arr = [ 2 , 3 , 4 , 5 ];
$direct_closure_result = array_reduce (
$arr ,
2021-01-06 18:59:51 +01:00
function () : int {
2020-01-31 19:58:02 +01:00
return 5 ;
},
1
); ' ,
'error_message' => 'InvalidArgument' ,
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MixedTypeCoercion' ],
2020-01-31 19:58:02 +01:00
],
'arrayReduceInvalidItemType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$arr = [ 2 , 3 , 4 , 5 ];
$direct_closure_result = array_reduce (
$arr ,
function ( int $carry , stdClass $item ) {
return $_GET [ " boo " ];
},
1
); ' ,
'error_message' => 'InvalidArgument' ,
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MissingClosureReturnType' ],
2020-01-31 19:58:02 +01:00
],
'arrayReduceInvalidCarryType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$arr = [ 2 , 3 , 4 , 5 ];
$direct_closure_result = array_reduce (
$arr ,
function ( stdClass $carry , int $item ) {
return $_GET [ " boo " ];
},
1
); ' ,
'error_message' => 'InvalidArgument' ,
2022-01-13 19:49:37 +01:00
'ignored_issues' => [ 'MissingClosureReturnType' ],
2020-01-31 19:58:02 +01:00
],
'arrayReduceInvalidCarryOutputType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$arr = [ 2 , 3 , 4 , 5 ];
$direct_closure_result = array_reduce (
$arr ,
function ( int $carry , int $item ) : stdClass {
return new stdClass ;
},
1
); ' ,
'error_message' => 'InvalidArgument' ,
],
'arrayPopNotNull' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
function expectsInt ( int $a ) : void {}
/**
2022-11-12 02:14:21 +01:00
* @ param array < array - key , array { item : int } > $list
2020-01-31 19:58:02 +01:00
*/
function test ( array $list ) : void
{
while ( ! empty ( $list )) {
$tmp = array_pop ( $list );
if ( $tmp === null ) {}
}
} ' ,
'error_message' => 'DocblockTypeContradiction' ,
],
'usortInvalidCallableString' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
$a = [[ 1 ], [ 2 ], [ 3 ]];
usort ( $a , " strcmp " ); ' ,
'error_message' => 'InvalidArgument' ,
],
'arrayShiftUndefinedVariable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @psalm-suppress MissingParamType */
function foo ( $data ) : void {
/** @psalm-suppress MixedArgument */
array_unshift ( $data , $a );
} ' ,
'error_message' => 'UndefinedVariable' ,
],
2020-08-30 17:44:14 +02:00
'arrayFilterTKeyedArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-31 19:58:02 +01:00
/** @param list<int> $ints */
function ints ( array $ints ) : void {}
$brr = array_filter ([ 2 , 3 , 0 , 4 , 5 ]);
ints ( $brr ); ' ,
2022-12-13 21:40:19 +01:00
'error_message' => 'ArgumentTypeCoercion' ,
2020-01-31 19:58:02 +01:00
],
2020-04-29 20:57:57 +02:00
'usortOneParamInvalid' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-04-29 20:57:57 +02:00
$list = [ 3 , 2 , 5 , 9 ];
usort ( $list , fn ( int $a , string $b ) : int => ( int ) ( $a > $b )); ' ,
2020-08-23 16:32:07 +02:00
'error_message' => 'InvalidScalarArgument' ,
2022-01-13 19:49:37 +01:00
'ignored_issues' => [],
'php_version' => '7.4' ,
2020-04-29 20:57:57 +02:00
],
2020-08-16 19:03:30 +02:00
'usortInvalidComparison' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-08-16 19:03:30 +02:00
$arr = [[ " one " ], [ " two " ], [ " three " ]];
usort (
$arr ,
function ( string $a , string $b ) : int {
return strcmp ( $a , $b );
}
); ' ,
'error_message' => 'InvalidArgument' ,
],
2020-09-14 19:06:15 +02:00
'arrayMergeKeepFirstKeysButNotType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-14 19:06:15 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { A : int } $a
2020-09-14 19:06:15 +02:00
* @ param array < string , string > $b
*
2022-11-12 02:14:21 +01:00
* @ return array { A : int , ... }
2020-09-14 19:06:15 +02:00
*/
function merger ( array $a , array $b ) : array {
return array_merge ( $a , $b );
} ' ,
2022-11-12 02:14:21 +01:00
'error_message' => 'LessSpecificReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:32 - The type \'array{A: int|string, ...<string, string>}\' is more general' ,
2020-09-14 19:06:15 +02:00
],
2021-10-15 12:06:19 +02:00
'arrayReplaceKeepFirstKeysButNotType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-10-15 12:06:19 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { A : int } $a
2021-10-15 12:06:19 +02:00
* @ param array < string , string > $b
*
2022-11-12 02:14:21 +01:00
* @ return array { A : int , ... }
2021-10-15 12:06:19 +02:00
*/
function merger ( array $a , array $b ) : array {
return array_replace ( $a , $b );
} ' ,
2022-11-12 02:14:21 +01:00
'error_message' => 'LessSpecificReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:32 - The type \'array{A: int|string, ...<string, string>}\' is more general' ,
2021-10-15 12:06:19 +02:00
],
2021-08-20 00:43:49 +02:00
'arrayWalkOverObject' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-08-20 00:43:49 +02:00
$o = new stdClass ();
array_walk ( $o , " var_dump " );
' ,
'error_message' => 'RawObjectIteration' ,
],
2021-08-31 22:18:20 +02:00
'arrayWalkRecursiveOverObject' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-08-31 22:18:20 +02:00
$o = new stdClass ();
array_walk_recursive ( $o , " var_dump " );
' ,
'error_message' => 'RawObjectIteration' ,
],
2022-11-09 08:49:27 +01:00
'implodeWithNonStringableArgs' => [
'code' => ' < ? php
implode ( " , " , [ new stdClass ]);
' ,
'error_message' => 'InvalidArgument' ,
],
2022-12-13 22:29:35 +01:00
'arrayCombineNotMatching' => [
'code' => ' < ? php
array_combine ([ " a " , " b " ], [ 1 , 2 , 3 ]); ' ,
'error_message' => 'InvalidArgument' ,
],
'arrayCombineNotMatchingPHP8' => [
'code' => ' < ? php
array_combine ([ " a " , " b " ], [ 1 , 2 , 3 ]); ' ,
'error_message' => 'InvalidArgument' ,
],
2022-12-18 14:50:51 +01:00
'arrayMergeNoNamed' => [
'code' => ' < ? php
$map = [ " a " => []];
array_merge ( ... $map );
' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'NamedArgumentNotAllowed' ,
2022-12-18 14:52:44 +01:00
],
'arrayMergeRecursiveNoNamed' => [
'code' => ' < ? php
$map = [ " a " => []];
array_merge_recursive ( ... $map );
' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'NamedArgumentNotAllowed' ,
],
2023-02-20 11:46:14 +01:00
'arrayUniquePreservesEmptyInput' => [
'code' => ' < ? php
/** @param non-empty-array<string, object> $input */
function takes_non_empty_array ( array $input ) : void {}
takes_non_empty_array ( array_unique ([]));
' ,
'error_message' => 'InvalidArgument' ,
],
'arrayUniqueConvertsListToArray' => [
'code' => ' < ? php
/** @param non-empty-list<object> $input */
function takes_non_empty_list ( array $input ) : void {}
takes_non_empty_list ( array_unique ([( object )[]]));
' ,
'error_message' => 'ArgumentTypeCoercion' ,
],
2023-04-13 22:19:44 +02:00
'arrayFlipPreservesEmptyInput' => [
'code' => ' < ? php
/** @param non-empty-array<string, int> $input */
function takes_non_empty_array ( array $input ) : void {}
takes_non_empty_array ( array_flip ([]));
' ,
'error_message' => 'InvalidArgument' ,
],
2020-01-31 19:58:02 +01:00
];
}
}