1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/FileManipulation/UnusedVariableManipulationTest.php
Daniil Gentili 1986c8b4a8
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395)
* Immutable CodeLocation

* Remove excess clones

* Remove external clones

* Remove leftover clones

* Fix final clone issue

* Immutable storages

* Refactoring

* Fixes

* Fixes

* Fix

* Fix

* Fixes

* Simplify

* Fixes

* Fix

* Fixes

* Update

* Fix

* Cache global types

* Fix

* Update

* Update

* Fixes

* Fixes

* Refactor

* Fixes

* Fix

* Fix

* More caching

* Fix

* Fix

* Update

* Update

* Fix

* Fixes

* Update

* Refactor

* Update

* Fixes

* Break one more test

* Fix

* FIx

* Fix

* Fix

* Fix

* Fix

* Improve performance and readability

* Equivalent logic

* Fixes

* Revert

* Revert "Revert"

This reverts commit f9175100c8452c80559234200663fd4c4f4dd889.

* Fix

* Fix reference bug

* Make default TypeVisitor immutable

* Bugfix

* Remove clones

* Partial refactoring

* Refactoring

* Fixes

* Fix

* Fixes

* Fixes

* cs-fix

* Fix final bugs

* Add test

* Misc fixes

* Update

* Fixes

* Experiment with removing different property

* revert "Experiment with removing different property"

This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9.

* Uniform naming

* Uniform naming

* Hack hotfix

* Clean up $_FILES ref #8621

* Undo hack, try fixing properly

* Helper method

* Remove redundant call

* Partially fix bugs

* Cleanup

* Change defaults

* Fix bug

* Fix (?, hope this doesn't break anything else)

* cs-fix

* Review fixes

* Bugfix

* Bugfix

* Improve logic

* Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389)

* Default to sealed arrays

* Fix array_merge bug

* Fixes

* Fix

* Sealed type checks

* Properly infer properties-of and get_object_vars on final classes

* Fix array_map zipping

* Fix tests

* Fixes

* Fixes

* Fix more stuff

* Recursively resolve type aliases

* Fix typo

* Fixes

* Fix array_is_list assertion on keyed array

* Add BC docs

* Fixes

* fix

* Update

* Update

* Update

* Update

* Seal arrays with count assertions

* Fix #8528

* Fix

* Update

* Improve sealed array foreach logic

* get_object_vars on template properties

* Fix sealed array assertion reconciler logic

* Improved reconciler

* Add tests

* Single source of truth for test types

* Fix tests

* Fixup tests

* Fixup tests

* Fixup tests

* Update

* Fix tests

* Fix tests

* Final fixes

* Fixes

* Use list syntax only when needed

* Fix tests

* Cs-fix

* Update docs

* Update docs

* Update docs

* Update docs

* Update docs

* Document missing types

* Update docs

* Improve class-string-map docs

* Update

* Update

* I love working on psalm :)

* Keep arrays unsealed by default

* Fixup tests

* Fix syntax mistake

* cs-fix

* Fix typo

* Re-import missing types

* Keep strict types only in return types

* argc/argv fixes

* argc/argv fixes

* Fix test

* Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00

681 lines
23 KiB
PHP

