2018-06-17 02:01:33 +02:00
< ? php
2021-12-15 04:58:32 +01:00
2018-06-17 02:01:33 +02:00
namespace Psalm\Tests ;
2023-04-10 00:53:33 +02:00
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait ;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait ;
2021-06-08 04:55:21 +02:00
2021-03-17 06:10:42 +01:00
use const DIRECTORY_SEPARATOR ;
2018-06-17 02:01:33 +02:00
class UnusedVariableTest extends TestCase
{
2023-04-10 00:53:33 +02:00
use ValidCodeAnalysisTestTrait ;
use InvalidCodeAnalysisTestTrait ;
2018-06-17 02:01:33 +02:00
2021-12-05 18:51:26 +01:00
public function setUp () : void
2018-06-17 02:01:33 +02:00
{
2023-04-10 00:53:33 +02:00
parent :: setUp ();
2019-03-05 21:45:09 +01:00
$this -> project_analyzer -> getCodebase () -> reportUnusedVariables ();
2018-06-17 02:01:33 +02:00
}
2020-09-12 17:24:05 +02:00
public function providerValidCodeParse () : array
2018-06-17 02:01:33 +02:00
{
return [
'arrayOffset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
$arr = [ " hello " ];
echo $arr [ $a ]; ' ,
],
'unset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
$arr = [ " hello " ];
unset ( $arr [ $a ]); ' ,
],
2023-03-15 10:06:36 +01:00
'eval' => [
'code' => ' < ? php
if ( rand ()) {
$v = " " ;
eval ( $v );
} ' ,
],
2018-06-17 02:01:33 +02:00
'usedVariables' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
/** @return string */
function foo () {
$a = 5 ;
$b = [];
$c [] = " hello " ;
2019-01-02 12:58:49 +01:00
class Foo {
public function __construct ( string $_i ) {}
}
2018-06-17 02:01:33 +02:00
$d = " Foo " ;
$e = " arg " ;
$f = new $d ( $e );
return $a . implode ( " , " , $b ) . $c [ 0 ] . get_class ( $f );
} ' ,
2023-04-10 00:53:33 +02:00
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [
2018-06-17 02:01:33 +02:00
'PossiblyUndefinedVariable' ,
'MixedArrayAccess' ,
'MixedOperand' ,
'MixedAssignment' ,
2018-11-12 18:03:55 +01:00
'InvalidStringClass' ,
2018-06-17 02:01:33 +02:00
],
],
'varDefinedInIfWithReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
if ( rand ( 0 , 1 )) {
$b = " hello " ;
} else {
$b = " goodbye " ;
}
echo $a . $b ; ' ,
],
'varRedefinedInIfWithReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-11-25 18:04:48 +01:00
$a = ( string ) 5 ;
2018-06-17 02:01:33 +02:00
if ( rand ( 0 , 1 )) {
2020-11-25 18:04:48 +01:00
$a = ( string ) 6 ;
2018-06-17 02:01:33 +02:00
}
echo $a ; ' ,
],
'byrefInForeachLoopWithReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = [ 1 , 2 , 3 ];
foreach ( $a as & $b ) {
$b = $b + 1 ;
}
echo $a [ 0 ]; ' ,
],
2018-06-17 03:14:19 +02:00
'foreachVarSetInValue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 03:14:19 +02:00
/** @param string[] $arr */
function foo ( array $arr ) : void {
$a = null ;
foreach ( $arr as $a ) { }
2024-01-12 23:37:45 +01:00
if ( $a !== null ) {}
2018-06-17 03:14:19 +02:00
} ' ,
],
2018-06-17 02:01:33 +02:00
'definedInSecondBranchOfCondition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
if ( rand ( 0 , 1 ) && $a = rand ( 0 , 1 )) {
echo $a ;
} ' ,
],
'booleanOr' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function foo ( int $a , int $b ) : bool {
return $a || $b ;
} ' ,
],
'paramUsedInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function foo ( string $a ) : void {
if ( rand ( 0 , 1 )) {
echo $a ;
}
} ' ,
],
'dummyByRefVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function foo ( string & $a = null , string $b = null ) : void {
2024-01-12 23:37:45 +01:00
if ( $a !== null ) {
2018-06-17 02:01:33 +02:00
echo $a ;
}
2024-01-12 23:37:45 +01:00
if ( $b !== null ) {
2018-06-17 02:01:33 +02:00
echo $b ;
}
}
function bar () : void {
foo ( $dummy_byref_var , " hello " );
}
bar (); ' ,
],
'foreachReassigned' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/**
* @ param list < int > $arr
*/
function foo ( array $arr ) : void {
$a = false ;
2018-06-17 02:01:33 +02:00
2020-09-25 06:14:27 +02:00
foreach ( $arr as $b ) {
$a = true ;
echo $b ;
}
2018-06-17 02:01:33 +02:00
2020-09-25 06:14:27 +02:00
echo $a ;
} ' ,
2018-06-17 02:01:33 +02:00
],
'doWhileReassigned' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
do {
echo $a ;
$a = $a - rand ( - 3 , 3 );
} while ( $a > 3 ); ' ,
],
'loopTypeChangedInIfAndContinueWithReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = true ;
continue ;
}
$a = false ;
}
echo $a ; ' ,
],
'loopReassignedInIfAndContinueWithReferenceAfter' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = 7 ;
continue ;
}
$a = 3 ;
}
echo $a ; ' ,
],
'loopReassignedInIfAndContinueWithReferenceBeforeAndAfter' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
2018-11-11 01:05:51 +01:00
echo $a ;
2018-06-17 02:01:33 +02:00
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = 7 ;
continue ;
}
$a = 3 ;
}
echo $a ; ' ,
],
'loopReassigned' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
while ( rand ( 0 , 1 )) {
$a = true ;
}
echo $a ; ' ,
],
'ifVarReassignedInBranchWithUse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = true ;
if ( rand ( 0 , 1 )) {
$a = false ;
}
if ( $a ) {
echo " cool " ;
} ' ,
],
'elseVarReassignedInBranchAndReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
if ( rand ( 0 , 1 )) {
// do nothing
} else {
$a = true ;
//echo $a;
}
if ( $a ) {
echo " cool " ;
} ' ,
],
'switchVarReassignedInBranch' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
switch ( rand ( 0 , 2 )) {
case 0 :
$a = true ;
}
if ( $a ) {
echo " cool " ;
} ' ,
],
'switchVarDefinedInAllBranches' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
switch ( rand ( 0 , 2 )) {
case 0 :
$a = true ;
break ;
default :
$a = false ;
}
if ( $a ) {
echo " cool " ;
} ' ,
],
2018-11-11 01:05:51 +01:00
'switchVarConditionalAssignmentWithReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 03:54:44 +02:00
switch ( rand ( 0 , 4 )) {
case 0 :
if ( rand ( 0 , 1 )) {
$a = 0 ;
break ;
}
default :
$a = 1 ;
}
2019-03-23 19:27:54 +01:00
echo $a ; ' ,
2018-06-17 03:54:44 +02:00
],
2018-06-17 02:01:33 +02:00
'throwWithMessageCall' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : void {
throw new \Exception ( " bad " );
}
function callDangerous () : void {
try {
dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
}
} ' ,
],
'throwWithMessageCallAndAssignmentAndReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
}
function callDangerous () : void {
$s = null ;
try {
$s = dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
}
2024-01-12 23:37:45 +01:00
if ( $s !== null ) {}
2018-06-17 02:01:33 +02:00
} ' ,
],
'throwWithMessageCallAndAssignmentInCatchAndReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
}
function callDangerous () : void {
$s = null ;
try {
dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
$s = " hello " ;
}
if ( $s ) {}
} ' ,
],
'throwWithMessageCallAndAssignmentInTryAndCatchAndReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
}
function callDangerous () : void {
try {
$s = dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
$s = " hello " ;
}
if ( $s ) {}
} ' ,
],
'throwWithMessageCallAndNestedAssignmentInTryAndCatchAndReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
}
function callDangerous () : void {
$s = null ;
if ( rand ( 0 , 1 )) {
$s = " hello " ;
} else {
try {
$t = dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
$t = " hello " ;
}
if ( $t ) {
$s = $t ;
}
}
if ( $s ) {}
} ' ,
],
2018-06-17 03:14:19 +02:00
'throwWithReturnInOneCatch' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 03:14:19 +02:00
class E1 extends Exception {}
function dangerous () : void {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
}
function callDangerous () : void {
try {
dangerous ();
$s = true ;
} catch ( E1 $e ) {
echo $e -> getMessage ();
$s = false ;
} catch ( Exception $e ) {
return ;
}
if ( $s ) {}
} ' ,
],
2018-06-17 02:01:33 +02:00
'loopWithIfRedefinition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$i = false ;
foreach ([ 1 , 2 , 3 ] as $a ) {
if ( rand ( 0 , 1 )) {
$i = true ;
}
echo $a ;
}
if ( $i ) {} ' ,
],
2020-03-06 03:10:29 +01:00
'unknownMethodCallWithVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-03-06 03:10:29 +01:00
/** @psalm-suppress MixedMethodCall */
function passesByRef ( object $a ) : void {
2020-03-06 03:24:08 +01:00
/** @psalm-suppress PossiblyUndefinedVariable */
2020-03-06 03:10:29 +01:00
$a -> passedByRef ( $b );
} ' ,
],
2018-06-17 02:01:33 +02:00
'usedMethodCallVariable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function reindex ( array $arr , string $methodName ) : array {
$ret = [];
foreach ( $arr as $element ) {
$ret [ $element -> $methodName ()] = true ;
}
return $ret ;
} ' ,
2023-04-10 00:53:33 +02:00
'assertions' => [],
2022-01-13 19:49:37 +01:00
'ignored_issues' => [
2018-06-17 02:01:33 +02:00
'MixedAssignment' ,
'MixedMethodCall' ,
'MixedArrayOffset' ,
],
],
'globalVariableUsage' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = " hello " ;
function example () : void {
global $a ;
echo $a ;
2020-04-19 19:15:04 +02:00
$a = " hello " ;
2018-06-17 02:01:33 +02:00
}
example (); ' ,
],
'staticVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function use_static () : void {
static $token ;
if ( ! $token ) {
$token = rand ( 1 , 10 );
}
echo " token is $token\n " ;
} ' ,
],
2019-03-20 04:26:46 +01:00
'staticVarUsedLater' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-03-20 04:26:46 +01:00
function use_static () : int {
static $x = null ;
if ( $x ) {
return ( int ) $x ;
}
$x = rand ( 0 , 1 );
return - 1 ;
} ' ,
],
2018-06-17 02:01:33 +02:00
'tryCatchWithUseInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function example_string () : string {
if ( rand ( 0 , 1 ) > 0 ) {
return " value " ;
}
throw new Exception ( " fail " );
}
function main () : void {
try {
$s = example_string ();
if ( ! $s ) {
echo " Failed to get string \n " ;
}
} catch ( Exception $e ) {
$s = " fallback " ;
}
printf ( " s is %s \n " , $s );
} ' ,
],
'loopTypeChangedInIfAndBreakWithReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 05:31:36 +02:00
$a = 1 ;
2018-06-17 02:01:33 +02:00
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
2019-05-13 05:31:36 +02:00
$a = 2 ;
2018-06-17 02:01:33 +02:00
break ;
}
2019-05-13 05:31:36 +02:00
$a = 3 ;
2018-06-17 02:01:33 +02:00
}
echo $a ; ' ,
],
'loopReassignedInIfAndBreakWithReferenceAfter' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = 7 ;
break ;
}
$a = 3 ;
}
echo $a ; ' ,
],
'loopSetIfNullWithBreakAndReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = null ;
while ( rand ( 0 , 1 )) {
if ( $a !== null ) {
$a = 4 ;
break ;
}
$a = 5 ;
}
echo $a ; ' ,
],
'loopSetIfNullWithContinueAndReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = null ;
while ( rand ( 0 , 1 )) {
if ( $a !== null ) {
$a = 4 ;
continue ;
}
$a = 5 ;
}
echo $a ; ' ,
],
'loopAssignmentAfterReferenceSimple' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
echo $a ;
$a = 1 ;
} ' ,
],
'loopAssignmentAfterReferenceWithContinue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
echo $a ;
$a = 1 ;
continue ;
} ' ,
],
'loopAssignmentAfterReferenceWithConditionalAssignmentWithContinue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
echo $a ;
if ( rand ( 0 , 1 )) {
$a = 1 ;
}
continue ;
} ' ,
],
'loopAssignmentAfterReferenceWithContinueInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
echo $a ;
if ( rand ( 0 , 1 )) {
$a = 1 ;
continue ;
}
} ' ,
],
'loopAssignmentAfterReferenceWithContinueInSwitch' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
switch ( rand ( 0 , 1 )) {
case 0 :
$a = 1 ;
break ;
}
}
echo $a ; ' ,
],
2018-11-27 23:45:07 +01:00
'loopAssignmentAfterReferenceWithContinueInSwitch2' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
switch ( rand ( 0 , 1 )) {
case 0 :
$a = 1 ;
break ;
}
}
}
echo $a ; ' ,
],
'listVarAssignmentInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = " a " ;
$b = " b " ;
if ( rand ( 0 , 1 )) {
list ( $a , $b ) = explode ( " . " , " c.d " );
}
echo $a ;
2019-03-23 19:27:54 +01:00
echo $b ; ' ,
2018-06-17 02:01:33 +02:00
],
2020-09-25 06:14:27 +02:00
'arrayVarAssignmentInFunctionAndReturned' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param array { string } $arr
2020-09-25 06:14:27 +02:00
*/
function far ( array $arr ) : string {
[ $a ] = $arr ;
return $a ;
} ' ,
],
'arrayUnpackInForeach' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/**
2022-11-12 02:14:21 +01:00
* @ param list < array { string , string } > $arr
2020-09-25 06:14:27 +02:00
*/
function far ( array $arr ) : void {
foreach ( $arr as [ $a , $b ]) {
echo $a ;
echo $b ;
}
} ' ,
],
2022-11-04 19:04:23 +01:00
'arraySubAppend' => [
'code' => ' < ? php
$rules = [ 0 , 1 , 2 ];
$report = [ " runs " => []];
foreach ( $rules as $rule ) {
$report [ " runs " ][] = $rule ;
}
2022-12-18 17:15:15 +01:00
echo ( count ( $report )); ' ,
2022-11-04 19:04:23 +01:00
],
2020-09-25 06:14:27 +02:00
'arrayAssignmentInFunctionCoerced' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
class A {
public int $a = 0 ;
public int $b = 1 ;
function setPhpVersion ( string $version ) : void {
[ $a , $b ] = explode ( " . " , $version );
$this -> a = ( int ) $a ;
$this -> b = ( int ) $b ;
}
}
2022-12-18 17:15:15 +01:00
' ,
2020-09-25 06:14:27 +02:00
],
2018-06-17 02:01:33 +02:00
'varCheckAfterNestedAssignmentAndBreak' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
if ( rand ( 0 , 1 )) {
while ( rand ( 0 , 1 )) {
$a = true ;
break ;
}
}
if ( $a ) {} ' ,
],
'varCheckAfterNestedAssignmentAndBreakInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
if ( rand ( 0 , 1 )) {
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = true ;
break ;
}
}
}
if ( $a ) {} ' ,
],
'breakInSwitchStatementIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
switch ( rand ( 0 , 1 )) {
default :
echo $a ;
if ( rand ( 0 , 1 )) {
$a = 5 ;
break ;
}
}
} ' ,
],
'breakInSwitchStatementIfWithSecondCase' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
switch ( rand ( 0 , 1 )) {
case 0 :
$a = 1 ;
break ;
default :
echo $a ;
if ( rand ( 0 , 1 )) {
$a = 5 ;
break ;
}
}
} ' ,
],
2018-06-17 05:40:25 +02:00
'echoVarWithAdditionOp' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 05:40:25 +02:00
$a = 5 ;
2020-09-25 06:14:27 +02:00
while ( rand ( 0 , 1 )) {
echo ( $a += 1 );
} ' ,
2018-06-17 05:40:25 +02:00
],
'echoVarWithIncrement' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 05:40:25 +02:00
function foo ( int $i ) : void {
echo $i ;
}
2020-09-25 06:14:27 +02:00
2018-06-17 05:40:25 +02:00
$a = 5 ;
2020-09-25 06:14:27 +02:00
while ( rand ( 0 , 1 )) {
foo ( ++ $a );
} ' ,
2018-06-17 05:40:25 +02:00
],
2018-06-26 01:38:15 +02:00
'afterMethodExistsCheck' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-26 01:38:15 +02:00
class A {
/**
* @ param array < string , string > $options
*/
public function __construct ( array $options ) {
$this -> setOptions ( $options );
}
/**
* @ param array < string , string > $options
*/
protected function setOptions ( array $options ) : void
{
foreach ( $options as $key => $value ) {
$normalized = ucfirst ( $key );
$method = " set " . $normalized ;
if ( method_exists ( $this , $method )) {
$this -> $method ( $value );
}
}
}
}
new A ([ " bar " => " bat " ]); ' ,
],
2018-06-27 16:07:31 +02:00
'instanceofVarUse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-27 16:07:31 +02:00
interface Foo { }
2020-03-15 19:43:05 +01:00
function returnFoo () : Foo {
return new class implements Foo { };
}
2018-06-27 16:07:31 +02:00
$interface = Foo :: class ;
if ( returnFoo () instanceof $interface ) {
exit ;
} ' ,
],
2018-06-30 17:08:51 +02:00
'usedVariableInDoWhile' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-30 17:08:51 +02:00
$i = 5 ;
do {
echo " hello " ;
} while ( -- $i > 0 );
echo $i ; ' ,
],
2018-10-04 22:42:40 +02:00
'callableReferencesItself' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-10-04 22:42:40 +02:00
/** @psalm-suppress UnusedParam */
function foo ( callable $c ) : void {}
$listener = function () use ( & $listener ) : void {
foo ( $listener );
};
foo ( $listener ); ' ,
],
2019-02-24 15:58:11 +01:00
'newVariableConstructor' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-02-24 15:58:11 +01:00
/**
* @ param class - string < ArrayObject > $type
*/
function bar ( string $type ) : ArrayObject {
$data = [[ " foo " ], [ " bar " ]];
2020-08-06 16:18:55 +02:00
/** @psalm-suppress UnsafeInstantiation */
2019-02-24 15:58:11 +01:00
return new $type ( $data [ 0 ]);
} ' ,
],
2019-03-10 21:36:35 +01:00
'byRefVariableUsedInAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-03-10 21:36:35 +01:00
$i = 0 ;
$a = function () use ( & $i ) : void {
$i = 1 ;
};
2020-09-28 00:12:53 +02:00
$a ();
echo $i ; ' ,
2019-03-10 21:36:35 +01:00
],
2020-09-25 06:14:27 +02:00
'regularVariableClosureUseInAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
$i = 0 ;
$a = function () use ( $i ) : int {
return $i + 1 ;
};
$a (); ' ,
],
2019-03-20 04:16:00 +01:00
'superGlobalInFunction' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-03-20 04:16:00 +01:00
function example1 () : void {
$_SESSION = [];
}
function example2 () : int {
return ( int ) $_SESSION [ " str " ];
} ' ,
],
2019-03-28 03:13:06 +01:00
'usedInArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-03-28 03:13:06 +01:00
/**
* @ psalm - suppress MixedMethodCall
* @ psalm - suppress MissingParamType
*/
function foo ( $a ) : void {
2019-05-02 21:33:47 +02:00
$b = " b " ;
$a -> bar ([ $b ]);
} ' ,
],
'paramUsedInsideLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-02 21:33:47 +02:00
function foo ( int $counter ) : void {
foreach ([ 1 , 2 , 3 ] as $_ ) {
echo ( $counter = $counter + 1 );
echo rand ( 0 , 1 ) ? 1 : 0 ;
}
2019-03-28 03:13:06 +01:00
} ' ,
],
2019-05-13 02:49:37 +02:00
'useParamInsideIfLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 02:49:37 +02:00
function foo () : void {
$a = 1 ;
if ( rand ( 0 , 1 )) {
while ( rand ( 0 , 1 )) {
$a = 2 ;
}
}
echo $a ;
} ' ,
],
2019-05-20 17:54:21 +02:00
'useVariableInsideTry' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-20 17:54:21 +02:00
$foo = false ;
try {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
$foo = rand ( 0 , 1 );
if ( $foo ) {}
} catch ( Exception $e ) {}
2024-01-12 23:37:45 +01:00
if ( $foo !== false && $foo !== 0 ) {} ' ,
2019-05-20 17:54:21 +02:00
],
'useTryAssignedVariableInsideFinally' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-20 17:54:21 +02:00
$var = " " ;
try {
if ( rand ( 0 , 1 )) {
throw new \Exception ();
}
$var = " hello " ;
} finally {
if ( $var !== " " ) {
echo $var ;
}
2019-07-05 22:24:00 +02:00
} ' ,
2019-05-20 17:54:21 +02:00
],
2019-05-20 18:01:18 +02:00
'useTryAssignedVariableInFinallyWhenCatchExits' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-20 18:01:18 +02:00
/**
* @ return resource
*/
function getStream () {
throw new \Exception ();
}
$stream = null ;
try {
$stream = getStream ();
\file_put_contents ( " ./foobar " , $stream );
} catch ( \Exception $e ) {
throw new \Exception ( " Something went wrong " );
} finally {
2019-11-12 00:38:33 +01:00
if ( $stream ) {
\fclose ( $stream );
}
2019-05-20 18:01:18 +02:00
} ' ,
],
2019-07-04 23:35:33 +02:00
'varUsedInloop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-07-04 23:35:33 +02:00
class A {
public static function getA () : ? A {
return rand ( 0 , 1 ) ? new A : null ;
}
}
function foo ( ? A $a ) : void {
while ( $a ) {
echo get_class ( $a );
$a = A :: getA ();
}
2019-07-05 22:24:00 +02:00
} ' ,
2019-07-04 23:35:33 +02:00
],
2019-07-24 22:53:14 +02:00
'varPassedByRef' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( array $returned ) : array {
$ancillary = & $returned ;
$ancillary [ " foo " ] = 5 ;
return $returned ;
2019-07-24 22:53:14 +02:00
} ' ,
],
2019-07-25 15:45:11 +02:00
'usedAsMethodName' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-07-25 15:45:11 +02:00
class A {
public static function foo () : void {}
}
function foo () : void {
$method = " foo " ;
A :: $method ();
} ' ,
],
2024-02-01 05:39:38 +01:00
'usedAsClassConstFetch' => [
'code' => ' < ? php
class A {
const bool something = false ;
public function foo () : void {
$var = " something " ;
if ( rand ( 0 , 1 )) {
static :: { $var };
}
}
} ' ,
'assertions' => [],
'ignored_issues' => [],
2024-02-05 09:51:34 +01:00
'php_version' => '8.3' ,
],
'usedAsEnumFetch' => [
'code' => ' < ? php
enum E {
case C ;
}
class A {
public function foo () : void {
$var = " C " ;
if ( rand ( 0 , 1 )) {
E :: { $var };
}
}
} ' ,
'assertions' => [],
'ignored_issues' => [],
2024-02-01 05:39:38 +01:00
'php_version' => '8.3' ,
],
2024-02-01 05:31:15 +01:00
'usedAsStaticPropertyAssign' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-09 18:55:33 +02:00
class A {
private static bool $something = false ;
public function foo () : void {
$var = " something " ;
2020-03-16 03:23:31 +01:00
if ( rand ( 0 , 1 )) {
2019-08-09 18:55:33 +02:00
static :: ${$var} = true ;
}
}
2022-12-18 17:15:15 +01:00
} ' ,
2019-08-09 18:55:33 +02:00
],
2024-02-01 05:31:15 +01:00
'usedAsStaticPropertyFetch' => [
'code' => ' < ? php
class A {
private static bool $something = false ;
public function foo () : void {
$var = " something " ;
if ( rand ( 0 , 1 )) {
static :: ${$var} ;
}
}
} ' ,
],
2019-08-12 22:01:24 +02:00
'setInLoopThatsAlwaysEntered' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-12 22:01:24 +02:00
/**
* @ param non - empty - array < int > $a
*/
function getLastNum ( array $a ) : int {
foreach ( $a as $num ) {
$last = $num ;
}
return $last ;
2022-12-18 17:15:15 +01:00
} ' ,
2019-08-12 22:01:24 +02:00
],
2019-08-13 20:07:45 +02:00
'usedStrtolowerInArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-13 20:07:45 +02:00
/**
* @ param array < string , int > $row
*/
function foo ( array $row , string $s ) : array {
$row [ " a " . strtolower ( $s )] += 1 ;
return $row ;
} ' ,
],
2019-08-13 20:53:31 +02:00
'pureWithReflectionMethodSetValue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-13 20:53:31 +02:00
function foo ( object $mock ) : void {
$m = new \ReflectionProperty ( $mock , " bar " );
$m -> setValue ([ get_class ( $mock ) => " hello " ]);
2022-12-18 17:15:15 +01:00
} ' ,
2019-08-13 20:53:31 +02:00
],
2019-08-15 15:43:43 +02:00
'defineBeforeAssignmentInConditional' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-14 23:15:35 +02:00
$i = null ;
if ( rand ( 0 , 1 ) || ( $i = rand ( 0 , 1 ))) {
echo $i ;
} ' ,
],
2019-08-15 15:43:43 +02:00
'definedInFirstAssignmentInConditional' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-14 23:15:35 +02:00
if (( $b = rand ( 0 , 1 )) || rand ( 0 , 1 )) {
echo $b ;
} ' ,
],
2019-08-15 15:51:40 +02:00
'noUnusedVariableWhenUndefinedMethod' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-15 15:51:40 +02:00
class A {}
function foo ( A $a ) : void {
$i = 0 ;
/** @psalm-suppress UndefinedMethod */
$a -> bar ( $i );
} ' ,
],
2019-08-26 17:41:15 +02:00
'noUnusedVariableAfterRedeclaredInCatch' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-26 17:41:15 +02:00
$path = " " ;
echo $path ;
try {
// do nothing
} catch ( \Exception $exception ) {
$path = " hello " ;
}
2022-12-18 17:15:15 +01:00
echo $path ; ' ,
2019-08-26 17:41:15 +02:00
],
2019-08-27 04:54:54 +02:00
'assignedInElseif' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-27 04:54:54 +02:00
function bar () : int {
if ( rand ( 0 , 1 ) === 0 ) {
$foo = 0 ;
} elseif ( $foo = rand ( 0 , 10 )) {
return 5 ;
}
return $foo ;
} ' ,
],
2019-09-19 17:59:43 +02:00
'refineForeachVarType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-09-19 17:59:43 +02:00
function foo () : array {
return [ " hello " ];
}
/** @var string $s */
foreach ( foo () as $s ) {
echo $s ;
} ' ,
],
2019-10-20 20:18:30 +02:00
'doWhileReassignedInConditional' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-10-20 20:18:30 +02:00
$index = 0 ;
do {
echo $index ;
2022-12-18 17:15:15 +01:00
} while (( $index = $index + 1 ) < 10 ); ' ,
2019-10-20 20:18:30 +02:00
],
2019-11-09 16:39:21 +01:00
'tryCatchInsaneRepro' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-09 16:39:21 +01:00
function maybeThrows () : string {
return " hello " ;
}
function b ( bool $a ) : void {
if ( ! $a ) {
return ;
}
$b = " " ;
try {
$b = maybeThrows ();
echo $b ;
} catch ( \Exception $e ) {}
echo $b ;
2022-12-18 17:15:15 +01:00
} ' ,
2019-11-09 16:39:21 +01:00
],
'tryCatchInsaneReproNoFirstBoolCheck' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-09 16:39:21 +01:00
function maybeThrows () : string {
return " hello " ;
}
function b () : void {
$b = " " ;
try {
$b = maybeThrows ();
echo $b ;
} catch ( \Exception $e ) {}
echo $b ;
2022-12-18 17:15:15 +01:00
} ' ,
2019-11-09 16:39:21 +01:00
],
2019-11-09 17:01:48 +01:00
'tryWithWhile' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-09 17:01:48 +01:00
function foo () : void {
$done = false ;
while ( ! $done ) {
try {
$done = true ;
} catch ( \Exception $e ) {
}
}
} ' ,
],
'tryWithWhileWithoutTry' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-09 17:01:48 +01:00
function foo () : void {
$done = false ;
while ( ! $done ) {
$done = true ;
}
} ' ,
],
2019-11-11 22:52:55 +01:00
'usedInCatchAndTryWithReturnInTry' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-11 22:52:55 +01:00
function foo () : ? string {
$a = null ;
try {
$a = " hello " ;
echo $a ;
} catch ( Exception $e ) {
return $a ;
}
return $a ;
}
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
} ' ,
],
2020-09-30 18:28:13 +02:00
'useTryAndCatchAssignedVariableInsideFinally' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-12 00:38:33 +01:00
function foo () : void {
try {
// do something dangerous
$a = 5 ;
} catch ( Exception $e ) {
$a = 4 ;
throw new Exception ( " bad " );
} finally {
2020-10-20 15:07:10 +02:00
/** @psalm-suppress PossiblyUndefinedVariable */
2019-11-12 00:38:33 +01:00
echo $a ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2019-11-12 00:38:33 +01:00
],
2019-11-12 06:52:10 +01:00
'usedVarInCatchAndAfter' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-12 06:52:10 +01:00
function foo () : void {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
}
$a = null ;
try {
foo ();
$a = " hello " ;
} catch ( \Exception $e ) {
echo $a ;
}
2022-12-18 17:15:15 +01:00
echo $a ; ' ,
2019-11-12 06:52:10 +01:00
],
2019-11-13 19:38:34 +01:00
'unusedForeach' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-13 19:38:34 +01:00
/**
* @ param array < int , string > $test
*/
function foo ( array $test ) : void {
foreach ( $test as $key => $_testValue ) {
echo $key ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2019-11-13 19:38:34 +01:00
],
'usedAfterMixedVariableAssignment' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-13 19:38:34 +01:00
function foo ( array $arr ) : array {
$c = " c " ;
/** @psalm-suppress MixedArrayAssignment */
$arr [ " a " ][ " b " ][ $c ] = 1 ;
return $arr ;
} ' ,
2019-11-13 20:32:27 +01:00
],
'binaryOpIncrementInElse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-13 20:32:27 +01:00
function foo ( int $i , string $alias ) : void {
echo $alias ? : $i ++ ;
echo $i ;
2022-12-18 17:15:15 +01:00
} ' ,
2019-11-13 20:32:27 +01:00
],
'binaryOpIncrementInCond' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-13 20:32:27 +01:00
function foo ( int $i , string $alias ) : void {
echo $i ++ ? : $alias ;
echo $i ;
2022-12-18 17:15:15 +01:00
} ' ,
2019-11-13 20:32:27 +01:00
],
'binaryOpIncrementInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-13 20:32:27 +01:00
function foo ( int $i , string $alias ) : void {
echo rand ( 0 , 1 ) ? $i ++ : $alias ;
echo $i ;
2022-12-18 17:15:15 +01:00
} ' ,
2019-11-13 20:32:27 +01:00
],
2019-11-18 22:14:21 +01:00
'usedInNewCall' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-11-18 22:14:21 +01:00
/**
* @ psalm - suppress MixedMethodCall
* @ psalm - suppress MixedArgument
2020-09-21 03:25:35 +02:00
* @ psalm - suppress PossiblyNullArgument
2020-09-30 18:28:13 +02:00
* @ param mixed $mixed
* @ param mixed | null $mixed_or_null
2019-11-18 22:14:21 +01:00
*/
2020-09-30 18:28:13 +02:00
function foo ( $mixed , $mixed_or_null ) : void {
$mixed -> foo ( new Exception ( $mixed_or_null ));
2019-11-18 22:14:21 +01:00
} ' ,
],
2019-12-01 16:19:36 +01:00
'validMixedAnnotation' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-12-01 16:19:36 +01:00
function keys () : array {
return [ " foo " , " bar " ];
}
/** @var mixed $k */
foreach ( keys () as $k ) {
echo gettype ( $k );
2022-12-18 17:15:15 +01:00
} ' ,
2019-12-01 16:19:36 +01:00
],
2020-09-30 18:28:13 +02:00
'byRefVariableAfterAssignmentToArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
$a = [ 1 , 2 , 3 ];
$b = & $a [ 1 ];
$b = 5 ;
2022-12-18 17:15:15 +01:00
print_r ( $a ); ' ,
2020-09-30 18:28:13 +02:00
],
'byRefVariableAfterAssignmentToProperty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-12-05 19:37:03 +01:00
class A {
public string $value = " " ;
public function writeByRef ( string $value ) : void {
$update =& $this -> value ;
$update = $value ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2023-05-10 15:43:22 +02:00
'assertions' => [],
'ignored_issues' => [ 'UnsupportedPropertyReferenceUsage' ],
2019-12-05 19:37:03 +01:00
],
2019-12-06 20:47:05 +01:00
'createdAndUsedInCondition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-12-06 20:47:05 +01:00
class A {
public function foo () : bool {
return true ;
}
}
function getA () : ? A {
return rand ( 0 , 1 ) ? new A () : null ;
}
if ( rand ( 0 , 1 )) {
if ( ! ( $a = getA ()) || $a -> foo ()) {}
return ;
}
2022-12-18 17:15:15 +01:00
if ( ! ( $a = getA ()) || $a -> foo ()) {} ' ,
2019-12-06 20:47:05 +01:00
],
2019-12-18 17:37:48 +01:00
'usedInUndefinedFunction' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-12-18 17:37:48 +01:00
/**
* @ psalm - suppress MixedInferredReturnType
* @ psalm - suppress MixedReturnStatement
*/
function test () : string {
$s = " a " ;
/** @psalm-suppress UndefinedFunction */
return undefined_function ( $s );
2022-12-18 17:15:15 +01:00
} ' ,
2019-12-18 17:37:48 +01:00
],
2020-01-02 20:07:02 +01:00
'useVariableVariable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-02 20:07:02 +01:00
$variables = [ " a " => " b " , " c " => " d " ];
foreach ( $variables as $name => $value ) {
${$name} = $value ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-02 20:07:02 +01:00
],
2020-01-03 16:33:32 +01:00
'usedLoopVariable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-03 16:33:32 +01:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
if ( $a < 20 ) {
$a = $a + 1 ;
echo " hello " ;
continue ;
}
echo " goodbye " ;
break ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-03 16:33:32 +01:00
],
2020-01-04 17:16:53 +01:00
'usedForVariable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-04 17:16:53 +01:00
$a = 0 ;
for ( $i = 0 ; $i < 1000 ; $i ++ ) {
if ( rand ( 0 , 1 )) {
2020-09-25 06:14:27 +02:00
$a = $a + 1 ;
2020-01-04 17:16:53 +01:00
continue ;
}
break ;
}
2022-12-18 17:15:15 +01:00
echo $a ; ' ,
2020-01-04 17:16:53 +01:00
],
2020-09-30 18:28:13 +02:00
'usedForVariableMinusString' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
function foo ( string $limit ) : void {
/**
* @ psalm - suppress InvalidOperand
*/
for ( $i = $limit ; $i > 0 ; $i -- ) {
echo $i . " \n " ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 18:28:13 +02:00
],
'usedForVariablePlusString' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
function foo ( string $limit ) : void {
/**
* @ psalm - suppress InvalidOperand
*/
for ( $i = $limit ; $i < 50 ; $i ++ ) {
echo $i . " \n " ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 18:28:13 +02:00
],
2020-01-27 18:17:12 +01:00
'breakInForeachInsideSwitch' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-27 18:17:12 +01:00
function foo ( string $b ) : void {
switch ( $b ){
case " foo " :
$a = null ;
foreach ([ 1 , 2 , 3 ] as $f ){
if ( $f == 2 ) {
$a = $f ;
break ;
}
}
echo $a ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-27 18:17:12 +01:00
],
2020-09-28 00:12:53 +02:00
'passedByRefSimpleUndefinedBefore' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
takes_ref ( $a );
function takes_ref ( ? array & $p ) : void {
$p = [ 0 ];
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'passedByRefSimpleDefinedBefore' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
$a = [];
2020-09-25 06:14:27 +02:00
takes_ref ( $a );
function takes_ref ( ? array & $p ) : void {
$p = [ 0 ];
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
2020-09-30 18:28:13 +02:00
'passedByRefSimpleDefinedBeforeWithExtract' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
function foo ( array $arr ) : void {
while ( rand ( 0 , 1 )) {
/** @psalm-suppress MixedArgument */
extract ( $arr );
$a = [];
takes_ref ( $a );
}
}
2022-01-07 23:16:36 +01:00
/**
* @ param mixed $p
* @ psalm - suppress UnusedParam
*/
2022-12-18 17:15:15 +01:00
function takes_ref ( & $p ) : void {} ' ,
2020-09-30 18:28:13 +02:00
],
2020-01-30 03:46:30 +01:00
'passedByRefArrayOffset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-01-30 03:46:30 +01:00
$a = [
" a " => [ 1 ],
" b " => [ 2 ]
];
foreach ([ " a " ] as $e ){
takes_ref ( $a [ $e ]);
}
2021-06-22 01:55:27 +02:00
/** @param array<string|int> $p */
2020-01-30 03:46:30 +01:00
function takes_ref ( array & $p ) : void {
echo implode ( " , " , $p );
2022-12-18 17:15:15 +01:00
} ' ,
2020-01-30 03:46:30 +01:00
],
2020-03-13 16:41:17 +01:00
'doWhileWithBreak' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-03-13 16:41:17 +01:00
function foo () : void {
$f = false ;
do {
if ( rand ( 0 , 1 )) {
$f = true ;
break ;
}
} while ( rand ( 0 , 1 ));
if ( $f ) {}
2022-12-18 17:15:15 +01:00
} ' ,
2020-03-13 16:41:17 +01:00
],
2020-09-25 06:14:27 +02:00
'usedParamInWhileAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
function foo ( int $index ) : void {
while ( $index ++ <= 100 ) {
//
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
2020-03-14 01:21:49 +01:00
'usedParamInWhileDirectly' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-03-14 01:21:49 +01:00
function foo ( int $index ) : void {
while ( 100 >= $index = nextNumber ( $index )) {
// ...
}
}
function nextNumber ( int $eee ) : int {
return $eee + 1 ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-03-14 01:21:49 +01:00
],
'usedParamInWhileIndirectly' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-03-14 01:21:49 +01:00
function foo ( int $i ) : void {
$index = $i ;
while ( 100 >= $index = nextNumber ( $index )) {
// ...
}
}
function nextNumber ( int $i ) : int {
return $i + 1 ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-03-14 01:21:49 +01:00
],
2020-07-31 18:44:01 +02:00
'doArrayIncrement' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-07-31 18:44:01 +02:00
/**
* @ param list < int > $keys
* @ param int $key
*/
function error2 ( array $keys , int $key ) : int
{
if ( $key === 1 ) {}
do {
$nextKey = $keys [ ++ $key ] ? ? null ;
} while ( $nextKey === null );
return $nextKey ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-07-31 18:44:01 +02:00
],
2020-09-25 06:14:27 +02:00
'variableUsedIndirectly' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )){
$b = $a + 1 ;
echo $b ;
$a = $b ;
} ' ,
],
'arrayMapClosureWithParamType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
$a = [ 1 , 2 , 3 ];
$b = array_map (
function ( int $i ) {
return $i * 3 ;
},
$a
);
foreach ( $b as $c ) {
echo $c ;
} ' ,
],
'arrayMapClosureWithoutParamType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
$a = [ 1 , 2 , 3 ];
$b = array_map (
function ( $i ) {
return $i * 3 ;
},
$a
);
foreach ( $b as $c ) {
echo $c ;
} ' ,
],
'unusedArrayAdditionWithArrayChecked' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
$a = [];
while ( rand ( 0 , 1 )) {
$a [] = 1 ;
}
if ( $a ) {} ' ,
],
'usedArrayRecursiveAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
$a = [];
while ( rand ( 0 , 1 )) {
$a [] = $a ;
}
print_r ( $a ); ' ,
],
'usedImmutableProperty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/**
* @ psalm - immutable
*/
class Clause {
/**
* @ var array < int , int >
*/
public $b = [];
}
function foo ( Clause $c , int $var ) : void {
$new_b = $c -> b ;
if ( isset ( $c -> b [ 0 ])) {
$new_b [ $var ] = 0 ;
}
if ( $new_b ) {}
} ' ,
],
'arrayAssignOpAdditionInsideLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/**
* @ param array < string , string > $arr0
* @ param array < string , string > $arr1
* @ param array < string , string > $arr2
* @ return void
*/
function parp ( array $arr0 , array $arr1 , array $arr2 ) {
$arr3 = $arr0 ;
foreach ( $arr1 as $a ) {
echo $a ;
$arr3 += $arr2 ;
}
if ( $arr3 ) {}
} ' ,
],
'arrayAdditionInsideLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/**
* @ param array < string , string > $arr0
* @ param array < string , string > $arr1
* @ param array < string , string > $arr2
* @ return void
*/
function parp ( array $arr0 , array $arr1 , array $arr2 ) {
$arr3 = $arr0 ;
foreach ( $arr1 as $a ) {
echo $a ;
$arr3 = $arr3 + $arr2 ;
}
if ( $arr3 ) {}
} ' ,
],
'checkValueBeforeAdding' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
class T {
public bool $b = false ;
}
function foo (
? T $t
) : void {
if ( ! $t ) {
$t = new T ();
} elseif ( rand ( 0 , 1 )) {
//
}
if ( $t -> b ) {}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
'loopOverUnknown' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/** @psalm-suppress MixedAssignment */
function foo ( Traversable $t ) : void {
foreach ( $t as $u ) {
if ( $u instanceof stdClass ) {}
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
'loopWithRequire' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/**
* @ psalm - suppress UnresolvableInclude
*/
function foo ( string $delta_file ) : void {
while ( rand ( 0 , 1 )) {
/**
* @ var array < string , mixed >
*/
$diff_call_map = require ( $delta_file );
foreach ( $diff_call_map as $key => $_ ) {
$cased_key = strtolower ( $key );
echo $cased_key ;
}
}
} ' ,
],
'loopAgain' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
/** @param non-empty-list<string> $lines */
function parse ( array $lines ) : array {
$last = 0 ;
foreach ( $lines as $k => $line ) {
if ( rand ( 0 , 1 )) {
$last = $k ;
} elseif ( rand ( 0 , 1 )) {
$last = 0 ;
} elseif ( $last !== 0 ) {
$lines [ $last ] .= $line ;
}
}
return $lines ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
'necessaryVarAnnotation' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
function foo ( array $arr ) : void {
/** @var int $key */
foreach ( $arr as $key => $_ ) {
echo $key ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
'continuingEducation' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
function breakUpPathIntoParts () : void {
$b = false ;
while ( rand ( 0 , 1 )) {
if ( $b ) {
if ( rand ( 0 , 1 )) {
$b = 0 ;
}
echo " hello " ;
continue ;
}
$b = true ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
'usedInBinaryOp' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
function foo ( int $a , int $b ) : int {
$a |= $b ;
return $a ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 06:14:27 +02:00
],
2020-09-25 16:20:22 +02:00
'reassignedInFinally' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 16:20:22 +02:00
function getRows ( int $s ) : void {
try {}
finally {
$s = $s + 3 ;
}
echo $s ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-25 16:20:22 +02:00
],
2020-09-28 00:12:53 +02:00
'divAssignOp' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function hslToRgb ( float $hue ) : float {
$hue /= 360 ;
return $hue ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'concatAssignOp' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function hslToRgb ( string $hue ) : string {
$hue .= " hello " ;
return $hue ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'possiblyUndefinedVariableUsed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( string $a ) : void {
if ( $a === " a " ) {
$hue = " hello " ;
} elseif ( $a === " b " ) {
$hue = " goodbye " ;
}
2021-03-21 01:53:51 +01:00
/**
* @ psalm - suppress PossiblyUndefinedVariable
* @ psalm - suppress MixedArgument
*/
2020-09-28 00:12:53 +02:00
echo $hue ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'possiblyUndefinedVariableUsedInUnknownMethod' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( string $a , object $b ) : void {
if ( $a === " a " ) {
$hue = " hello " ;
} elseif ( $a === " b " ) {
$hue = " goodbye " ;
}
/**
* @ psalm - suppress PossiblyUndefinedVariable
* @ psalm - suppress MixedMethodCall
*/
$b -> foo ( $hue );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'usedAsArrayKey' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function hslToRgb ( string $hue , string $lightness ) : array {
$arr = [ $hue => $lightness ];
return $arr ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'assignToGlobalVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
/** @psalm-suppress MixedAssignment */
function foo ( array $args ) : void {
foreach ( $args as $key => $value ) {
$_GET [ $key ] = $value ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'assignToArrayTwice' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( string $c ) : void {
$arr = [ $c ];
$arr [] = 1 ;
foreach ( $arr as $e ) {
echo $e ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'classPropertyThing' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo () : string {
$notice = " i " ;
$notice .= " j " ;
$notice .= " k " ;
$notice .= " l " ;
$notice .= " m " ;
$notice .= " n " ;
$notice .= " o " ;
$notice .= " p " ;
$notice .= " q " ;
$notice .= " r " ;
$notice .= " s " ;
return $notice ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'usedInIsset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( int $i ) : void {
if ( $i === 0 ) {
$j = " hello " ;
} elseif ( $i === 1 ) {
$j = " goodbye " ;
}
if ( isset ( $j )) {
2021-03-21 01:53:51 +01:00
/** @psalm-suppress MixedArgument */
2020-09-28 00:12:53 +02:00
echo $j ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'byRefNestedArrayParam' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( array & $arr ) : void {
$b = 5 ;
$arr [ 0 ] = $b ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
2022-01-07 23:16:36 +01:00
'byRefDeeplyNestedArrayParam' => [
'code' => ' < ? php
2022-10-03 13:58:01 +02:00
/**
* @ param non - empty - list < non - empty - list < int >> $arr
* @ param - out non - empty - list < non - empty - list < int >> $arr
*/
2022-01-07 23:16:36 +01:00
function foo ( array & $arr ) : void {
$b = 5 ;
$arr [ 0 ][ 0 ] = $b ;
2022-12-18 17:15:15 +01:00
} ' ,
2022-01-07 23:16:36 +01:00
],
'nestedReferencesToByRefParam' => [
'code' => ' < ? php
2022-10-03 13:58:01 +02:00
/**
* @ param non - empty - list < non - empty - list < int >> $arr
* @ param - out non - empty - list < non - empty - list < int >> $arr
*/
2022-01-07 23:16:36 +01:00
function foo ( array & $arr ) : void {
$a = & $arr [ 0 ];
$b = & $a [ 0 ];
$b = 5 ;
2022-12-18 17:15:15 +01:00
} ' ,
2022-01-07 23:16:36 +01:00
],
2020-09-28 00:12:53 +02:00
'byRefNestedArrayInForeach' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( array $arr ) : array {
/**
* @ psalm - suppress MixedAssignment
* @ psalm - suppress MixedArrayAssignment
*/
foreach ( $arr as & $element ) {
$b = 5 ;
$element [ 0 ] = $b ;
}
return $arr ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'instantArrayAssignment' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( string $b ) : array {
/** @psalm-suppress PossiblyUndefinedVariable */
$arr [ " foo " ] = $b ;
return $arr ;
} ' ,
],
2020-09-30 18:28:13 +02:00
'explodeSource' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
$start = microtime ();
$start = explode ( " " , $start );
/**
* @ psalm - suppress InvalidOperand
*/
$start = $start [ 1 ] + $start [ 0 ];
2022-12-18 17:15:15 +01:00
echo $start ; ' ,
2020-09-30 18:28:13 +02:00
],
2020-09-28 00:12:53 +02:00
'csvByRefForeach' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( string $value ) : array {
$arr = str_getcsv ( $value );
foreach ( $arr as & $element ) {
2024-01-12 23:37:45 +01:00
$element = $element !== null ? : " foo " ;
2020-09-28 00:12:53 +02:00
}
return $arr ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
2020-09-30 18:28:13 +02:00
'memoryFree' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
function verifyLoad ( string $free ) : void {
$free = explode ( " \n " , $free );
$parts_mem = preg_split ( " / \ s+/ " , $free [ 1 ]);
$free_mem = $parts_mem [ 3 ];
$total_mem = $parts_mem [ 1 ];
/** @psalm-suppress InvalidOperand */
$used_mem = ( $total_mem - $free_mem ) / $total_mem ;
echo $used_mem ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 18:28:13 +02:00
],
2020-09-28 00:12:53 +02:00
'returnNotBool' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function verifyLoad ( bool $b ) : bool {
$c = ! $b ;
return $c ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'sourcemaps' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
/**
* @ psalm - suppress MixedAssignment
2021-03-21 01:53:51 +01:00
* @ psalm - suppress MixedArgument
2020-09-28 00:12:53 +02:00
* @ param iterable < mixed , int > $keys
*/
function foo ( iterable $keys , int $colno ) : void {
$i = 0 ;
$key = 0 ;
$index = 0 ;
foreach ( $keys as $index => $key ) {
if ( $key === $colno ) {
$i = $index ;
break ;
} elseif ( $key > $colno ) {
$i = $index ;
break ;
}
}
echo $i ;
echo $index ;
echo $key ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'whileLoopVarUpdatedInWhileLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
/** @param non-empty-list<int> $arr */
function foo ( array $arr ) : void {
while ( $a = array_pop ( $arr )) {
if ( $a === 4 ) {
$arr = array_merge ( $arr , [ " a " , " b " , " c " ]);
continue ;
}
echo " here " ;
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'usedThroughParamByRef' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
$arr = [];
$populator = function ( array & $arr ) : void {
$arr [] = 5 ;
};
$populator ( $arr );
2022-12-18 17:15:15 +01:00
print_r ( $arr ); ' ,
2020-09-28 00:12:53 +02:00
],
'maybeUndefinedCheckedWithEmpty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( array $arr ) : void {
if ( rand ( 0 , 1 )) {
$maybe_undefined = $arr ;
}
if ( empty ( $maybe_undefined )) {
$maybe_undefined = [ 0 ];
}
print_r ( $maybe_undefined );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'maybeUndefinedCheckedWithEmptyOrRand' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( array $arr ) : void {
if ( rand ( 0 , 1 )) {
$maybe_undefined = $arr ;
}
if ( empty ( $maybe_undefined ) || rand ( 0 , 1 )) {
$maybe_undefined = [ 0 ];
}
print_r ( $maybe_undefined );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'maybeUndefinedCheckedWithNotIsset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( array $arr ) : void {
if ( rand ( 0 , 1 )) {
$maybe_undefined = $arr ;
}
if ( ! isset ( $maybe_undefined )) {
$maybe_undefined = [ 0 ];
}
print_r ( $maybe_undefined );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
'maybeUndefinedCheckedWithImplicitIsset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-28 00:12:53 +02:00
function foo ( array $arr ) : void {
if ( rand ( 0 , 1 )) {
$maybe_undefined = $arr ;
}
/** @psalm-suppress MixedAssignment */
$maybe_undefined = $maybe_undefined ? ? [ 0 ];
print_r ( $maybe_undefined );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-28 00:12:53 +02:00
],
2020-09-30 18:28:13 +02:00
'usedInGlobalAfterAssignOp' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
$total = 0 ;
$foo = & $total ;
$total = 5 ;
2022-12-18 17:15:15 +01:00
echo $foo ; ' ,
2020-09-30 18:28:13 +02:00
],
'takesByRefThing' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$c = 5 ;
}
takesByRef ( $c );
echo $c ;
}
/**
2020-11-27 23:05:26 +01:00
* @ psalm - param - out int $c
2020-09-30 18:28:13 +02:00
*/
function takesByRef ( ? int & $c ) : void {
$c = 7 ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 18:28:13 +02:00
],
'clips' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php declare ( strict_types = 1 );
2020-09-30 18:28:13 +02:00
function foo ( array $clips ) : void {
/** @psalm-suppress MixedAssignment */
foreach ( $clips as & $clip ) {
/** @psalm-suppress MixedArgument */
if ( ! empty ( $clip )) {
$legs = explode ( " / " , $clip );
$clip_id = $clip = $legs [ 1 ];
if (( is_numeric ( $clip_id ) || $clip = ( new \Exception ( $clip_id )))) {}
print_r ( $clips );
}
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 18:28:13 +02:00
],
'validator' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
/**
* @ param bool $b
*/
function validate ( $b , string $source ) : void {
2023-11-18 11:03:17 +01:00
/** @var bool|string $b */
if ( ! is_bool ( $b )) {
$source = $b ;
$b = false ;
}
2020-09-30 18:28:13 +02:00
/**
2023-11-18 11:03:17 +01:00
* test to ensure $b is only type bool and not bool | string anymore
* after we set $b = false ; inside the condition above
* @ psalm - suppress TypeDoesNotContainType
2020-09-30 18:28:13 +02:00
*/
if ( ! is_bool ( $b )) {
2023-11-18 11:03:17 +01:00
echo " this should not happen " ;
2020-09-30 18:28:13 +02:00
}
print_r ( $source );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 18:28:13 +02:00
],
2020-09-30 19:08:01 +02:00
'implicitSpread' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 19:08:01 +02:00
function validate ( bool $b , bool $c ) : void {
$d = [ $b , $c ];
print_r ( ... $d );
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 19:08:01 +02:00
],
2021-01-20 23:42:11 +01:00
'explicitSpread' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-01-20 23:42:11 +01:00
function f () : array {
$s = [ 1 , 2 , 3 ];
$b = [ " a " , " b " , " c " ];
$r = [ ... $s , ... $b ];
return $r ;
2022-12-18 17:15:15 +01:00
} ' ,
2021-01-20 23:42:11 +01:00
],
2020-09-30 19:08:01 +02:00
'funcGetArgs' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 19:08:01 +02:00
function validate ( bool $b , bool $c ) : void {
2020-10-15 00:51:15 +02:00
/** @psalm-suppress MixedArgument */
2020-09-30 19:08:01 +02:00
print_r ( ... func_get_args ());
2022-12-18 17:15:15 +01:00
} ' ,
2020-09-30 19:08:01 +02:00
],
2020-10-15 15:57:37 +02:00
'nullCoalesce' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-10-15 15:57:37 +02:00
function foo ( ? bool $b , int $c ) : void {
$b ? ? = $c ;
echo $b ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-10-15 15:57:37 +02:00
],
2020-10-20 15:32:08 +02:00
'arrowFunctionImplicitlyUsedVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-10-20 15:32:08 +02:00
function test ( Exception $e ) : callable {
return fn () => $e -> getMessage ();
2022-12-18 17:15:15 +01:00
} ' ,
2020-10-20 15:32:08 +02:00
],
2020-10-20 16:59:09 +02:00
'useImmutableGetIteratorInForeach' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-10-20 16:59:09 +02:00
/**
* @ psalm - immutable
2022-01-26 18:46:02 +01:00
* @ psalm - suppress MissingTemplateParam
2020-10-20 16:59:09 +02:00
*/
class A implements IteratorAggregate
{
/**
* @ return Iterator < int >
*/
public function getIterator () {
yield from [ 1 , 2 , 3 ];
}
}
$a = new A ();
foreach ( $a as $v ) {
echo $v ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-10-20 16:59:09 +02:00
],
2020-10-28 19:06:05 +01:00
'castToBoolAndDouble' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-10-28 19:06:05 +01:00
function string_to_bool ( string $a ) : bool {
$b = ( bool ) $a ;
return $b ;
}
function string_to_float ( string $a ) : float {
$b = ( float ) $a ;
return $b ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-10-28 19:06:05 +01:00
],
2020-11-13 18:50:01 +01:00
'allowUseByRef' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-11-13 18:50:01 +01:00
function foo ( array $data ) : array {
$output = [];
array_map (
function ( array $row ) use ( & $output ) {
$output = $row ;
},
$data
);
return $output ;
2022-12-18 17:15:15 +01:00
} ' ,
2020-11-13 18:50:01 +01:00
],
2020-11-13 19:37:27 +01:00
'allowedUseByRefArrayAssignment' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-11-13 19:37:27 +01:00
$output_rows = [];
$a = function () use ( & $output_rows ) : void {
$output_row = 5 ;
$output_rows [] = $output_row ;
};
$a ();
2022-12-18 17:15:15 +01:00
print_r ( $output_rows ); ' ,
2020-11-13 19:37:27 +01:00
],
2020-11-17 21:30:46 +01:00
'usedInAssignOpToByRef' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-11-17 21:30:46 +01:00
function foo ( int & $d ) : void {
$l = 4 ;
$d += $l ;
} ' ,
],
2020-12-03 19:09:08 +01:00
'mixedArrayAccessMighBeObject' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-12-03 19:09:08 +01:00
function takesResults ( array $arr ) : void {
/**
* @ psalm - suppress MixedAssignment
*/
foreach ( $arr as $item ) {
/**
* @ psalm - suppress MixedArrayAccess
* @ psalm - suppress MixedArrayAssignment
*/
$item [ 0 ] = $item [ 1 ];
}
2022-12-18 17:15:15 +01:00
} ' ,
2020-12-03 19:09:08 +01:00
],
2021-02-07 04:07:01 +01:00
'usedThrow' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-02-07 04:07:01 +01:00
function f ( Exception $e ) : void {
throw $e ;
}
' ,
],
'usedThrowInReturnedCallable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-02-07 04:07:01 +01:00
function createFailingFunction ( RuntimeException $exception ) : Closure
{
return static function () use ( $exception ) : void {
throw $exception ;
};
}
' ,
2021-03-11 06:08:32 +01:00
],
'usedInIntCastInAssignment' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-11 06:08:32 +01:00
/** @return mixed */
function f () {
$a = random_int ( 0 , 10 ) >= 5 ? true : false ;
$b = ( int ) $a ;
return $b ;
}
2022-12-18 17:15:15 +01:00
' ,
2021-03-16 18:41:43 +01:00
],
'promotedPropertiesAreNeverMarkedAsUnusedParams' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-16 18:41:43 +01:00
class Container {
private function __construct (
public float $value
) {}
public static function fromValue ( float $value ) : self {
return new self ( $value );
}
2022-12-18 17:15:15 +01:00
} ' ,
2021-03-16 18:41:43 +01:00
],
2021-04-18 22:16:54 +02:00
'noUnusedVariableDefinedInBranchOfIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-04-18 22:16:54 +02:00
abstract class Foo {
abstract function validate () : bool | string ;
abstract function save () : bool | string ;
function bar () : int {
if (( $result = $this -> validate ()) && ( $result = $this -> save ())) {
return 0 ;
} elseif ( is_string ( $result )) {
return 1 ;
} else {
return 2 ;
}
}
2022-01-22 01:00:45 +01:00
} ' ,
2023-04-10 00:53:33 +02:00
'assertions' => [],
2024-01-12 23:37:45 +01:00
'ignored_issues' => [ 'RiskyTruthyFalsyComparison' ],
2022-01-22 01:00:45 +01:00
'php_version' => '8.0' ,
2021-04-18 22:16:54 +02:00
],
2021-04-25 20:49:43 +02:00
'concatWithUnknownProperty' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-04-25 20:49:43 +02:00
/** @param array<string> $key */
function foo ( object $a , string $k ) : string {
$sortA = " " ;
/** @psalm-suppress MixedOperand */
$sortA .= $a -> $k ;
return $sortA ;
2022-12-18 17:15:15 +01:00
} ' ,
2021-04-25 20:49:43 +02:00
],
2021-04-25 21:11:23 +02:00
'varDocblockVariableIsUsedByRef' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-06-22 01:55:27 +02:00
/** @param array<string|int> $arr */
2021-04-25 21:11:23 +02:00
function foo ( array $arr ) : string {
/** @var string $val */
foreach ( $arr as & $val ) {
$val = urlencode ( $val );
}
return implode ( " / " , $arr );
2022-12-18 17:15:15 +01:00
} ' ,
2021-04-25 21:11:23 +02:00
],
2021-07-12 23:05:33 +02:00
'initVariableInOffset' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-07-12 23:05:33 +02:00
$a = [
$b = " b " => $b ,
];
foreach ( $a as $key => $value ) {
echo $key . " " . $value ;
} ' ,
],
2021-07-12 19:09:20 +02:00
'intAndBitwiseNotOperator' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-07-12 19:09:20 +02:00
function foo () : int
{
$bitmask = 0x1 ;
$bytes = 2 ;
$ret = $bytes | ~ $bitmask ;
return $ret ;
2022-12-18 17:15:15 +01:00
} ' ,
2021-07-12 19:09:20 +02:00
],
'stringAndBitwiseAndOperator' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-07-12 19:09:20 +02:00
function randomBits () : string
{
$bitmask = \chr ( 0xFF >> 1 );
$randomBytes = random_bytes ( 1 );
$randomBytes [ 0 ] = $randomBytes [ 0 ] & $bitmask ;
return $randomBytes ;
2022-12-18 17:15:15 +01:00
} ' ,
2021-07-12 19:09:20 +02:00
],
2021-07-17 22:00:54 +02:00
'globalChangeValue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-07-17 22:00:54 +02:00
function setProxySettingsFromEnv () : void {
global $a ;
$a = false ;
2022-12-18 17:15:15 +01:00
} ' ,
2021-07-17 22:00:54 +02:00
],
2022-02-13 22:02:46 +01:00
'usedInCatchIsAlwaysUsedInTry' => [
2022-02-14 00:12:31 +01:00
'code' => ' < ? php
2022-02-13 22:02:46 +01:00
$step = 0 ;
try {
$step = 1 ;
$step = 2 ;
} catch ( Throwable $_ ) {
echo $step ;
}
' ,
],
'usedInFinallyIsAlwaysUsedInTry' => [
2022-02-14 00:12:31 +01:00
'code' => ' < ? php
2022-02-13 22:02:46 +01:00
$step = 0 ;
try {
$step = 1 ;
$step = 2 ;
} finally {
echo $step ;
}
' ,
],
2022-02-13 22:30:06 +01:00
'usedInFinallyIsAlwaysUsedInTryWithNestedTry' => [
2022-02-14 00:12:31 +01:00
'code' => ' < ? php
2022-02-13 22:30:06 +01:00
$step = 0 ;
try {
try {
$step = 1 ;
} finally {
}
$step = 2 ;
$step = 3 ;
} finally {
echo $step ;
}
' ,
],
2022-01-07 23:16:36 +01:00
'referenceUseUsesReferencedVariable' => [
'code' => ' < ? php
$a = 1 ;
$b = & $a ;
echo $b ;
' ,
],
'referenceAssignmentToNonReferenceCountsAsUse' => [
'code' => ' < ? php
$b = & $a ;
$b = 2 ;
echo $a ;
' ,
],
'referenceUsedAfterVariableReassignment' => [
'code' => ' < ? php
$b = & $a ;
$a = 2 ;
echo $a ;
$b = 3 ;
echo $a ;
' ,
],
'referenceUsedInForeach' => [
'code' => ' < ? php
foreach ([ 1 , 2 , 3 ] as & $var ) {
$var += 1 ;
}
' ,
],
'SKIPPED-referenceUsedInDestructuredForeach' => [
'code' => ' < ? php
foreach ([[ 1 , 2 ], [ 3 , 4 ]] as [ & $a , $_ ]) {
$a += 1 ;
}
' ,
],
2022-01-11 00:45:29 +01:00
'arrayWithReferenceIsUsed' => [
'code' => ' < ? php
/** @var non-empty-list<int> */
$arr = [ 1 ];
$arr [ 1 ] = & $arr [ 0 ];
takesArray ( $arr );
function takesArray ( array $_arr ) : void {}
' ,
],
'arrayWithVariableOffsetAssignedToReferenceUsesVariableOffset' => [
'code' => ' < ? php
/** @var non-empty-list<int> */
$arr = [ 1 ];
$int = 1 ;
$arr [ $int ] = & $arr [ 0 ];
takesArray ( $arr );
function takesArray ( array $_arr ) : void {}
' ,
],
2022-01-29 00:30:47 +01:00
'usedPlusInAddition' => [
'code' => ' < ? php
function takesAnInt () : void {
$i = 0 ;
while ( rand ( 0 , 1 )) {
if (( $i = $i + 1 ) > 10 ) {
break ;
} else {}
}
} ' ,
],
'usedPlusInUnaryAddition' => [
'code' => ' < ? php
function takesAnInt () : void {
$i = 0 ;
while ( rand ( 0 , 1 )) {
if ( ++ $i > 10 ) {
break ;
} else {}
}
} ' ,
],
2022-02-17 16:31:33 +01:00
'referenceInPropertyIsNotUnused' => [
'code' => ' < ? php
class Foo
{
/** @var int|null */
public $bar = null ;
public function setBarRef ( int $ref ) : void
{
$this -> bar = & $ref ;
}
}
' ,
],
2023-04-10 00:53:50 +02:00
'requiredClosureArgumentMustNotGetReported' => [
'code' => ' < ? php
/** @param callable(string,int): void $callable */
function takesCallable ( callable $callable ) : void
{
$callable ( " foo " , 0 );
}
takesCallable (
static function ( string $foo , int $bar ) {
if ( $bar === 0 ) {
throw new RuntimeException ();
}
}
); ' ,
],
2018-06-17 02:01:33 +02:00
];
}
2020-09-12 17:24:05 +02:00
public function providerInvalidCodeParse () : array
2018-06-17 02:01:33 +02:00
{
return [
'simpleUnusedVariable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
$b = [];
echo $a ; ' ,
'error_message' => 'UnusedVariable' ,
],
2018-06-17 05:40:25 +02:00
'unusedVarWithAdditionOp' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 05:40:25 +02:00
$a = 5 ;
$a += 1 ; ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedVarWithConditionalAdditionOp' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 05:40:25 +02:00
$a = 5 ;
if ( rand ( 0 , 1 )) {
$a += 1 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedVarWithConditionalAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 05:40:25 +02:00
$a = 5 ;
if ( rand ( 0 , 1 )) {
$a = $a + 1 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedVarWithIncrement' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 05:40:25 +02:00
$a = 5 ;
$a ++ ; ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedVarWithConditionalIncrement' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 05:40:25 +02:00
$a = 5 ;
if ( rand ( 0 , 1 )) {
$a ++ ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
2018-06-17 02:01:33 +02:00
'ifInBothBranchesWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
if ( rand ( 0 , 1 )) {
$b = " hello " ;
} else {
$b = " goodbye " ;
}
echo $a ; ' ,
'error_message' => 'UnusedVariable' ,
],
'varInNestedAssignmentWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
if ( rand ( 0 , 1 )) {
$a = " foo " ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'varInSecondNestedAssignmentWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
if ( rand ( 0 , 1 )) {
$a = " foo " ;
echo $a ;
}
if ( rand ( 0 , 1 )) {
$a = " foo " ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'varReassignedInBothBranchesOfIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = " foo " ;
if ( rand ( 0 , 1 )) {
$a = " bar " ;
} else {
$a = " bat " ;
}
echo $a ; ' ,
'error_message' => 'UnusedVariable' ,
],
'varReassignedInNestedBranchesOfIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = " foo " ;
if ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = " bar " ;
} else {
$a = " bat " ;
}
} else {
$a = " bang " ;
}
echo $a ; ' ,
'error_message' => 'UnusedVariable' ,
],
'ifVarReassignedInBranchWithNoUse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = true ;
if ( rand ( 0 , 1 )) {
$a = false ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'elseVarReassignedInBranchAndNoReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = true ;
if ( rand ( 0 , 1 )) {
// do nothing
} else {
$a = false ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'switchVarReassignedInBranch' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
switch ( rand ( 0 , 2 )) {
case 0 :
$a = true ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'switchVarReassignedInBranchWithDefault' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
switch ( rand ( 0 , 2 )) {
case 0 :
$a = true ;
break ;
default :
$a = false ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'switchVarReassignedInAllBranches' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
switch ( rand ( 0 , 2 )) {
case 0 :
$a = true ;
break ;
default :
$a = false ;
}
if ( $a ) {
echo " cool " ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedListVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
list ( $a , $b ) = explode ( " " , " hello world " );
echo $a ; ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedPreForVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$i = 0 ;
for ( $i = 0 ; $i < 10 ; $i ++ ) {
echo $i ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedIfInReturnBlock' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$i = rand ( 0 , 1 );
foreach ([ 1 , 2 , 3 ] as $a ) {
if ( $a % 2 ) {
$i = 7 ;
return ;
}
}
if ( $i ) {} ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedIfVarInBranch' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
if ( rand ( 0 , 1 )) {
} elseif ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = " foo " ;
} else {
$a = " bar " ;
echo $a ;
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
'throwWithMessageCallAndAssignmentAndNoReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
}
function callDangerous () : void {
$s = null ;
try {
$s = dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
'throwWithMessageCallAndAssignmentInCatchAndNoReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
}
function callDangerous () : void {
$s = null ;
try {
dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
$s = " hello " ;
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
'throwWithMessageCallAndNestedAssignmentInTryAndCatchAndNoReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
function dangerous () : string {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
return " hello " ;
}
function callDangerous () : void {
$s = null ;
if ( rand ( 0 , 1 )) {
$s = " hello " ;
} else {
try {
$t = dangerous ();
} catch ( Exception $e ) {
echo $e -> getMessage ();
$t = " hello " ;
}
if ( $t ) {
$s = $t ;
}
}
2018-06-17 03:14:19 +02:00
} ' ,
'error_message' => 'UnusedVariable' ,
],
'throwWithReturnInOneCatchAndNoReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 03:14:19 +02:00
class E1 extends Exception {}
function dangerous () : void {
if ( rand ( 0 , 1 )) {
throw new \Exception ( " bad " );
}
}
function callDangerous () : void {
try {
dangerous ();
$s = true ;
} catch ( E1 $e ) {
echo $e -> getMessage ();
$s = false ;
} catch ( Exception $e ) {
return ;
}
2018-06-17 02:01:33 +02:00
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopTypeChangedInIfWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = true ;
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopTypeChangedInIfAndContinueWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = false ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = true ;
continue ;
}
$a = false ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopReassignedInIfAndContinueWithoutReferenceAfter' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = 7 ;
continue ;
}
$a = 3 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopReassignedInIfAndContinueWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 3 ;
2018-11-11 01:05:51 +01:00
echo $a ;
2018-06-17 02:01:33 +02:00
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = 5 ;
continue ;
}
$a = 3 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'unusedConditionalCode' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
if ( rand ( 0 , 1 )) {
$a = $a + 5 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'varDefinedInIfWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 5 ;
if ( rand ( 0 , 1 )) {
$b = " hello " ;
} else {
$b = " goodbye " ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'SKIPPED-byrefInForeachLoopWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = [ 1 , 2 , 3 ];
foreach ( $a as & $b ) {
$b = $b + 1 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopSetIfNullWithBreakWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = null ;
while ( rand ( 0 , 1 )) {
if ( $a !== null ) {
$a = 4 ;
break ;
}
$a = 5 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
2018-11-27 23:45:07 +01:00
'loopSetIfNullWithBreakWithoutReference2' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = null ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = 4 ;
break ;
}
$a = 5 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopSetIfNullWithContinueWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = null ;
while ( rand ( 0 , 1 )) {
if ( rand ( 0 , 1 )) {
$a = 4 ;
continue ;
}
$a = 5 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopAssignmentAfterReferenceWithBreak' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
echo $a ;
$a = 1 ;
break ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'loopAssignmentAfterReferenceWithBreakInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 02:01:33 +02:00
$a = 0 ;
while ( rand ( 0 , 1 )) {
echo $a ;
if ( rand ( 0 , 1 )) {
$a = 1 ;
break ;
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
2018-06-17 03:54:44 +02:00
'switchVarConditionalAssignmentWithoutReference' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 03:54:44 +02:00
switch ( rand ( 0 , 4 )) {
case 0 :
if ( rand ( 0 , 1 )) {
$a = 0 ;
break ;
}
default :
$a = 1 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
2018-06-17 06:12:26 +02:00
'switchInIf' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 06:12:26 +02:00
$a = 0 ;
if ( rand ( 0 , 1 )) {
switch ( rand ( 0 , 4 )) {
case 0 :
$a = 3 ;
break ;
default :
$a = 3 ;
}
} else {
$a = 6 ;
}
echo $a ; ' ,
'error_message' => 'UnusedVariable' ,
],
2018-06-17 06:52:32 +02:00
'reusedKeyVar' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2018-06-17 06:52:32 +02:00
$key = " a " ;
echo $key ;
$arr = [ " foo " => " foo.foo " ];
foreach ( $arr as $key => $v ) {
list ( $key ) = explode ( " . " , $v );
echo $key ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-05-02 23:21:02 +02:00
'detectUnusedVarBeforeTryInsideForeach' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-02 23:21:02 +02:00
function foo () : void {
$unused = 1 ;
while ( rand ( 0 , 1 )) {
try {} catch ( \Exception $e ) {}
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-05-13 02:49:37 +02:00
'detectUnusedVariableInsideIfLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 02:49:37 +02:00
function foo () : void {
$a = 1 ;
if ( rand ( 0 , 1 )) {
while ( rand ( 0 , 1 )) {
$a = 2 ;
}
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
'detectUnusedVariableInsideIfElseLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 02:49:37 +02:00
function foo () : void {
$a = 1 ;
if ( rand ( 0 , 1 )) {
} else {
while ( rand ( 0 , 1 )) {
$a = 2 ;
}
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
'detectUnusedVariableInsideIfElseifLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 02:49:37 +02:00
function foo () : void {
$a = 1 ;
if ( rand ( 0 , 1 )) {
} elseif ( rand ( 0 , 1 )) {
while ( rand ( 0 , 1 )) {
$a = 2 ;
}
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
'detectUnusedVariableInsideIfLoopWithEchoInside' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 02:49:37 +02:00
function foo () : void {
$a = 1 ;
if ( rand ( 0 , 1 )) {
while ( rand ( 0 , 1 )) {
$a = 2 ;
echo $a ;
}
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-05-13 05:31:36 +02:00
'detectUnusedVariableInsideLoopAfterAssignment' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 05:31:36 +02:00
function foo () : void {
foreach ([ 1 , 2 , 3 ] as $i ) {
$i = $i ;
}
} ' ,
2021-06-17 06:40:24 +02:00
'error_message' => 'UnusedForeachValue' ,
2019-05-13 05:31:36 +02:00
],
'detectUnusedVariableInsideLoopAfterAssignmentWithAddition' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 05:31:36 +02:00
function foo () : void {
foreach ([ 1 , 2 , 3 ] as $i ) {
$i = $i + 1 ;
}
} ' ,
2021-06-17 06:40:24 +02:00
'error_message' => 'UnusedForeachValue' ,
2019-05-13 05:31:36 +02:00
],
'detectUnusedVariableInsideLoopCalledInFunction' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-13 05:31:36 +02:00
function foo ( int $s ) : int {
return $s ;
}
function bar () : void {
foreach ([ 1 , 2 , 3 ] as $i ) {
$i = foo ( $i );
}
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-05-21 00:25:11 +02:00
'detectUnusedVariableReassignedInIfFollowedByTryInsideForLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-21 00:25:11 +02:00
$user_id = 0 ;
$user = null ;
if ( rand ( 0 , 1 )) {
$user_id = rand ( 0 , 1 );
$user = $user_id ;
}
2024-01-12 23:37:45 +01:00
if ( $user !== null && $user !== 0 ) {
2019-05-21 00:25:11 +02:00
$a = 0 ;
for ( $i = 1 ; $i <= 10 ; $i ++ ) {
$a += $i ;
try {} catch ( \Exception $e ) {}
}
echo $i ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'detectUnusedVariableReassignedInIfFollowedByTryInsideForeachLoop' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-05-21 00:25:11 +02:00
$user_id = 0 ;
$user = null ;
if ( rand ( 0 , 1 )) {
$user_id = rand ( 0 , 1 );
$user = $user_id ;
}
2024-01-12 23:37:45 +01:00
if ( $user !== null && $user !== 0 ) {
2019-05-21 00:25:11 +02:00
$a = 0 ;
foreach ([ 1 , 2 , 3 ] as $i ) {
$a += $i ;
try {} catch ( \Exception $e ) {}
}
echo $i ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-07-04 22:38:31 +02:00
'detectUselessArrayAssignment' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-07-04 22:38:31 +02:00
function foo () : void {
$a = [];
$a [ 0 ] = 1 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-07-04 23:35:33 +02:00
'detectUnusedSecondAssignmentBeforeTry' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-07-04 23:35:33 +02:00
$a = [ 1 , 2 , 3 ];
echo ( $a [ 0 ]);
$a = [ 4 , 5 , 6 ];
try {
// something
} catch ( \Throwable $t ) {
// something else
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-07-05 04:25:49 +02:00
'detectRedundancyAfterLoopWithContinue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-07-05 04:25:49 +02:00
$gap = null ;
foreach ([ 1 , 2 , 3 ] as $_ ) {
if ( rand ( 0 , 1 )) {
continue ;
}
$gap = " asa " ;
throw new \Exception ( $gap );
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-08-12 22:01:24 +02:00
'setInLoopThatsAlwaysEnteredButNotReferenced' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-12 22:01:24 +02:00
/**
* @ param non - empty - array < int > $a
*/
function getLastNum ( array $a ) : int {
foreach ( $a as $num ) {
$last = $num ;
}
return 4 ;
} ' ,
2021-06-17 06:40:24 +02:00
'error_message' => 'UnusedForeachValue' ,
],
'conditionalForeachWithUnusedValue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-06-17 06:40:24 +02:00
if ( rand ( 0 , 1 ) > 0 ) {
foreach ([ 1 , 2 , 3 ] as $val ) {}
}
' ,
'error_message' => 'UnusedForeachValue' ,
],
'doubleForeachWithInnerUnusedValue' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-06-17 06:40:24 +02:00
/**
* @ param non - empty - list < list < int >> $arr
* @ return list < int >
*/
function f ( array $arr ) : array {
foreach ( $arr as $elt ) {
foreach ( $elt as $subelt ) {}
}
return $elt ;
}
' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'UnusedForeachValue' ,
2019-08-12 22:01:24 +02:00
],
2019-08-15 15:43:43 +02:00
'defineInBothBranchesOfConditional' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-08-15 15:43:43 +02:00
$i = null ;
if (( $i = rand ( 0 , 5 )) || ( $i = rand ( 0 , 3 ))) {
echo $i ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
2019-09-19 17:59:43 +02:00
'knownVarType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-09-19 17:59:43 +02:00
function foo () : string {
return " hello " ;
}
/** @var string */
$a = foo ();
echo $a ; ' ,
'error_message' => 'UnnecessaryVarAnnotation' ,
],
'knownVarTypeWithName' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-09-19 17:59:43 +02:00
function foo () : string {
return " hello " ;
}
/** @var string $a */
$a = foo ();
echo $a ; ' ,
'error_message' => 'UnnecessaryVarAnnotation' ,
],
'knownForeachVarType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2019-09-19 17:59:43 +02:00
/** @return string[] */
function foo () : array {
return [ " hello " ];
}
/** @var string $s */
foreach ( foo () as $s ) {
echo $s ;
} ' ,
'error_message' => 'UnnecessaryVarAnnotation' ,
],
2020-09-12 23:03:11 +02:00
'arrowFunctionUnusedVariable' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 23:03:11 +02:00
function f ( callable $c ) : void {
$c ( 22 );
}
f (
fn ( int $p )
=>
++ $p
); ' ,
'error_message' => 'UnusedVariable' ,
],
'arrowFunctionUnusedParam' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-12 23:03:11 +02:00
function f ( callable $c ) : void {
$c ( 22 );
}
f (
fn ( int $p )
=>
0
); ' ,
'error_message' => 'UnusedClosureParam' ,
],
2020-09-14 16:58:36 +02:00
'unusedFunctionParamWithDefault' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-14 16:58:36 +02:00
function foo ( bool $b = false ) : void {} ' ,
'error_message' => 'UnusedParam' ,
],
2020-09-25 06:14:27 +02:00
'arrayMapClosureWithParamTypeNoUse' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-25 06:14:27 +02:00
$a = [ 1 , 2 , 3 ];
$b = array_map (
function ( int $i ) {
return rand ( 0 , 5 );
},
$a
);
foreach ( $b as $c ) {
echo $c ;
} ' ,
'error_message' => 'UnusedClosureParam' ,
],
2020-09-30 18:28:13 +02:00
'noUseOfInstantArrayAssignment' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
function foo () : void {
/** @psalm-suppress PossiblyUndefinedVariable */
$arr [ " foo " ] = 1 ;
} ' ,
'error_message' => 'UnusedVariable' ,
],
'expectsNonNullAndPassedPossiblyNull' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-09-30 18:28:13 +02:00
/**
* @ param mixed | null $mixed_or_null
*/
function foo ( $mixed_or_null ) : Exception {
/**
* @ psalm - suppress MixedArgument
*/
return new Exception ( $mixed_or_null );
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'PossiblyNullArgument' ,
2020-09-30 18:28:13 +02:00
],
2020-11-13 19:13:29 +01:00
'useArrayAssignmentNeverUsed' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2020-11-13 19:13:29 +01:00
$data = [];
return function () use ( $data ) {
$data [] = 1 ;
}; ' ,
'error_message' => 'UnusedVariable' ,
],
2021-03-17 06:10:42 +01:00
'warnAboutOriginalBadArray' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-17 06:10:42 +01:00
function takesArray ( array $arr ) : void {
foreach ( $arr as $a ) {}
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:42 - Unable to determine the type that $a is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:2:47' ,
2021-03-17 06:10:42 +01:00
],
2021-03-18 00:37:21 +01:00
'warnAboutOriginalBadFunctionCall' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-18 00:37:21 +01:00
function makeArray () : array {
return [ " hello " ];
}
$arr = makeArray ();
foreach ( $arr as $a ) {
echo $a ;
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:38 - Unable to determine the type that $a is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:2:44' ,
2021-03-18 00:37:21 +01:00
],
'warnAboutOriginalBadStaticCall' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-18 00:37:21 +01:00
class A {
public static function makeArray () : array {
return [ " hello " ];
}
}
$arr = A :: makeArray ();
foreach ( $arr as $a ) {
echo $a ;
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:10:38 - Unable to determine the type that $a is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:3:62' ,
2021-03-18 00:37:21 +01:00
],
'warnAboutOriginalBadInstanceCall' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-18 00:37:21 +01:00
class A {
public function makeArray () : array {
return [ " hello " ];
}
}
$arr = ( new A ) -> makeArray ();
foreach ( $arr as $a ) {
echo $a ;
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:10:38 - Unable to determine the type that $a is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:3:55' ,
2021-03-18 00:37:21 +01:00
],
2021-03-18 20:19:29 +01:00
'warnAboutDocblockReturnType' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-18 20:19:29 +01:00
/** @return array[] */
function makeArray () : array {
return [[ " hello " ]];
}
$arr = makeArray ();
foreach ( $arr as $some_arr ) {
foreach ( $some_arr as $a ) {
echo $a ;
}
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:10:47 - Unable to determine the type that $a is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:2:33' ,
2021-03-21 02:45:38 +01:00
],
'warnAboutMixedArgument' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-21 02:45:38 +01:00
function makeArray () : array {
return [ " hello " ];
}
$arr = makeArray ();
/** @psalm-suppress MixedAssignment */
foreach ( $arr as $a ) {
echo $a ;
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:10:30 - Argument 1 of echo cannot be mixed, expecting string. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:2:44' ,
2021-03-21 02:45:38 +01:00
],
'warnAboutMixedMethodCall' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-21 02:45:38 +01:00
function makeArray () : array {
return [ " hello " ];
}
$arr = makeArray ();
/** @psalm-suppress MixedAssignment */
foreach ( $arr as $a ) {
$a -> foo ();
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedMethodCall - src' . DIRECTORY_SEPARATOR . 'somefile.php:10:29 - Cannot determine the type of $a when calling method foo. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:2:44' ,
2021-03-21 02:45:38 +01:00
],
'warnAboutMixedReturnStatement' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-21 02:45:38 +01:00
function makeArray () : array {
return [ " hello " ];
}
function foo () : string {
$arr = makeArray ();
/** @psalm-suppress MixedAssignment */
foreach ( $arr as $a ) {
return $a ;
}
return " " ;
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:36 - Could not infer a return type. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:2:44' ,
2021-03-18 20:19:29 +01:00
],
2021-03-24 20:32:56 +01:00
'warnAboutIterableKeySource' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-24 20:32:56 +01:00
function foo ( iterable $arr ) : void {
foreach ( $arr as $key => $_ ) {}
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:42 - Unable to determine the type that $key is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:2:43' ,
2021-03-24 20:32:56 +01:00
],
2021-03-24 21:42:22 +01:00
'warnAboutMixedKeySource' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-24 21:42:22 +01:00
/** @param mixed $arr */
function foo ( $arr ) : void {
foreach ( $arr as $key => $_ ) {}
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:42 - Unable to determine the type that $key is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:3:34' ,
2021-03-24 21:42:22 +01:00
],
2021-03-28 17:32:38 +02:00
'warnAboutMixedArgumentTypeCoercionSource' => [
2022-01-13 19:49:37 +01:00
'code' => ' < ? php
2021-03-28 17:32:38 +02:00
/** @param array<string> $arr */
function takesArrayOfString ( array $arr ) : void {
foreach ( $arr as $a ) {
echo $a ;
}
}
/** @param mixed $a */
function takesArray ( $a ) : void {
$arr = [ $a ];
takesArrayOfString ( $arr );
} ' ,
2022-12-18 17:15:15 +01:00
'error_message' => 'MixedArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:44 - Argument 1 of takesArrayOfString expects array<array-key, string>, but parent type list{mixed} provided. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:10:41' ,
2021-03-28 17:32:38 +02:00
],
2022-02-13 22:30:06 +01:00
'warnAboutUnusedVariableInTryReassignedInCatch' => [
2022-02-14 00:12:31 +01:00
'code' => ' < ? php
2022-02-13 22:30:06 +01:00
$step = 0 ;
try {
$step = 1 ;
$step = 2 ;
} catch ( Throwable $_ ) {
$step = 3 ;
echo $step ;
}
' ,
'error_message' => 'UnusedVariable' ,
],
'warnAboutUnusedVariableInTryReassignedInFinally' => [
2022-02-14 00:12:31 +01:00
'code' => ' < ? php
2022-02-13 22:30:06 +01:00
$step = 0 ;
try {
$step = 1 ;
$step = 2 ;
} finally {
$step = 3 ;
echo $step ;
}
' ,
'error_message' => 'UnusedVariable' ,
],
'SKIPPED-warnAboutVariableUsedInNestedTryNotUsedInOuterTry' => [
2022-02-14 00:12:31 +01:00
'code' => ' < ? php
2022-02-13 22:30:06 +01:00
$step = 0 ;
try {
$step = 1 ; // Unused
$step = 2 ;
try {
$step = 3 ;
$step = 4 ;
} finally {
echo $step ;
}
} finally {
}
' ,
'error_message' => 'UnusedVariable' ,
],
2022-01-07 23:16:36 +01:00
'referenceReassignmentUnusedVariable' => [
'code' => ' < ? php
$a = $b = 1 ;
$c = & $a ;
$c = & $b ;
$c = 2 ;
echo $a + $b + $c ;
' ,
'error_message' => 'UnusedVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:21 - $c' ,
],
'referenceAssignmentIsNotUsed' => [
'code' => ' < ? php
$a = 1 ;
$b = & $a ;
' ,
'error_message' => 'UnusedVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:21 - $a' ,
],
'unusedReferenceToPreviouslyUsedVariable' => [
'code' => ' < ? php
$a = 1 ;
echo $a ;
$b = & $a ;
' ,
'error_message' => 'UnusedVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:21 - $b' ,
],
'SKIPPED-unusedReferenceToSubsequentlyUsedVariable' => [ // Not easy to do the way it's currently set up
'code' => ' < ? php
$a = 1 ;
$b = & $a ;
echo $a ;
' ,
'error_message' => 'UnusedVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:21 - $b' ,
],
'unusedReferenceInForeach' => [
'code' => ' < ? php
foreach ([ 1 , 2 , 3 ] as & $var ) {
}
' ,
'error_message' => 'UnusedForeachValue' ,
],
'SKIPPED-unusedReferenceInDestructuredForeach' => [
'code' => ' < ? php
foreach ([[ 1 , 2 ], [ 3 , 4 ]] as [ & $var , $_ ]) {
}
' ,
'error_message' => 'UnusedForeachValue' ,
],
'unusedReturnByReference' => [
'code' => ' < ? php
function & foo () : int
{
/** @var ?int */
static $i ;
if ( $i === null ) {
$i = 0 ;
}
return $i ;
}
$bar = foo ();
' ,
'error_message' => 'UnusedVariable' ,
],
'unusedPassByReference' => [
'code' => ' < ? php
function foo ( int & $arg ) : int
{
return 0 ;
}
' ,
'error_message' => 'UnusedParam' ,
],
'SKIPPED-unusedGlobalVariable' => [
'code' => ' < ? php
$a = 0 ;
function foo () : void
{
global $a ;
}
' ,
'error_message' => 'UnusedVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:21 - $a' ,
],
'unusedUndeclaredGlobalVariable' => [
'code' => ' < ? php
function foo () : void
{
global $a ;
}
' ,
'error_message' => 'UnusedVariable' ,
],
2023-04-10 01:53:45 +02:00
'reportWillReportFloatAsItIsAfterRequiredParameterAndUnused' => [
'code' => ' < ? php
/** @param callable(string,int,bool,mixed,float): void $callable */
function takesCallable ( callable $callable ) : void
{
/** @var mixed $mixed */
$mixed = null ;
$callable ( " foo " , 0 , true , $mixed , 0.0 );
}
takesCallable (
static function ( string $foo , int $bar , $float ) {
if ( $bar === 0 ) {
throw new RuntimeException ();
}
}
); ' ,
'error_message' => 'Param float is never referenced in this method' ,
],
2018-06-17 02:01:33 +02:00
];
}
}