1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Break apart FileManipulationTest

This commit is contained in:
Brown 2019-05-02 12:15:38 -04:00
parent ae531506de
commit d64ca30633
6 changed files with 2277 additions and 2208 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace Psalm\Tests\FileManipulation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Tests\Internal\Provider;
use Psalm\Tests\TestConfig;
abstract class FileManipulationTest extends \Psalm\Tests\TestCase
{
/** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */
protected $project_analyzer;
/**
* @return void
*/
public function setUp()
{
FileAnalyzer::clearCache();
\Psalm\Internal\FileManipulation\FunctionDocblockManipulator::clearCache();
$this->file_provider = new Provider\FakeFileProvider();
}
/**
* @dataProvider providerValidCodeParse
*
* @param string $input_code
* @param string $output_code
* @param string $php_version
* @param string[] $issues_to_fix
* @param bool $safe_types
*
* @return void
*/
public function testValidCode($input_code, $output_code, $php_version, array $issues_to_fix, $safe_types)
{
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
$config = new TestConfig();
$this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
$config,
new \Psalm\Internal\Provider\Providers(
$this->file_provider,
new Provider\FakeParserCacheProvider()
)
);
if (empty($issues_to_fix)) {
$config->addPluginPath('examples/plugins/ClassUnqualifier.php');
$config->initializePlugins($this->project_analyzer);
}
$context = new Context();
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile(
$file_path,
$input_code
);
$this->project_analyzer->setPhpVersion($php_version);
$keyed_issues_to_fix = [];
foreach ($issues_to_fix as $issue) {
$keyed_issues_to_fix[$issue] = true;
}
$this->project_analyzer->setIssuesToFix($keyed_issues_to_fix);
$this->project_analyzer->alterCodeAfterCompletion(
false,
$safe_types
);
$this->project_analyzer->getCodebase()->reportUnusedCode();
$this->analyzeFile($file_path, $context);
$this->project_analyzer->checkClassReferences();
$this->project_analyzer->getCodebase()->analyzer->updateFile($file_path, false);
$this->assertSame($output_code, $this->project_analyzer->getCodebase()->getFileContents($file_path));
}
/**
* @return array<string,array{string,string,string,string[],bool}>
*/
abstract public function providerValidCodeParse();
}

View File