<?php
namespace Psalm\Tests\FileManipulation;
class UnusedVariableManipulationTest extends FileManipulationTestCase
{
public function providerValidCodeParse(): array
{
return [
'removeUnusedVariableSimple' => [
'input' => '<?php
class A {
public function foo() : void {
$a = 5;
$b = "hello";
echo $b;
}
}',
'output' => '<?php
class A {
public function foo() : void {
$b = "hello";
echo $b;
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableFromTry' => [
'input' => '<?php
class A {
public function foo() : void {
try {
$c = false;
$d = null;
} catch (Exception $e) {}
}
}',
'output' => '<?php
class A {
public function foo() : void {
try {
} catch (Exception $e) {}
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableAndFunctionCall' => [
'input' => '<?php
class A {
public function foo() : void {
$a = substr("wonderful", 2);
$b = "hello";
echo $b;
}
}',
'output' => '<?php
class A {
public function foo() : void {
$b = "hello";
echo $b;
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableTwoVar' => [
'input' => '<?php
class A {
public function foo() : void {
$a = "a";
$b = "b";
$c = "c";
echo $b;
}
}',
'output' => '<?php
class A {
public function foo() : void {
$b = "b";
echo $b;
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableTwoVarFunctionCalls' => [
'input' => '<?php
class A {
public function foo() : void {
$a = substr("hello world", 4);
$b = "b";
$c = file_get_contents("foo.php");
echo $b;
}
}',
'output' => '<?php
class A {
public function foo() : void {
$b = "b";
file_get_contents("foo.php");
echo $b;
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableClassMethod' => [
'input' => '<?php
class A {
public function foo() : void {
$a =bar();
$b = "b";
echo $b;
}
public function bar() : void {
;
}
}',
'output' => '<?php
class A {
public function foo() : void {
bar();
$b = "b";
echo $b;
}
public function bar() : void {
;
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableFunctionCallAndStrLiteral' => [
'input' => '<?php
class A {
public function foo() : void {
$a = "hello".bar();
$b = "world";
echo $b;
}
public function bar() : string {
return "bar";
}
}',
'output' => '<?php
class A {
public function foo() : void {
"hello".bar();
$b = "world";
echo $b;
}
public function bar() : string {
return "bar";
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableChainAssignment' => [
'input' => '<?php
class A {
public function foo() : void {
$a = $b = $c = $d = $e = "";
echo $a.$b.$d.$e;
}
}',
'output' => '<?php
class A {
public function foo() : void {
$a = $b = $d = $e = "";
echo $a.$b.$d.$e;
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeTwoUnusedVariableChainAssignment' => [
'input' => '<?php
class A {
public function foo() : void {
$a = $b = $c = $d = $e = "hello";
echo $a.$d.$e;
}
}',
'output' => '<?php
class A {
public function foo() : void {
$a = $d = $e = "hello";
echo $a.$d.$e;
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeAllUnusedVariableChainAssignment' => [
'input' => '<?php
class A {
public function foo() : void {
$a = $b = $c = $d = $e = "hello";
echo "hello";
}
}',
'output' => '<?php
class A {
public function foo() : void {
echo "hello";
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedArrayAccess' => [
'input' => '<?php
function foo($b) : void {
$a = $b[1];
}',
'output' => '<?php
function foo($b) : void {
$b[1];
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeEmptyArrayAssign' => [
'input' => '<?php
function foo($b) : void {
$a = [];
echo "foo";
}',
'output' => '<?php
function foo($b) : void {
echo "foo";
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedArrayAssignInt' => [
'input' => '<?php
function foo($b) : void {
$a = [5];
echo "foo";
}',
'output' => '<?php
function foo($b) : void {
echo "foo";
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedArrayAssignCallable' => [
'input' => '<?php
function foo($b) : void {
$a = [foo()];
echo "foo";
}',
'output' => '<?php
function foo($b) : void {
[foo()];
echo "foo";
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVarShellExec' => [
'input' => '<?php
function foo() : void {
$a = shell_exec("ls");
}',
'output' => '<?php
function foo() : void {
shell_exec("ls");
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVarExit' => [
'input' => '<?php
function foo() : void {
$a = exit(1);
}',
'output' => '<?php
function foo() : void {
exit(1);
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVarTwoPass' => [
'input' => '<?php
function foo() : void {
$a = 5;
$a += 1;
}',
'output' => '<?php
function foo() : void {
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'SKIPPED-removeUnusedVarAssignByRefToSubsequentlyUsedVariable' => [
'input' => '<?php
function foo() : void {
$a = 5;
$b = &$a;
echo $a;
}',
'output' => '<?php
function foo() : void {
$a = 5;
echo $a;
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVarAssignByRef' => [
'input' => '<?php
function foo() : void {
$a = 5;
$b = &$a;
}',
'output' => '<?php
function foo() : void {
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVarAssignByRefPartial' => [
'input' => '<?php
function foo() : void {
$a = [1, 2, 3];
$b = &$a[1];
print_r($a);
}',
'output' => '<?php
function foo() : void {
$a = [1, 2, 3];
$a[1];
print_r($a);
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVarAssignByRefPartialWithSpaceAfter' => [
'input' => '<?php
function foo() : void {
$a = [1, 2, 3];
$b = & $a[1];
print_r($a);
}',
'output' => '<?php
function foo() : void {
$a = [1, 2, 3];
$a[1];
print_r($a);
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVarNewObject' => [
'input' => '<?php
class B {}
function foo() : void {
$a = 5;
$b = new B();
echo $a;
}',
'output' => '<?php
class B {}
function foo() : void {
$a = 5;
new B();
echo $a;
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedChainMixedAssign' => [
'input' => '<?php
function foo() : void {
$a = 5;
$b = 6;
$c = $b += $a -= intval("4");
echo "foo";
}',
'output' => '<?php
function foo() : void {
echo "foo";
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedUnchainedAssign' => [
'input' => '<?php
function foo() : void {
$a = 5;
$b = 6;
$a -= intval("4");
$b += $a;
$c = $b;
echo "foo";
}',
'output' => '<?php
function foo() : void {
echo "foo";
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeUnusedVariableBinaryOp' => [
'input' => '<?php
function foo() : void {
$a = 5;
$b = 6;
$c = $a + $b;
echo "foo";
}',
'output' => '<?php
function foo() : void {
echo "foo";
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'dontremoveUnusedVariableFor' => [
'input' => '<?php
function foo($b) : void {
for($i = 5; $j<5; $j++){
echo "abc";
}
}',
'output' => '<?php
function foo($b) : void {
for($i = 5; $j<5; $j++){
echo "abc";
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'dontremoveUnusedVariableWhile' => [
'input' => '<?php
function foo($b) : void {
while($i=5){
echo "abc";
}
}',
'output' => '<?php
function foo($b) : void {
while($i=5){
echo "abc";
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'dontRemoveUnusedVariableInsideIf' => [
'input' => '<?php
class A {
public function foo() : void {
$a = "hello";
if($b = 5) {
echo $a;
}
}
}',
'output' => '<?php
class A {
public function foo() : void {
$a = "hello";
if($b = 5) {
echo $a;
}
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'donRemoveSuppressedUnusedVariable' => [
'input' => '<?php
/** @psalm-suppress UnusedVariable */
function foo() : void {
$a = 5;
$b = "hello";
echo $b;
}',
'output' => '<?php
/** @psalm-suppress UnusedVariable */
function foo() : void {
$a = 5;
$b = "hello";
echo $b;
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'removeLongUnusedAssignment' => [
'input' => '<?php
/**
* @psalm-external-mutation-free
*/
class A {
private string $foo;
public function __construct(string $foo) {
$this->foo = $foo;
}
public function getFoo() : void {
return "abular" . $this->foo;
}
}
/**
* @psalm-pure
*/
function makeA(string $s) : A {
return new A($s);
}
function foo() : void {
$a = makeA("hello")->getFoo();
}',
'output' => '<?php
/**
* @psalm-external-mutation-free
*/
class A {
private string $foo;
public function __construct(string $foo) {
$this->foo = $foo;
}
public function getFoo() : void {
return "abular" . $this->foo;
}
}
/**
* @psalm-pure
*/
function makeA(string $s) : A {
return new A($s);
}
function foo() : void {
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'dontRemoveUsedToStringCall' => [
'input' => '<?php
class S {
/**
* @throws Exception
*/
public function __toString() {
if(rand(0,1)){
throw new exception();
}
return "";
}
}
function foo(S $a) {
try {
$b = (string) $a;
} catch(Exception $e){
// this class is not stringable
}
}',
'output' => '<?php
class S {
/**
* @throws Exception
*/
public function __toString() {
if(rand(0,1)){
throw new exception();
}
return "";
}
}
function foo(S $a) {
try {
(string) $a;
} catch(Exception $e){
// this class is not stringable
}
}',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
'dontRemoveUnusedClosureUse' => [
'input' => '<?php
$b = 5;
echo $b;
$a = function() use ($b) : void {
echo 4;
};
$a();',
'output' => '<?php
$b = 5;
echo $b;
$a = function() use ($b) : void {
echo 4;
};
$a();',
'php_version' => '7.1',
'issues_to_fix' => ['UnusedVariable'],
'safe_types' => true,
],
];
}
}