2018-06-17 02:01:33 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
2018-11-12 16:57:05 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2018-06-17 02:01:33 +02:00
|
|
|
|
|
|
|
class UnusedVariableTest extends TestCase
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
/** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $project_analyzer;
|
2018-06-17 02:01:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2018-06-17 02:01:33 +02:00
|
|
|
|
|
|
|
$this->file_provider = new Provider\FakeFileProvider();
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
|
2018-06-17 02:01:33 +02:00
|
|
|
new TestConfig(),
|
2018-11-06 03:57:36 +01:00
|
|
|
new \Psalm\Internal\Provider\Providers(
|
2018-09-28 22:18:45 +02:00
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
)
|
2018-06-17 02:01:33 +02:00
|
|
|
);
|
|
|
|
|
2019-02-07 21:27:43 +01:00
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
2019-03-05 21:45:09 +01:00
|
|
|
$this->project_analyzer->getCodebase()->reportUnusedVariables();
|
2018-06-17 02:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @dataProvider providerValidCodeParse
|
2018-06-17 02:01:33 +02:00
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @param array<string> $error_levels
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidCode($code, array $error_levels = [])
|
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
$test_name = $this->getTestName();
|
2019-02-07 18:25:57 +01:00
|
|
|
if (strpos($test_name, 'SKIPPED-') !== false) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($error_levels as $error_level) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
2018-06-17 02:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
$context->collect_references = true;
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @dataProvider providerInvalidCodeParse
|
2018-06-17 02:01:33 +02:00
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @param string $error_message
|
|
|
|
* @param array<string> $error_levels
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidCode($code, $error_message, $error_levels = [])
|
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
if (strpos($this->getTestName(), 'SKIPPED-') !== false) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:22:39 +01:00
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2019-01-12 16:52:23 +01:00
|
|
|
$this->expectExceptionMessageRegExp('/\b' . preg_quote($error_message, '/') . '\b/');
|
2018-06-17 02:01:33 +02:00
|
|
|
|
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
|
|
|
foreach ($error_levels as $error_level) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
2018-06-17 02:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
$file_path,
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
$context->collect_references = true;
|
|
|
|
|
|
|
|
$this->analyzeFile($file_path, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-23 22:22:39 +01:00
|
|
|
* @return array<string, array{string,error_levels?:string[]}
|
2018-06-17 02:01:33 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2018-06-17 02:01:33 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'arrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
|
|
|
|
$arr = ["hello"];
|
|
|
|
|
|
|
|
echo $arr[$a];',
|
|
|
|
],
|
|
|
|
'unset' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
|
|
|
|
$arr = ["hello"];
|
|
|
|
|
|
|
|
unset($arr[$a]);',
|
|
|
|
],
|
|
|
|
'usedVariables' => [
|
|
|
|
'<?php
|
|
|
|
/** @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);
|
|
|
|
}',
|
|
|
|
'error_levels' => [
|
|
|
|
'PossiblyUndefinedVariable',
|
|
|
|
'MixedArrayAccess',
|
|
|
|
'MixedOperand',
|
|
|
|
'MixedAssignment',
|
2018-11-12 18:03:55 +01:00
|
|
|
'InvalidStringClass',
|
2018-06-17 02:01:33 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'varDefinedInIfWithReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$b = "hello";
|
|
|
|
} else {
|
|
|
|
$b = "goodbye";
|
|
|
|
}
|
|
|
|
echo $a . $b;',
|
|
|
|
],
|
|
|
|
'varRedefinedInIfWithReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = (string) "fdf";
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = (string) "ard";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'byrefInForeachLoopWithReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = [1, 2, 3];
|
|
|
|
foreach ($a as &$b) {
|
|
|
|
$b = $b + 1;
|
|
|
|
}
|
|
|
|
echo $a[0];',
|
|
|
|
],
|
2018-06-17 03:14:19 +02:00
|
|
|
'foreachVarSetInValue' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
$a = null;
|
|
|
|
foreach ($arr as $a) { }
|
|
|
|
if ($a) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-06-17 02:01:33 +02:00
|
|
|
'definedInSecondBranchOfCondition' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1) && $a = rand(0, 1)) {
|
|
|
|
echo $a;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'booleanOr' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $a, int $b): bool {
|
|
|
|
return $a || $b;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'paramUsedInIf' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a): void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'dummyByRefVar' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string &$a = null, string $b = null): void {
|
|
|
|
if ($a) {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
if ($b) {
|
|
|
|
echo $b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(): void {
|
|
|
|
foo($dummy_byref_var, "hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
bar();',
|
|
|
|
],
|
|
|
|
'foreachReassigned' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $b) {
|
|
|
|
$a = true;
|
|
|
|
echo $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'doWhileReassigned' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
|
|
|
|
do {
|
|
|
|
echo $a;
|
|
|
|
$a = $a - rand(-3, 3);
|
|
|
|
} while ($a > 3);',
|
|
|
|
],
|
|
|
|
'loopTypeChangedInIfAndContinueWithReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'loopReassignedInIfAndContinueWithReferenceAfter' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 7;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'loopReassignedInIfAndContinueWithReferenceBeforeAndAfter' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
while(rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'ifVarReassignedInBranchWithUse' => [
|
|
|
|
'<?php
|
|
|
|
$a = true;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
echo "cool";
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'elseVarReassignedInBranchAndReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a = true;
|
|
|
|
//echo $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
echo "cool";
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'switchVarReassignedInBranch' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
switch (rand(0, 2)) {
|
|
|
|
case 0:
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
echo "cool";
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'switchVarDefinedInAllBranches' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
2018-06-17 03:54:44 +02:00
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function dangerous(): void {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
function callDangerous(): void {
|
|
|
|
try {
|
|
|
|
dangerous();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'throwWithMessageCallAndAssignmentAndReference' => [
|
|
|
|
'<?php
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($s) {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'throwWithMessageCallAndAssignmentInCatchAndReference' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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();
|
|
|
|
$s = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($s) {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'throwWithMessageCallAndNestedAssignmentInTryAndCatchAndReference' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
$i = false;
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $a) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$i = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i) {}',
|
|
|
|
],
|
|
|
|
'unknownMethodCallWithVar' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress MixedMethodCall */
|
|
|
|
function passesByRef(object $a): void {
|
|
|
|
$a->passedByRef($b);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'usedMethodCallVariable' => [
|
|
|
|
'<?php
|
|
|
|
function reindex(array $arr, string $methodName): array {
|
|
|
|
$ret = [];
|
|
|
|
|
|
|
|
foreach ($arr as $element) {
|
|
|
|
$ret[$element->$methodName()] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}',
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
|
|
|
'MixedMethodCall',
|
|
|
|
'MixedArrayOffset',
|
2019-01-05 06:15:53 +01:00
|
|
|
'MixedTypeCoercion',
|
2018-06-17 02:01:33 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'globalVariableUsage' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
|
|
|
function example() : void {
|
|
|
|
global $a;
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
example();',
|
|
|
|
],
|
|
|
|
'staticVar' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'loopReassignedInIfAndBreakWithReferenceAfter' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 7;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'loopSetIfNullWithBreakAndReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if ($a !== null) {
|
|
|
|
$a = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'loopSetIfNullWithContinueAndReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if ($a !== null) {
|
|
|
|
$a = 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'loopAssignmentAfterReferenceSimple' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
echo $a;
|
|
|
|
$a = 1;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'loopAssignmentAfterReferenceWithContinue' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
echo $a;
|
|
|
|
$a = 1;
|
|
|
|
continue;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'loopAssignmentAfterReferenceWithConditionalAssignmentWithContinue' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
echo $a;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'loopAssignmentAfterReferenceWithContinueInIf' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
echo $a;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'loopAssignmentAfterReferenceWithContinueInSwitch' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
2018-06-17 02:01:33 +02:00
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
switch (rand(0, 1)) {
|
|
|
|
case 0:
|
|
|
|
$a = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
],
|
|
|
|
'listVarAssignmentInIf' => [
|
|
|
|
'<?php
|
|
|
|
$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
|
|
|
],
|
|
|
|
'varCheckAfterNestedAssignmentAndBreak' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
],
|
|
|
|
'varCheckAfterNestedAssignmentAndBreakInIf' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
],
|
|
|
|
'breakInSwitchStatementIf' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
switch (rand(0, 1)) {
|
|
|
|
default:
|
|
|
|
echo $a;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'breakInSwitchStatementIfWithSecondCase' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i) : void {
|
|
|
|
echo $i;
|
|
|
|
}
|
|
|
|
$a = 5;
|
|
|
|
foo($a += 1);',
|
|
|
|
],
|
|
|
|
'echoVarWithIncrement' => [
|
|
|
|
'<?php
|
|
|
|
function foo(int $i) : void {
|
|
|
|
echo $i;
|
|
|
|
}
|
|
|
|
$a = 5;
|
|
|
|
foo(++$a);',
|
|
|
|
],
|
2018-06-26 01:38:15 +02:00
|
|
|
'afterMethodExistsCheck' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo { }
|
|
|
|
|
|
|
|
function returnFoo(): Foo { return new class implements Foo { }; }
|
|
|
|
|
|
|
|
$interface = Foo::class;
|
|
|
|
|
|
|
|
if (returnFoo() instanceof $interface) {
|
|
|
|
exit;
|
|
|
|
}',
|
|
|
|
],
|
2018-06-30 17:08:51 +02:00
|
|
|
'usedVariableInDoWhile' => [
|
|
|
|
'<?php
|
|
|
|
$i = 5;
|
|
|
|
do {
|
|
|
|
echo "hello";
|
|
|
|
} while (--$i > 0);
|
|
|
|
echo $i;',
|
|
|
|
],
|
2018-10-04 22:42:40 +02:00
|
|
|
'callableReferencesItself' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress UnusedParam */
|
|
|
|
function foo(callable $c) : void {}
|
|
|
|
$listener = function () use (&$listener) : void {
|
|
|
|
/** @psalm-suppress MixedArgument */
|
|
|
|
foo($listener);
|
|
|
|
};
|
|
|
|
foo($listener);',
|
|
|
|
],
|
2019-02-24 15:58:11 +01:00
|
|
|
'newVariableConstructor' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param class-string<ArrayObject> $type
|
|
|
|
*/
|
|
|
|
function bar(string $type) : ArrayObject {
|
|
|
|
$data = [["foo"], ["bar"]];
|
|
|
|
|
|
|
|
return new $type($data[0]);
|
|
|
|
}',
|
|
|
|
],
|
2019-03-10 21:36:35 +01:00
|
|
|
'byRefVariableUsedInAddition' => [
|
|
|
|
'<?php
|
|
|
|
$i = 0;
|
|
|
|
$a = function () use (&$i) : void {
|
|
|
|
$i = 1;
|
|
|
|
};
|
|
|
|
$a();',
|
|
|
|
],
|
2019-03-20 04:16:00 +01:00
|
|
|
'superGlobalInFunction' => [
|
|
|
|
'<?php
|
|
|
|
function example1() : void {
|
|
|
|
$_SESSION = [];
|
|
|
|
}
|
|
|
|
function example2() : int {
|
|
|
|
return (int) $_SESSION["str"];
|
|
|
|
}',
|
|
|
|
],
|
2019-03-28 03:13:06 +01:00
|
|
|
'usedInArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedMethodCall
|
|
|
|
* @psalm-suppress MissingParamType
|
|
|
|
*/
|
|
|
|
function foo($a) : void {
|
|
|
|
$b = "b";
|
|
|
|
$a->bar([$b]);
|
|
|
|
}',
|
|
|
|
],
|
2018-06-17 02:01:33 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-23 22:22:39 +01:00
|
|
|
* @return array<string,array{string,error_message:string}>
|
2018-06-17 02:01:33 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2018-06-17 02:01:33 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'simpleUnusedVariable' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
$b = [];
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
2018-06-17 05:40:25 +02:00
|
|
|
'unusedVarWithAdditionOp' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
$a += 1;',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedVarWithConditionalAdditionOp' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a += 1;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedVarWithConditionalAddition' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = $a + 1;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedVarWithIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
$a++;',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedVarWithConditionalIncrement' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a++;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
2018-06-17 02:01:33 +02:00
|
|
|
'ifInBothBranchesWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$b = "hello";
|
|
|
|
} else {
|
|
|
|
$b = "goodbye";
|
|
|
|
}
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'varInNestedAssignmentWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "foo";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'varInSecondNestedAssignmentWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "foo";
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "foo";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'varReassignedInBothBranchesOfIf' => [
|
|
|
|
'<?php
|
|
|
|
$a = "foo";
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "bar";
|
|
|
|
} else {
|
|
|
|
$a = "bat";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'varReassignedInNestedBranchesOfIf' => [
|
|
|
|
'<?php
|
|
|
|
$a = "foo";
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "bar";
|
|
|
|
} else {
|
|
|
|
$a = "bat";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$a = "bang";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'ifVarReassignedInBranchWithNoUse' => [
|
|
|
|
'<?php
|
|
|
|
$a = true;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = false;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'elseVarReassignedInBranchAndNoReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = true;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a = false;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'switchVarReassignedInBranch' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
switch (rand(0, 2)) {
|
|
|
|
case 0:
|
|
|
|
$a = true;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'switchVarReassignedInBranchWithDefault' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
switch (rand(0, 2)) {
|
|
|
|
case 0:
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$a = false;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'switchVarReassignedInAllBranches' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
switch (rand(0, 2)) {
|
|
|
|
case 0:
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$a = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
echo "cool";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedListVar' => [
|
|
|
|
'<?php
|
|
|
|
list($a, $b) = explode(" ", "hello world");
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedPreForVar' => [
|
|
|
|
'<?php
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i < 10; $i++) {
|
|
|
|
echo $i;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedIfInReturnBlock' => [
|
|
|
|
'<?php
|
|
|
|
$i = rand(0, 1);
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $a) {
|
|
|
|
if ($a % 2) {
|
|
|
|
$i = 7;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i) {}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'unusedIfVarInBranch' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "foo";
|
|
|
|
} else {
|
|
|
|
$a = "bar";
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'throwWithMessageCallAndAssignmentAndNoReference' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'loopTypeChangedInIfAndContinueWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = false;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'loopReassignedInIfAndContinueWithoutReferenceAfter' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 7;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 3;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'loopReassignedInIfAndContinueWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = $a + 5;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'varDefinedInIfWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$b = "hello";
|
|
|
|
} else {
|
|
|
|
$b = "goodbye";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'SKIPPED-byrefInForeachLoopWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = [1, 2, 3];
|
|
|
|
foreach ($a as &$b) {
|
|
|
|
$b = $b + 1;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'loopSetIfNullWithBreakWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
2018-06-17 02:01:33 +02:00
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 5;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'loopSetIfNullWithContinueWithoutReference' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 5;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'loopAssignmentAfterReferenceWithBreak' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
echo $a;
|
|
|
|
$a = 1;
|
|
|
|
break;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
|
|
|
'loopAssignmentAfterReferenceWithBreakInIf' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
$key = "a";
|
|
|
|
echo $key;
|
|
|
|
|
|
|
|
$arr = ["foo" => "foo.foo"];
|
|
|
|
|
|
|
|
foreach ($arr as $key => $v) {
|
|
|
|
list($key) = explode(".", $v);
|
|
|
|
echo $key;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnusedVariable',
|
|
|
|
],
|
2018-06-17 02:01:33 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|