1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 09:47:29 +01:00
psalm/tests/FileManipulation/ClassMoveTest.php

755 lines
24 KiB
PHP
Raw Normal View History

2019-06-04 22:36:32 +02:00
<?php
2019-06-04 22:36:32 +02:00
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;
2019-06-04 22:36:32 +02:00
use Psalm\Tests\TestConfig;
2021-06-08 04:55:21 +02:00
use function strpos;
2019-06-04 22:36:32 +02:00
2021-12-03 20:11:20 +01:00
class ClassMoveTest extends TestCase
2019-06-04 22:36:32 +02:00
{
2021-12-04 03:37:19 +01:00
/** @var ProjectAnalyzer */
2019-06-04 22:36:32 +02:00
protected $project_analyzer;
public function setUp(): void
2019-06-04 22:36:32 +02:00
{
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();
2019-06-04 22:36:32 +02:00
$this->file_provider = new FakeFileProvider();
2019-06-04 22:36:32 +02:00
}
/**
* @dataProvider providerValidCodeParse
*
* @param array<string, string> $constants_to_move
*/
public function testValidCode(
string $input_code,
string $output_code,
array $constants_to_move
): void {
2019-06-04 22:36:32 +02:00
$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(
2019-06-04 22:36:32 +02:00
$config,
2021-12-03 20:11:20 +01:00
new Providers(
2019-06-04 22:36:32 +02:00
$this->file_provider,
2021-12-04 21:55:53 +01:00
new FakeParserCacheProvider()
2019-06-04 22:36:32 +02:00
)
);
$context = new Context();
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile(
$file_path,
$input_code
);
$codebase = $this->project_analyzer->getCodebase();
$this->project_analyzer->refactorCodeAfterCompletion($constants_to_move);
$this->analyzeFile($file_path, $context);
$this->project_analyzer->prepareMigration();
$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>}>
2019-06-04 22:36:32 +02:00
*/
public function providerValidCodeParse(): array
2019-06-04 22:36:32 +02:00
{
return [
'renameEmptyClass' => [
'input' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class A {}
class C extends A {
/**
* @var A
*/
public $one;
}
/**
* @param A $a
2019-06-06 23:18:24 +02:00
* @param A::class|C::class $b
2019-06-04 22:36:32 +02:00
* @return A
*/
2019-06-06 23:18:24 +02:00
function foo(A $a, string $b) : A {
2019-06-04 22:36:32 +02:00
return $a;
}
/** @var A */
$i = new A();
if ($i instanceof A) {}',
'output' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class B {}
class C extends B {
/**
* @var B
*/
public $one;
}
/**
* @param B $a
2019-06-06 23:18:24 +02:00
* @param B::class|C::class $b
2019-06-04 22:36:32 +02:00
* @return B
*/
2019-06-06 23:18:24 +02:00
function foo(B $a, string $b) : B {
2019-06-04 22:36:32 +02:00
return $a;
}
/** @var B */
$i = new B();
if ($i instanceof B) {}',
'migrations' => [
2019-06-04 22:36:32 +02:00
'Ns\A' => 'Ns\B',
2019-07-05 22:24:00 +02:00
],
2019-06-04 22:36:32 +02:00
],
2019-06-06 19:57:00 +02:00
'renameEmptyClassWithSpacesInDocblock' => [
'input' => '<?php
2019-06-06 19:57:00 +02:00
namespace Ns;
class A {}
/**
* @param ?A $a
* @param string | null $b
2019-06-06 20:27:49 +02:00
* @param callable(): A $c
2019-06-06 19:57:00 +02:00
* @return A | null
*/
2019-06-06 20:27:49 +02:00
function foo(?A $a, $b, $c) : ?A {
2019-06-06 19:57:00 +02:00
return $a;
}',
'output' => '<?php
2019-06-06 19:57:00 +02:00
namespace Ns;
class B {}
/**
* @param B|null $a
2019-06-06 19:57:00 +02:00
* @param string | null $b
2019-06-06 20:27:49 +02:00
* @param callable():B $c
* @return B|null
2019-06-06 19:57:00 +02:00
*/
2019-06-06 20:27:49 +02:00
function foo(?B $a, $b, $c) : ?B {
2019-06-06 19:57:00 +02:00
return $a;
}',
'migrations' => [
2019-06-06 19:57:00 +02:00
'Ns\A' => 'Ns\B',
2019-07-05 22:24:00 +02:00
],
2019-06-06 19:57:00 +02:00
],
2019-06-04 22:36:32 +02:00
'renameClassWithInstanceMethod' => [
'input' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class A {
/**
* @param self $one
* @param A $two
*/
public function foo(self $one, A $two) : void {}
}
function foo(A $a) : A {
return $a->foo($a, $a);
}',
'output' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class B {
/**
* @param self $one
* @param self $two
*/
public function foo(self $one, self $two) : void {}
}
function foo(B $a) : B {
return $a->foo($a, $a);
}',
'migrations' => [
2019-06-04 22:36:32 +02:00
'Ns\A' => 'Ns\B',
2019-07-05 22:24:00 +02:00
],
2019-06-04 22:36:32 +02:00
],
'renameClassWithStaticMethod' => [
'input' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
2019-11-15 21:34:25 +01:00
class AParent {
public static function foo(A $one, A $two) {
}
}
class A extends AParent {
2019-06-04 22:36:32 +02:00
/**
* @param self $one
* @param A $two
2019-11-15 22:50:43 +01:00
* @return static
2019-06-04 22:36:32 +02:00
*/
public static function foo(self $one, A $two) : void {
A::foo($one, $two);
2019-11-17 01:59:08 +01:00
self::foo($one, $two);
2019-11-15 21:34:25 +01:00
parent::foo($one, $two);
static::foo($one, $two);
2019-11-15 22:50:43 +01:00
return new static();
2019-06-04 22:36:32 +02:00
}
}
2019-11-15 21:34:25 +01:00
$a = A::class;
$a::foo(new A(), new A());
2019-06-04 22:36:32 +02:00
function foo() {
A::foo(new A(), A::foo());
}',
'output' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
2019-11-15 21:34:25 +01:00
class AParent {
public static function foo(B $one, B $two) {
}
}
class B extends AParent {
2019-06-04 22:36:32 +02:00
/**
* @param self $one
* @param self $two
2019-11-15 22:50:43 +01:00
* @return static
2019-06-04 22:36:32 +02:00
*/
public static function foo(self $one, self $two) : void {
2019-11-17 01:59:08 +01:00
B::foo($one, $two);
2019-06-05 17:22:09 +02:00
self::foo($one, $two);
2019-11-15 21:34:25 +01:00
parent::foo($one, $two);
static::foo($one, $two);
2019-11-15 22:50:43 +01:00
return new static();
2019-06-04 22:36:32 +02:00
}
}
2019-11-15 21:34:25 +01:00
$a = B::class;
$a::foo(new B(), new B());
2019-06-04 22:36:32 +02:00
function foo() {
B::foo(new B(), B::foo());
}',
'migrations' => [
2019-06-04 22:36:32 +02:00
'Ns\A' => 'Ns\B',
2019-07-05 22:24:00 +02:00
],
2019-06-04 22:36:32 +02:00
],
'renameClassWithInstanceProperty' => [
'input' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class A {
/**
* @var A
*/
public $one;
/**
* @var self
*/
public $two;
}',
'output' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class B {
/**
* @var self
*/
public $one;
/**
* @var self
*/
public $two;
}',
'migrations' => [
2019-06-04 22:36:32 +02:00
'Ns\A' => 'Ns\B',
2019-07-05 22:24:00 +02:00
],
2019-06-04 22:36:32 +02:00
],
'renameClassWithStaticProperty' => [
'input' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class A {
/**
* @var string
*/
public static $one = "one";
2019-11-15 21:34:25 +01:00
/**
* @var array
*/
public static $vars = ["one"];
2019-06-04 22:36:32 +02:00
}
echo A::$one;
2019-11-15 21:34:25 +01:00
A::$one = "two";
foreach (A::$vars as $var) {}',
'output' => '<?php
2019-06-04 22:36:32 +02:00
namespace Ns;
class B {
/**
* @var string
*/
public static $one = "one";
2019-11-15 21:34:25 +01:00
/**
* @var array
2019-11-15 21:34:25 +01:00
*/
public static $vars = ["one"];
2019-06-04 22:36:32 +02:00
}
echo B::$one;
2019-11-15 21:34:25 +01:00
B::$one = "two";
foreach (B::$vars as $var) {}',
'migrations' => [
2019-06-04 22:36:32 +02:00
'Ns\A' => 'Ns\B',
2019-07-05 22:24:00 +02:00
],
2019-06-04 22:36:32 +02:00
],
'moveClassIntoNamespace' => [
'input' => '<?php
2019-06-05 17:15:52 +02:00
use Exception;
2019-06-04 22:36:32 +02:00
class A {
/** @var ?Exception */
public $x;
/**
* @param ArrayObject<int, A> $a
2019-11-15 21:34:25 +01:00
* @throws RunTimeException
2019-06-04 22:36:32 +02:00
*/
public function foo(ArrayObject $a) : Exception {
foreach ($a as $b) {
$b->bar();
}
2019-06-05 17:33:04 +02:00
try {
// something
} catch (InvalidArgumentException $e) {
}
2019-06-05 17:15:52 +02:00
echo \A::class;
echo __CLASS__;
2019-11-17 01:59:08 +01:00
echo self::class;
2019-06-05 17:15:52 +02:00
2019-06-05 17:22:09 +02:00
ArrayObject::foo();
2019-06-04 22:36:32 +02:00
return new Exception("bad");
}
public function bar() : void {}
}',
'output' => '<?php
2019-06-04 22:36:32 +02:00
namespace Foo\Bar\Baz;
2019-06-05 17:15:52 +02:00
use Exception;
2019-06-04 22:36:32 +02:00
class B {
/** @var Exception|null */
2019-06-04 22:36:32 +02:00
public $x;
/**
* @param \ArrayObject<int, self> $a
2019-11-15 21:34:25 +01:00
* @throws \RunTimeException
2019-06-04 22:36:32 +02:00
*/
2019-06-05 17:15:52 +02:00
public function foo(\ArrayObject $a) : Exception {
2019-06-04 22:36:32 +02:00
foreach ($a as $b) {
$b->bar();
}
2019-06-05 17:33:04 +02:00
try {
// something
} catch (\InvalidArgumentException $e) {
}
2019-11-17 01:59:08 +01:00
echo B::class;
echo B::class;
2019-06-05 17:15:52 +02:00
echo self::class;
2019-06-05 17:22:09 +02:00
\ArrayObject::foo();
2019-06-05 17:15:52 +02:00
return new Exception("bad");
2019-06-04 22:36:32 +02:00
}
public function bar() : void {}
}',
'migrations' => [
2019-06-04 22:36:32 +02:00
'A' => 'Foo\Bar\Baz\B',
2019-07-05 22:24:00 +02:00
],
2019-06-04 22:36:32 +02:00
],
2019-06-07 00:46:40 +02:00
'moveClassDeeperIntoNamespaceAdjustUseWithoutAlias' => [
'input' => '<?php
2019-06-06 04:13:33 +02:00
namespace Foo {
use Bar\Bat;
2019-06-04 22:36:32 +02:00
echo Bat::FOO;
2019-06-07 00:46:40 +02:00
echo Bat::FAR;
2019-06-04 22:36:32 +02:00
/**
2019-06-06 04:13:33 +02:00
* @param Bat $b
2019-06-07 00:46:40 +02:00
* @param Bat::FOO|Bat::FAR $c
2019-06-04 22:36:32 +02:00
*/
2019-06-07 00:46:40 +02:00
function doSomething(Bat $b, int $c) : void {}
class A {
/** @var ?Bat */
public $x = null;
}
2019-06-06 04:13:33 +02:00
}
namespace Bar {
class Bat {
const FOO = 5;
2019-06-07 00:46:40 +02:00
const FAR = 7;
}
2019-06-04 22:36:32 +02:00
}',
'output' => '<?php
2019-06-06 04:13:33 +02:00
namespace Foo {
use Bar\Baz\Bahh;
2019-06-04 22:36:32 +02:00
echo Bahh::FOO;
2019-06-07 00:46:40 +02:00
echo Bahh::FAR;
2019-06-04 22:36:32 +02:00
/**
2019-06-06 04:13:33 +02:00
* @param Bahh $b
* @param Bahh::FAR|Bahh::FOO $c
2019-06-04 22:36:32 +02:00
*/
2019-06-07 00:46:40 +02:00
function doSomething(Bahh $b, int $c) : void {}
class A {
/** @var Bahh|null */
public $x = null;
}
2019-06-06 04:13:33 +02:00
}
namespace Bar\Baz {
class Bahh {
const FOO = 5;
2019-06-07 00:46:40 +02:00
const FAR = 7;
}
}',
'migrations' => [
2019-06-07 00:46:40 +02:00
'Bar\Bat' => 'Bar\Baz\Bahh',
2019-07-05 22:24:00 +02:00
],
2019-06-07 00:46:40 +02:00
],
2019-06-10 23:09:34 +02:00
'moveClassesIntoNamespace' => [
'input' => '<?php
2019-06-10 23:09:34 +02:00
namespace Foo {
class A {
/** @var ?B */
public $x = null;
/** @var ?A */
public $y = null;
/** @var A|B|C|null */
public $z = null;
}
}
namespace Foo {
class B {
/** @var ?A */
public $x = null;
/** @var ?B */
public $y = null;
/** @var A|B|C|null */
public $z = null;
}
}
2019-06-11 16:33:52 +02:00
namespace Bar {
use Foo\A;
use Foo\B;
2019-06-10 23:09:34 +02:00
class C {
/** @var ?A */
public $x = null;
/** @var ?B */
public $y = null;
/** @var A|B|null */
2019-06-11 16:33:52 +02:00
public $z = null;
2019-06-10 23:09:34 +02:00
}
}',
'output' => '<?php
2019-06-10 23:09:34 +02:00
namespace Bar\Baz {
class A {
/** @var B|null */
2019-06-10 23:09:34 +02:00
public $x = null;
/** @var null|self */
public $y = null;
/** @var B|\Foo\C|null|self */
2019-06-10 23:09:34 +02:00
public $z = null;
}
}
namespace Bar\Baz {
class B {
/** @var A|null */
2019-06-10 23:09:34 +02:00
public $x = null;
/** @var null|self */
public $y = null;
/** @var A|\Foo\C|null|self */
2019-06-10 23:09:34 +02:00
public $z = null;
}
}
2019-06-11 16:33:52 +02:00
namespace Bar {
use Bar\Baz\A;
use Bar\Baz\B;
2019-06-10 23:09:34 +02:00
class C {
/** @var A|null */
2019-06-10 23:09:34 +02:00
public $x = null;
/** @var B|null */
2019-06-10 23:09:34 +02:00
public $y = null;
/** @var A|B|null */
2019-06-11 16:33:52 +02:00
public $z = null;
2019-06-10 23:09:34 +02:00
}
}',
'migrations' => [
2019-06-10 23:09:34 +02:00
'Foo\A' => 'Bar\Baz\A',
'Foo\B' => 'Bar\Baz\B',
2019-07-05 22:24:00 +02:00
],
2019-06-10 23:09:34 +02:00
],
2019-11-15 22:50:43 +01:00
'moveClassesIntoNamespaceWithoutAlias' => [
'input' => '<?php
2019-11-15 22:50:43 +01:00
namespace Foo {
class A {
/** @var ?B */
public $x = null;
/** @var ?A */
public $y = null;
/** @var A|B|C|null */
public $z = null;
public static $vars = [1, 2, 3];
}
}
namespace Foo {
class B {
/** @var ?A */
public $x = null;
/** @var ?B */
public $y = null;
/** @var A|B|C|null */
public $z = null;
}
foreach (A::$vars[$foo] as $var) {}
}
namespace Bar {
class C {
/** @var ?\Foo\A */
public $x = null;
/** @var ?\Foo\B */
public $y = null;
/** @var \Foo\A|\Foo\B|null */
public $z = null;
}
foreach (\Foo\A::$vars as $var) {}
}',
'output' => '<?php
2019-11-15 22:50:43 +01:00
namespace Bar\Baz {
class A {
/** @var B|null */
public $x = null;
/** @var null|self */
public $y = null;
/** @var B|\Foo\C|null|self */
public $z = null;
public static $vars = [1, 2, 3];
}
}
namespace Bar\Baz {
class B {
/** @var A|null */
public $x = null;
/** @var null|self */
public $y = null;
/** @var A|\Foo\C|null|self */
public $z = null;
}
foreach (\Bar\Baz\A::$vars[$foo] as $var) {}
}
namespace Bar {
class C {
/** @var Baz\A|null */
public $x = null;
/** @var Baz\B|null */
public $y = null;
/** @var Baz\A|Baz\B|null */
public $z = null;
}
foreach (Baz\A::$vars as $var) {}
}',
'migrations' => [
2019-11-15 22:50:43 +01:00
'Foo\A' => 'Bar\Baz\A',
'Foo\B' => 'Bar\Baz\B',
],
],
2019-06-07 00:46:40 +02:00
'moveClassDeeperIntoNamespaceAdjustUseWithAlias' => [
'input' => '<?php
2019-06-07 00:46:40 +02:00
namespace Foo {
use Bar\Bat as Kappa;
echo Kappa::FOO;
echo Kappa::FAR;
/**
* @param Kappa $b
* @param Kappa::FOO|Kappa::FAR $c
*/
function doSomething(Kappa $b, int $c) : void {}
class A {
/** @var ?Kappa */
public $x = null;
}
}
namespace Bar {
class Bat {
const FOO = 5;
const FAR = 7;
}
}',
'output' => '<?php
2019-06-07 00:46:40 +02:00
namespace Foo {
use Bar\Baz\Bahh as Kappa;
echo Kappa::FOO;
echo Kappa::FAR;
/**
* @param Kappa $b
* @param Kappa::FAR|Kappa::FOO $c
2019-06-07 00:46:40 +02:00
*/
function doSomething(Kappa $b, int $c) : void {}
class A {
/** @var Kappa|null */
2019-06-07 00:46:40 +02:00
public $x = null;
}
}
namespace Bar\Baz {
class Bahh {
const FOO = 5;
const FAR = 7;
}
2019-06-06 04:13:33 +02:00
}',
'migrations' => [
2019-06-06 04:13:33 +02:00
'Bar\Bat' => 'Bar\Baz\Bahh',
2019-07-05 22:24:00 +02:00
],
2019-06-06 04:13:33 +02:00
],
'moveClassDeeperIntoNamespaceDontAdjustGroupUse' => [
'input' => '<?php
2019-06-06 04:13:33 +02:00
namespace Foo {
use Bar\{Bat};
2019-06-04 22:36:32 +02:00
2019-06-06 04:13:33 +02:00
/**
* @param Bat $b
*/
function doSomething(Bat $b) : void {}
}
namespace Bar {
class Bat {}
}',
'output' => '<?php
2019-06-06 04:13:33 +02:00
namespace Foo {
use Bar\{Bat};
2019-06-04 22:36:32 +02:00
2019-06-06 04:13:33 +02:00
/**
* @param \Bar\Baz\Bahh $b
*/
function doSomething(\Bar\Baz\Bahh $b) : void {}
}
namespace Bar\Baz {
class Bahh {}
2019-06-04 22:36:32 +02:00
}',
'migrations' => [
2019-06-06 04:13:33 +02:00
'Bar\Bat' => 'Bar\Baz\Bahh',
2019-07-05 22:24:00 +02:00
],
2019-06-04 22:36:32 +02:00
],
'moveClassBewareOfPropertyNotSetInConstructorCheck' => [
'input' => '<?php
namespace Foo {
class Base
{
protected $property1;
public function __construct()
{
$this->property1 = "";
}
}
}
namespace Foo {
class Hello extends Base {}
}',
'output' => '<?php
namespace Foo {
class Base
{
protected $property1;
public function __construct()
{
$this->property1 = "";
}
}
}
namespace Foo\Bar {
class Hello extends \Foo\Base {}
}',
'migrations' => [
'Foo\Hello' => 'Foo\Bar\Hello',
],
],
2019-06-04 22:36:32 +02:00
];
}
}