1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00
psalm/tests/FileManipulation/MethodMoveTest.php

785 lines
23 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\FileManipulation;
use Psalm\Context;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Provider\Providers;
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
use Psalm\Internal\RuntimeCaches;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
2021-12-03 20:11:20 +01:00
use Psalm\Tests\TestCase;
use Psalm\Tests\TestConfig;
2021-06-08 04:55:21 +02:00
use function strpos;
2021-12-03 20:11:20 +01:00
class MethodMoveTest extends TestCase
{
2022-12-16 19:58:47 +01:00
protected ProjectAnalyzer $project_analyzer;
public function setUp(): void
{
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
RuntimeCaches::clearAll();
$this->file_provider = new FakeFileProvider();
}
/**
* @dataProvider providerValidCodeParse
2019-06-02 18:02:32 +02:00
* @param array<string, string> $methods_to_move
*/
public function testValidCode(
string $input_code,
string $output_code,
array $methods_to_move
): void {
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
$config = new TestConfig();
2021-12-03 20:11:20 +01:00
$this->project_analyzer = new ProjectAnalyzer(
$config,
2021-12-03 20:11:20 +01:00
new Providers(
$this->file_provider,
2022-12-18 17:15:15 +01:00
new FakeParserCacheProvider(),
),
);
$context = new Context();
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile(
$file_path,
2022-12-18 17:15:15 +01:00
$input_code,
);
$codebase = $this->project_analyzer->getCodebase();
$this->project_analyzer->refactorCodeAfterCompletion($methods_to_move);
$this->analyzeFile($file_path, $context);
$this->project_analyzer->prepareMigration();
2019-06-02 07:10:50 +02:00
$codebase->analyzer->updateFile($file_path, false);
$this->project_analyzer->migrateCode();
$this->assertSame($output_code, $codebase->getFileContents($file_path));
}
/**
* @return array<string,array{input:string,output:string,migrations:array<string, string>}>
*/
public function providerValidCodeParse(): array
{
return [
2019-06-04 06:32:19 +02:00
'moveSimpleStaticMethodWithForeachIterator' => [
'input' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
2019-06-02 07:10:50 +02:00
use ArrayObject;
A::Foo();
class A {
/**
2019-06-02 07:10:50 +02:00
* @return ArrayObject<int, int>
*/
public static function Foo() {
2019-06-04 06:32:19 +02:00
return new ArrayObject([1, 2, 3]);
}
}
class B {
public static function bar() : void {
A::Foo();
2019-06-02 07:10:50 +02:00
foreach (A::Foo() as $f) {}
}
}',
'output' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
2019-06-02 07:10:50 +02:00
use ArrayObject;
B::Fe();
class A {
}
class B {
public static function bar() : void {
2019-11-17 01:59:08 +01:00
B::Fe();
foreach (B::Fe() as $f) {}
}
2019-06-04 06:32:19 +02:00
/**
* @return ArrayObject<int, int>
*/
public static function Fe() {
2019-06-04 06:32:19 +02:00
return new ArrayObject([1, 2, 3]);
}
}',
'migrations' => [
2019-06-04 06:32:19 +02:00
'Ns\A::Foo' => 'Ns\B::Fe',
2019-07-05 22:24:00 +02:00
],
2019-06-04 06:32:19 +02:00
],
'moveSimpleStaticMethodWithConstant' => [
'input' => '<?php
2019-06-04 06:32:19 +02:00
namespace Ns;
class A {
const C = 5;
public static function Foo() : void {
echo self::C;
}
}
class B {
}',
'output' => '<?php
2019-06-04 06:32:19 +02:00
namespace Ns;
class A {
const C = 5;
}
class B {
public static function Fe() : void {
echo A::C;
}
2019-06-04 06:32:19 +02:00
}',
'migrations' => [
2019-06-04 06:32:19 +02:00
'Ns\A::Foo' => 'Ns\B::Fe',
2019-07-05 22:24:00 +02:00
],
2019-06-04 06:32:19 +02:00
],
'moveSimpleStaticMethodWithProperty' => [
'input' => '<?php
2019-06-04 06:32:19 +02:00
namespace Ns;
class A {
/** @var int */
public static $baz;
public static function Foo() : void {
echo self::$baz;
echo A::$baz . " ";
self::$baz = 12;
A::$baz = 14;
}
}
class B {
}',
'output' => '<?php
2019-06-04 06:32:19 +02:00
namespace Ns;
class A {
/** @var int */
public static $baz;
2019-06-04 06:32:19 +02:00
}
class B {
public static function Fe() : void {
echo A::$baz;
echo A::$baz . " ";
A::$baz = 12;
A::$baz = 14;
}
}',
'migrations' => [
'Ns\A::Foo' => 'Ns\B::Fe',
2019-07-05 22:24:00 +02:00
],
],
'moveStaticMethodIntoNamespaceWithExistingUse' => [
'input' => '<?php
2019-06-02 07:10:50 +02:00
namespace {
class A {
public static function Foo() : void {}
}
}
namespace Ns {
use A;
class C {
public static function Bar() : void {
A::Foo();
}
}
}
namespace Ns\A {
class B {
}
2019-06-02 07:10:50 +02:00
}',
'output' => '<?php
2019-06-02 07:10:50 +02:00
namespace {
class A {
2019-06-02 07:10:50 +02:00
}
}
namespace Ns {
use A;
class C {
public static function Bar() : void {
\Ns\A\B::Fedcba();
}
}
}
namespace Ns\A {
class B {
public static function Fedcba() : void {}
}
2019-06-02 07:10:50 +02:00
}',
'migrations' => [
'A::Foo' => 'Ns\A\B::Fedcba',
2019-07-05 22:24:00 +02:00
],
2019-06-02 07:10:50 +02:00
],
'moveEmptyStaticMethodOnly' => [
'input' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class A {
/**
* @return void
*/
public static function Foo() : void {}
}
class B {
}',
'output' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class A {
}
class B {
/**
* @return void
*/
public static function Fedcba() : void {}
}',
'migrations' => [
'Ns\A::Foo' => 'Ns\B::Fedcba',
],
],
'moveStaticMethodOnly' => [
'input' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class A {
const C = 5;
/**
2019-06-01 22:46:10 +02:00
* @param self $a1
* Some description
* @param ?self
* $a2
* @param array<
* int,
* self
* > $a3
* @return self
*/
2019-06-01 23:22:33 +02:00
public static function Foo(self $a1, ?self $a2, array $a3) : self {
echo self::C;
echo A::C;
self::Bar();
A::Bar();
echo \Ns\B::D;
2019-06-01 16:32:49 +02:00
new A();
/** @var self */
$a = new self();
2019-06-01 16:32:49 +02:00
new B();
return $a;
}
public static function Bar() : void {}
}
class B {
const D = 5;
}',
'output' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class A {
const C = 5;
public static function Bar() : void {}
}
class B {
const D = 5;
/**
2019-06-01 22:46:10 +02:00
* @param A $a1
* Some description
* @param A|null
* $a2
* @param array<int, A> $a3
* @return A
*/
2019-06-01 23:22:33 +02:00
public static function Fedbca(A $a1, ?A $a2, array $a3) : A {
echo A::C;
echo A::C;
A::Bar();
A::Bar();
2019-11-17 01:59:08 +01:00
echo B::D;
2019-06-01 16:32:49 +02:00
new A();
/** @var A */
$a = new A();
2019-11-17 01:59:08 +01:00
new B();
return $a;
}
}',
'migrations' => [
'Ns\A::Foo' => 'Ns\B::Fedbca',
2019-07-05 22:24:00 +02:00
],
],
'moveTwoStaticMethods' => [
'input' => '<?php
namespace Ns;
class A {
const C = 5;
/**
* @param self $a1
* Some description
* @param ?self
* $a2
* @param array<
* int,
* self
* > $a3
* @return self
*/
public static function Foo(self $a1, ?self $a2, array $a3) : self {
echo self::C;
echo A::C;
self::Bar();
A::Bar();
echo \Ns\B::D;
new A();
/** @var self */
$a = new self();
new B();
return $a;
}
public static function Bar() : void {}
}
class B {
const D = 5;
}',
'output' => '<?php
namespace Ns;
class A {
const C = 5;
}
class B {
const D = 5;
/**
* @param A $a1
* Some description
* @param A|null
* $a2
* @param array<int, A> $a3
* @return A
*/
public static function Fedbca(A $a1, ?A $a2, array $a3) : A {
echo A::C;
echo A::C;
self::Blacksheep();
2019-11-17 01:59:08 +01:00
B::Blacksheep();
echo B::D;
new A();
/** @var A */
$a = new A();
2019-11-17 01:59:08 +01:00
new B();
return $a;
}
public static function Blacksheep() : void {}
}',
'migrations' => [
'Ns\A::Foo' => 'Ns\B::Fedbca',
'Ns\A::Bar' => 'Ns\B::Blacksheep',
2019-07-05 22:24:00 +02:00
],
],
'moveInstanceMethodIntoSubclassOnly' => [
'input' => '<?php
namespace Ns;
class A {
const C = 5;
/**
* @param self $a1
* Some description
* @param ?self
* $a2
* @param array<
* int,
* self
* > $a3
* @return self
*/
public function Foo(self $a1, ?self $a2, array $a3) : self {
echo self::C;
echo A::C;
$this->Bar();
A::Bar();
echo \Ns\AChild::D;
new A();
/** @var self */
$a = new self();
new AChild();
return $a;
}
public function Bar() : void {}
}
class AChild extends A {
const D = 5;
}',
'output' => '<?php
namespace Ns;
class A {
const C = 5;
public function Bar() : void {}
}
class AChild extends A {
const D = 5;
/**
* @param A $a1
* Some description
* @param A|null
* $a2
* @param array<int, A> $a3
* @return A
*/
public function Fedbca(A $a1, ?A $a2, array $a3) : A {
echo A::C;
echo A::C;
$this->Bar();
A::Bar();
2019-11-17 01:59:08 +01:00
echo AChild::D;
new A();
/** @var A */
$a = new A();
2019-11-17 01:59:08 +01:00
new AChild();
return $a;
}
}',
'migrations' => [
'Ns\A::Foo' => 'Ns\AChild::Fedbca',
2019-07-05 22:24:00 +02:00
],
],
'moveStaticMethodAndReferencesFromAbove' => [
'input' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class A {
const C = 5;
/**
* @return void
*/
public static function Foo() : void {
echo self::C;
}
}
class B {
public static function bar() : void {
A::Foo();
}
}',
'output' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class A {
const C = 5;
}
class B {
public static function bar() : void {
2019-11-17 01:59:08 +01:00
B::Fe();
}
/**
* @return void
*/
public static function Fe() : void {
echo A::C;
}
}',
'migrations' => [
'Ns\A::Foo' => 'Ns\B::Fe',
2019-07-05 22:24:00 +02:00
],
],
'moveStaticMethodAndReferencesFromBelow' => [
'input' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class B {
public static function bar() : void {
A::Foo();
}
}
class A {
const C = 5;
/**
* @return void
*/
public static function Foo() : void {
echo self::C;
}
}',
'output' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns;
class B {
public static function bar() : void {
2019-11-17 01:59:08 +01:00
B::Fe();
}
/**
* @return void
*/
public static function Fe() : void {
echo A::C;
}
}
class A {
const C = 5;
}',
'migrations' => [
'Ns\A::Foo' => 'Ns\B::Fe',
2019-06-01 07:33:21 +02:00
],
],
'moveStaticMethodAndReferencesAcrossNamespaces' => [
'input' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns1 {
class A {
const C = 5;
/**
* @return void
*/
public static function Foo() : void {
echo self::C;
}
}
}
namespace Ns2\Ns3 {
class B {
public static function bar() : void {
\Ns1\A::Foo();
}
}
}',
'output' => '<?php
2019-06-01 07:33:21 +02:00
namespace Ns1 {
class A {
const C = 5;
}
}
namespace Ns2\Ns3 {
class B {
public static function bar() : void {
2019-11-17 01:59:08 +01:00
B::Fe();
2019-06-01 07:33:21 +02:00
}
/**
* @return void
*/
public static function Fe() : void {
echo \Ns1\A::C;
}
}
}',
'migrations' => [
'Ns1\A::Foo' => 'Ns2\Ns3\B::Fe',
2019-07-05 22:24:00 +02:00
],
2019-06-01 07:33:21 +02:00
],
'moveStaticMethodAndReferencesAcrossNamespacesWithExistingUse' => [
'input' => '<?php
namespace Ns1 {
class A {
const C = 5;
/**
* @return void
*/
public static function Foo() : void {
echo self::C;
}
}
}
namespace Ns2\Ns3 {
use Ns1\A;
class B {
public static function bar() : void {
\Ns1\A::Foo();
}
}
}',
'output' => '<?php
namespace Ns1 {
class A {
const C = 5;
}
}
namespace Ns2\Ns3 {
use Ns1\A;
class B {
public static function bar() : void {
2019-11-17 01:59:08 +01:00
B::Fedcba();
}
/**
* @return void
*/
public static function Fedcba() : void {
echo A::C;
}
}
}',
'migrations' => [
'Ns1\A::Foo' => 'Ns2\Ns3\B::Fedcba',
2019-07-05 22:24:00 +02:00
],
],
2019-06-04 06:32:19 +02:00
'renameInstanceMethod' => [
'input' => '<?php
2019-06-04 06:32:19 +02:00
namespace Ns;
use ArrayObject;
class A {
/**
* @return ArrayObject<int, int>
*/
public function Foo() {
return new ArrayObject([self::C]);
}
public function bat() {
$this->foo();
}
}
class B extends A {
public static function bar(A $a) : void {
$a->Foo();
$this->foo();
parent::foo();
foreach ($a->Foo() as $f) {}
}
}',
'output' => '<?php
2019-06-04 06:32:19 +02:00
namespace Ns;
use ArrayObject;
class A {
/**
* @return ArrayObject<int, int>
*/
public function Fedcba() {
return new ArrayObject([self::C]);
}
public function bat() {
$this->Fedcba();
}
}
class B extends A {
public static function bar(A $a) : void {
$a->Fedcba();
$this->Fedcba();
parent::Fedcba();
foreach ($a->Fedcba() as $f) {}
}
}',
'migrations' => [
2019-06-04 06:32:19 +02:00
'Ns\A::foo' => 'Ns\A::Fedcba',
],
],
];
}
}