2019-05-02 18:15:38 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-05-02 18:15:38 +02:00
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
2021-01-11 23:30:33 +01:00
|
|
|
class UndefinedVariableManipulationTest extends FileManipulationTestCase
|
2019-05-02 18:15:38 +02:00
|
|
|
{
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2019-05-02 18:15:38 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'possiblyUndefinedVariable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
$flag = rand(0, 1);
|
|
|
|
$otherflag = rand(0, 1);
|
|
|
|
$yetanotherflag = rand(0, 1);
|
|
|
|
|
|
|
|
if ($flag) {
|
|
|
|
if ($otherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($flag) {
|
|
|
|
if ($yetanotherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
$flag = rand(0, 1);
|
|
|
|
$otherflag = rand(0, 1);
|
|
|
|
$yetanotherflag = rand(0, 1);
|
|
|
|
|
|
|
|
$a = null;
|
|
|
|
if ($flag) {
|
|
|
|
if ($otherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($flag) {
|
|
|
|
if ($yetanotherflag) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '5.6',
|
|
|
|
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
|
|
|
'twoPossiblyUndefinedVariables' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 1;
|
|
|
|
$b = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
echo $b;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
$a = null;
|
|
|
|
$b = null;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 1;
|
|
|
|
$b = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
echo $b;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '5.6',
|
|
|
|
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
|
|
|
'possiblyUndefinedVariableInElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
$a = null;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '5.6',
|
|
|
|
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
|
|
|
'unsetPossiblyUndefinedVariable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "bar";
|
|
|
|
}
|
|
|
|
unset($a);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = "bar";
|
|
|
|
}
|
|
|
|
unset($a);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '5.6',
|
|
|
|
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
|
|
|
'useUnqualifierPlugin' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
namespace A\B\C {
|
|
|
|
class D {}
|
|
|
|
}
|
|
|
|
namespace Foo\Bar {
|
|
|
|
use A\B\C\D;
|
|
|
|
|
|
|
|
new \A\B\C\D();
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2019-05-02 18:15:38 +02:00
|
|
|
namespace A\B\C {
|
|
|
|
class D {}
|
|
|
|
}
|
|
|
|
namespace Foo\Bar {
|
|
|
|
use A\B\C\D;
|
|
|
|
|
|
|
|
new D();
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => [],
|
|
|
|
'safe_types' => true,
|
2019-05-02 18:15:38 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|