@ -0,0 +1,333 @@
<?php
namespace Psalm\Tests\FileManipulation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Tests\Internal\Provider;
class ParamTypeManipulationTest extends FileManipulationTest
{
/**
* @return array<string,array{string,string,string,string[],bool}>
*/
public function providerValidCodeParse()
{
return [
'fixMismatchingDocblockParamType70' => [
'<?php
/**
* @param int $s
*/
function foo(string $s): string {
return "hello";
}',
'<?php
/**
* @param string $s
*/
function foo(string $s): string {
return "hello";
}',
'7.0',
['MismatchingDocblockParamType'],
true,
],
'fixNamespacedMismatchingDocblockParamsType70' => [
'<?php
namespace Foo\Bar {
class A {
/**
* @param \B $b
* @param \C $c
*/
function foo(B $b, C $c): string {
return "hello";
}
}
class B {}
class C {}
}',
'<?php
namespace Foo\Bar {
class A {
/**
* @param B $b
* @param C $c
*/
function foo(B $b, C $c): string {
return "hello";
}
}
class B {}
class C {}
}',
'7.0',
['MismatchingDocblockParamType'],
true,
],
'noStringParamType' => [
'<?php
function fooFoo($a): void {
echo substr($a, 4, 2);
}',
'<?php
/**
* @param string $a
*/
function fooFoo($a): void {
echo substr($a, 4, 2);
}',
'7.1',
['MissingParamType'],
true,
],
'stringParamTypeAndConcatNoop' => [
'<?php
function fooFoo(string $a): void {
echo $a . "foo";
}',
'<?php
function fooFoo(string $a): void {
echo $a . "foo";
}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButConcat' => [
'<?php
function fooFoo($a): void {
echo $a . "foo";
}',
'<?php
/**
* @param string|int $a
*/
function fooFoo($a): void {
echo $a . "foo";
}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButConcatAndStringUsage' => [
'<?php
function fooFoo($a): void {
echo $a . "foo";
echo substr($a, 4, 2);
}',
'<?php
/**
* @param string $a
*/
function fooFoo($a): void {
echo $a . "foo";
echo substr($a, 4, 2);
}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButConcatAndStringUsageReversed' => [
'<?php
function fooFoo($a): void {
echo substr($a, 4, 2);
echo $a . "foo";
}',
'<?php
/**
* @param string $a
*/
function fooFoo($a): void {
echo substr($a, 4, 2);
echo $a . "foo";
}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButAddition' => [
'<?php
function fooFoo($a): void {
echo $a + 5;
}',
'<?php
/**
* @param int|float $a
*/
function fooFoo($a): void {
echo $a + 5;
}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButAdditionAndDefault' => [
'<?php
function fooFoo($a = 5): void {
takesInt($a);
}
function takesInt(int $i) {}',
'<?php
/**
* @param int $a
*/
function fooFoo($a = 5): void {
takesInt($a);
}
function takesInt(int $i) {}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButIntUseAndNullCheck' => [
'<?php
function fooFoo($a): void {
if ($a === null) {
return;
}
takesInt($a);
}
function takesInt(int $i) {}',
'<?php
/**
* @param null|int $a
*/
function fooFoo($a): void {
if ($a === null) {
return;
}
takesInt($a);
}
function takesInt(int $i) {}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButDivision' => [
'<?php
function fooFoo($a): void {
echo $a / 5;
}',
'<?php
/**
* @param int|float $a
*/
function fooFoo($a): void {
echo $a / 5;
}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButTemplatedString' => [
'<?php
function fooFoo($a): void {
echo "$a";
}',
'<?php
/**
* @param string|int|float $a
*/
function fooFoo($a): void {
echo "$a";
}',
'7.1',
['MissingParamType'],
true,
],
'noParamTypeButTemplatedStringAntStringUsage' => [
'<?php
function fooFoo($a): void {
echo "$a";
echo substr($a, 4, 2);
}',
'<?php
/**
* @param string $a
*/
function fooFoo($a): void {
echo "$a";
echo substr($a, 4, 2);
}',
'7.1',
['MissingParamType'],
true,
],
'noStringIntParamType' => [
'<?php
function fooFoo($a): void {
if (is_string($a)) {
echo substr($a, 4, 2);
} else {
echo substr("hello", $a, 2);
}
}',
'<?php
/**
* @param int|string $a
*/
function fooFoo($a): void {
if (is_string($a)) {
echo substr($a, 4, 2);
} else {
echo substr("hello", $a, 2);
}
}',
'7.1',
['MissingParamType'],
true,
],
'alreadyHasCheck' => [
'<?php
function takesString(string $s): void {}
function shouldTakeString($s): void {
if (is_string($s)) {
takesString($s);
}
}',
'<?php
function takesString(string $s): void {}
function shouldTakeString($s): void {
if (is_string($s)) {
takesString($s);
}
}',
'7.1',
['MissingParamType'],
true,
],
'isSetBeforeInferrence' => [
'<?php
function takesString(string $s): void {}
/** @return mixed */
function returnsMixed() {}
function shouldTakeString($s): void {
$s = returnsMixed();
takesString($s);
}',
'<?php
function takesString(string $s): void {}
/** @return mixed */
function returnsMixed() {}
function shouldTakeString($s): void {
$s = returnsMixed();
takesString($s);
}',
'7.1',
['MissingParamType'],
true,
],
];
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,147 @@
<?php
namespace Psalm\Tests\FileManipulation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Tests\Internal\Provider;
class UndefinedVariableManipulationTest extends FileManipulationTest
{
/**
* @return array<string,array{string,string,string,string[],bool}>
*/
public function providerValidCodeParse()
{
return [
'possiblyUndefinedVariable' => [
'<?php
$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;
}',
'<?php
$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;
}',
'5.6',
['PossiblyUndefinedGlobalVariable'],
true,
],
'twoPossiblyUndefinedVariables' => [
'<?php
if (rand(0, 1)) {
$a = 1;
$b = 2;
}
echo $a;
echo $b;',
'<?php
$a = null;
$b = null;
if (rand(0, 1)) {
$a = 1;
$b = 2;
}
echo $a;
echo $b;',
'5.6',
['PossiblyUndefinedGlobalVariable'],
true,
],
'possiblyUndefinedVariableInElse' => [
'<?php
if (rand(0, 1)) {
// do nothing
} else {
$a = 5;
}
echo $a;',
'<?php
$a = null;
if (rand(0, 1)) {
// do nothing
} else {
$a = 5;
}
echo $a;',
'5.6',
['PossiblyUndefinedGlobalVariable'],
true,
],
'unsetPossiblyUndefinedVariable' => [
'<?php
if (rand(0, 1)) {
$a = "bar";
}
unset($a);',
'<?php
if (rand(0, 1)) {
$a = "bar";
}
unset($a);',
'5.6',
['PossiblyUndefinedGlobalVariable'],
true,
],
'useUnqualifierPlugin' => [
'<?php
namespace A\B\C {
class D {}
}
namespace Foo\Bar {
use A\B\C\D;
new \A\B\C\D();
}',
'<?php
namespace A\B\C {
class D {}
}
namespace Foo\Bar {
use A\B\C\D;
new D();
}',
PHP_VERSION,
[],
true,
],
];
}
}

