1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 09:19:40 +01:00
psalm/tests/FileManipulation/UndefinedVariableManipulationTest.php

144 lines
4.2 KiB
PHP
Raw Normal View History

2019-05-02 18:15:38 +02:00
<?php
2023-10-19 13:12:06 +02:00
declare(strict_types=1);
2019-05-02 18:15:38 +02:00
namespace Psalm\Tests\FileManipulation;
class UndefinedVariableManipulationTest extends FileManipulationTestCase
2019-05-02 18:15:38 +02:00
{
public function providerValidCodeParse(): array
2019-05-02 18:15:38 +02:00
{
return [
'possiblyUndefinedVariable' => [
'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;
}',
'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;
}',
'php_version' => '5.6',
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'twoPossiblyUndefinedVariables' => [
'input' => '<?php
2019-05-02 18:15:38 +02:00
if (rand(0, 1)) {
$a = 1;
$b = 2;
}
echo $a;
echo $b;',
'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;',
'php_version' => '5.6',
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'possiblyUndefinedVariableInElse' => [
'input' => '<?php
2019-05-02 18:15:38 +02:00
if (rand(0, 1)) {
// do nothing
} else {
$a = 5;
}
echo $a;',
'output' => '<?php
2019-05-02 18:15:38 +02:00
$a = null;
if (rand(0, 1)) {
// do nothing
} else {
$a = 5;
}
echo $a;',
'php_version' => '5.6',
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'unsetPossiblyUndefinedVariable' => [
'input' => '<?php
2019-05-02 18:15:38 +02:00
if (rand(0, 1)) {
$a = "bar";
}
unset($a);',
'output' => '<?php
2019-05-02 18:15:38 +02:00
if (rand(0, 1)) {
$a = "bar";
}
unset($a);',
'php_version' => '5.6',
'issues_to_fix' => ['PossiblyUndefinedGlobalVariable'],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
'useUnqualifierPlugin' => [
'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();
}',
'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();
}',
'php_version' => '7.4',
'issues_to_fix' => [],
'safe_types' => true,
2019-05-02 18:15:38 +02:00
],
];
}
}