View File

@ -0,0 +1,660 @@
<?php
namespace Psalm\Tests\FileManipulation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Tests\Internal\Provider;
class UnusedCodeManipulationTest extends FileManipulationTest
{
/**
* @return array<string,array{string,string,string,string[],bool}>
*/
public function providerValidCodeParse()
{
return [
'removePossiblyUnusedMethod' => [
'<?php
class A {
public function foo() : void {}
public function bar() : void {}
}
(new A)->foo();',
'<?php
class A {
public function foo() : void {}
}
(new A)->foo();',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removePossiblyUnusedMethodInMiddle' => [
'<?php
class A {
public function foo() : void {}
public function bar() : void {}
public function bat() : void {}
}
(new A)->foo();
(new A)->bat();',
'<?php
class A {
public function foo() : void {}
public function bat() : void {}
}
(new A)->foo();
(new A)->bat();',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removeAllPossiblyUnusedMethods' => [
'<?php
class A {
public function foo() : void {}
public function bar() : void {}
public function bat() : void {}
}
new A();',
'<?php
class A {
}
new A();',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithMixedUse' => [
'<?php
class A {
public function foo() : void {}
}
function foo($a) {
$a->foo();
}',
'<?php
class A {
public function foo() : void {}
}
function foo($a) {
$a->foo();
}',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithSuppression' => [
'<?php
class A {
public function foo() : void {}
/** @psalm-suppress PossiblyUnusedMethod */
public function bar() : void {}
}
(new A)->foo();',
'<?php
class A {
public function foo() : void {}
/** @psalm-suppress PossiblyUnusedMethod */
public function bar() : void {}
}
(new A)->foo();',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removeUnusedMethod' => [
'<?php
class A {
public function foo() : void {}
private function bar() : void {}
}
(new A)->foo();',
'<?php
class A {
public function foo() : void {}
}
(new A)->foo();',
'7.1',
['UnusedMethod'],
true,
],
'removeUnusedMethodAtBeginning' => [
'<?php
class A {
private function foo() : void {}
public function bar() : void {}
}
(new A)->bar();',
'<?php
class A {
public function bar() : void {}
}
(new A)->bar();',
'7.1',
['UnusedMethod'],
true,
],
'removePossiblyUnusedMethodWithDocblock' => [
'<?php
class A {
public function foo() : void {}
/** @return void */
public function bar() : void {}
}
(new A)->foo();',
'<?php
class A {
public function foo() : void {}
}
(new A)->foo();',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removeUnusedMethodWithDocblock' => [
'<?php
class A {
public function foo() : void {}
/** @return void */
private function bar() : void {}
}
(new A)->foo();',
'<?php
class A {
public function foo() : void {}
}
(new A)->foo();',
'7.1',
['UnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithVariableCall' => [
'<?php
class A {
public function foo() : void {}
}
function foo(A $a, string $var) {
echo $a->$var();
}',
'<?php
class A {
public function foo() : void {}
}
function foo(A $a, string $var) {
echo $a->$var();
}',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithVariableCallableCall' => [
'<?php
class A {
public function foo() : void {}
}
function takeCallable(callable $c) : void {}
function foo(A $a, string $var) {
takeCallable([$a, $var]);
}',
'<?php
class A {
public function foo() : void {}
}
function takeCallable(callable $c) : void {}
function foo(A $a, string $var) {
takeCallable([$a, $var]);
}',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithCallUserFuncCall' => [
'<?php
class A {
public function foo() : void {}
}
function foo(A $a, string $var) {
call_user_func([$a, $var]);
}',
'<?php
class A {
public function foo() : void {}
}
function foo(A $a, string $var) {
call_user_func([$a, $var]);
}',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithVariableCallableLhsCall' => [
'<?php
class A {
public function foo() : void {}
public function bar() : void {}
}
function takeCallable(callable $c) : void {}
function foo($a) {
takeCallable([$a, "foo"]);
}
foo(new A);',
'<?php
class A {
public function foo() : void {}
}
function takeCallable(callable $c) : void {}
function foo($a) {
takeCallable([$a, "foo"]);
}
foo(new A);',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'dontRemovePossiblyUnusedMethodWithVariableCallOnParent' => [
'<?php
class A { }
class B extends A {
public function foo() : void {}
}
function foo(A $a, string $var) {
echo $a->$var();
}
foo(new B);',
'<?php
class A { }
class B extends A {
public function foo() : void {}
}
function foo(A $a, string $var) {
echo $a->$var();
}
foo(new B);',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removePossiblyUnusedMethodWithVariableCall' => [
'<?php
class A {
public function foo() : void {}
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-method */
echo $a->$var();
}',
'<?php
class A {
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-method */
echo $a->$var();
}',
'7.1',
['PossiblyUnusedMethod'],
true,
],
'removePossiblyUnusedPropertyWithDocblock' => [
'<?php
class A {
/** @var string */
public $foo = "hello";
/** @var string */
public $bar = "hello";
}
echo (new A)->foo;',
'<?php
class A {
/** @var string */
public $foo = "hello";
}
echo (new A)->foo;',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithMixedUse' => [
'<?php
class A {
public $foo = "hello";
}
function foo($a) {
echo $a->foo;
}',
'<?php
class A {
public $foo = "hello";
}
function foo($a) {
echo $a->foo;
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithSuppression' => [
'<?php
/** @psalm-suppress PossiblyUnusedProperty */
class A {
public $foo = "hello";
}',
'<?php
/** @psalm-suppress PossiblyUnusedProperty */
class A {
public $foo = "hello";
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableFetch' => [
'<?php
class A {
public $foo = "hello";
}
function foo(A $a, string $var) {
echo $a->$var;
}',
'<?php
class A {
public $foo = "hello";
}
function foo(A $a, string $var) {
echo $a->$var;
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'removePossiblyUnusedPropertyWithVariableFetch' => [
'<?php
class A {
public $foo = "hello";
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-property */
echo $a->$var;
}',
'<?php
class A {
}
function foo(A $a, string $var) {
/** @psalm-ignore-variable-property */
echo $a->$var;
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableFetchInParent' => [
'<?php
class A {
public function __set(string $k, $v) {
$this->$k = $v;
}
}
class B extends A {
public $foo = "hello";
}
(new B())->__set("foo", "bar");',
'<?php
class A {
public function __set(string $k, $v) {
$this->$k = $v;
}
}
class B extends A {
public $foo = "hello";
}
(new B())->__set("foo", "bar");',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableOnParent' => [
'<?php
class A {}
class B extends A {
public $foo = "hello";
}
function foo(A $a, string $var) {
echo $a->$var;
}
foo(new A(), "foo");',
'<?php
class A {}
class B extends A {
public $foo = "hello";
}
function foo(A $a, string $var) {
echo $a->$var;
}
foo(new A(), "foo");',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableFetchImplementedInterface' => [
'<?php
interface I {}
class A implements I {
public $foo = "hello";
}
function foo(I $i, string $var) {
echo $i->$var;
}
foo(new A(), "foo");',
'<?php
interface I {}
class A implements I {
public $foo = "hello";
}
function foo(I $i, string $var) {
echo $i->$var;
}
foo(new A(), "foo");',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithStaticVariableFetch' => [
'<?php
class A {
public static $foo = "hello";
}
function foo(string $var) {
echo A::$$var;
}',
'<?php
class A {
public static $foo = "hello";
}
function foo(string $var) {
echo A::$$var;
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableAssignment' => [
'<?php
class A {
public $foo = "hello";
}
function foo(A $a, string $var) {
$a->$var = "hello";
}',
'<?php
class A {
public $foo = "hello";
}
function foo(A $a, string $var) {
$a->$var = "hello";
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithVariableAssignmentOnParent' => [
'<?php
class A {}
class B extends A {
public $foo = "hello";
}
function foo(A $a, string $var) {
$a->$var = "hello";
}
foo(new B);',
'<?php
class A {}
class B extends A {
public $foo = "hello";
}
function foo(A $a, string $var) {
$a->$var = "hello";
}
foo(new B);',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'dontRemovePossiblyUnusedPropertyWithStaticVariableAssignment' => [
'<?php
class A {
public static $foo = "hello";
}
function foo(string $var) {
A::$$var = "hello";
}',
'<?php
class A {
public static $foo = "hello";
}
function foo(string $var) {
A::$$var = "hello";
}',
'7.1',
['PossiblyUnusedProperty'],
true,
],
'removeUnusedPropertyWithDocblock' => [
'<?php
class A {
/** @var string */
public $foo = "hello";
/** @var string */
private $bar = "hello";
}
echo (new A)->foo;',
'<?php
class A {
/** @var string */
public $foo = "hello";
}
echo (new A)->foo;',
'7.1',
['UnusedProperty'],
true,
],
];
}
}

File diff suppressed because it is too large Load Diff