2020-08-23 16:28:26 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 14:16:41 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-23 16:28:26 +02:00
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
2021-01-11 23:30:33 +01:00
|
|
|
class PureAnnotationAdditionTest extends FileManipulationTestCase
|
2020-08-23 16:28:26 +02:00
|
|
|
{
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2020-08-23 16:28:26 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'addPureAnnotationToFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-23 16:28:26 +02:00
|
|
|
function foo(string $s): string {
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-23 16:28:26 +02:00
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-23 16:28:26 +02:00
|
|
|
],
|
2020-08-23 19:34:32 +02:00
|
|
|
'addPureAnnotationToFunctionWithExistingDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-23 19:34:32 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function foo(string $s) {
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-23 19:34:32 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function foo(string $s) {
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-23 19:34:32 +02:00
|
|
|
],
|
2020-08-23 16:28:26 +02:00
|
|
|
'dontAddPureAnnotationToImpureFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-23 16:28:26 +02:00
|
|
|
function foo(string $s): string {
|
|
|
|
echo $s;
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-23 16:28:26 +02:00
|
|
|
function foo(string $s): string {
|
|
|
|
echo $s;
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-23 16:28:26 +02:00
|
|
|
],
|
2020-08-23 16:57:24 +02:00
|
|
|
'dontAddPureAnnotationToMutationFreeMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-23 16:57:24 +02:00
|
|
|
class A {
|
|
|
|
public string $foo = "hello";
|
|
|
|
|
|
|
|
public function getFoo() : string {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-23 16:57:24 +02:00
|
|
|
class A {
|
|
|
|
public string $foo = "hello";
|
|
|
|
|
|
|
|
public function getFoo() : string {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-23 16:57:24 +02:00
|
|
|
],
|
2020-08-23 19:34:32 +02:00
|
|
|
'dontAddPureAnnotationToFunctionWithImpureCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-23 19:34:32 +02:00
|
|
|
function foo(string $s): string {
|
|
|
|
if (file_exists($s)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-23 19:34:32 +02:00
|
|
|
function foo(string $s): string {
|
|
|
|
if (file_exists($s)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-23 19:34:32 +02:00
|
|
|
],
|
2020-08-23 20:07:19 +02:00
|
|
|
'dontAddPureAnnotationToFunctionWithImpureClosure' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-23 20:07:19 +02:00
|
|
|
/** @param list<string> $arr */
|
|
|
|
function foo(array $arr): array {
|
|
|
|
return array_map($arr, function ($s) { echo $s; return $s;});
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-23 20:07:19 +02:00
|
|
|
/** @param list<string> $arr */
|
|
|
|
function foo(array $arr): array {
|
|
|
|
return array_map($arr, function ($s) { echo $s; return $s;});
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-23 20:07:19 +02:00
|
|
|
],
|
2020-08-24 00:41:31 +02:00
|
|
|
'dontAddWhenReferencingThis' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-24 04:07:02 +02:00
|
|
|
abstract class A {
|
2020-08-24 00:41:31 +02:00
|
|
|
public int $a = 5;
|
|
|
|
|
|
|
|
public function foo() : self {
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-24 04:07:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-24 04:07:02 +02:00
|
|
|
abstract class A {
|
2020-08-24 00:41:31 +02:00
|
|
|
public int $a = 5;
|
|
|
|
|
|
|
|
public function foo() : self {
|
|
|
|
return $this;
|
|
|
|
}
|
2020-08-24 04:07:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-24 00:41:31 +02:00
|
|
|
],
|
2020-08-24 04:16:03 +02:00
|
|
|
'dontAddInChildMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-24 04:16:03 +02:00
|
|
|
class A {
|
|
|
|
public int $a = 5;
|
|
|
|
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $string . $this->a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-24 04:16:03 +02:00
|
|
|
class A {
|
|
|
|
public int $a = 5;
|
|
|
|
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $string . $this->a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-24 04:16:03 +02:00
|
|
|
],
|
|
|
|
'doAddInOtherMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-24 04:16:03 +02:00
|
|
|
class A {
|
|
|
|
public int $a = 5;
|
|
|
|
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $string . $this->a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function bar(string $s) : string {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-24 04:16:03 +02:00
|
|
|
class A {
|
|
|
|
public int $a = 5;
|
|
|
|
|
|
|
|
public function foo(string $s) : string {
|
|
|
|
return $string . $this->a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
public function bar(string $s) : string {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-24 04:16:03 +02:00
|
|
|
],
|
2022-01-31 20:52:25 +01:00
|
|
|
'dontAddPureIfCallableNotPure' => [
|
2022-02-11 02:51:48 +01:00
|
|
|
'input' => '<?php
|
2022-01-31 20:52:25 +01:00
|
|
|
function pure(callable $callable): string{
|
|
|
|
return $callable();
|
|
|
|
}',
|
2022-02-11 02:51:48 +01:00
|
|
|
'output' => '<?php
|
2022-01-31 20:52:25 +01:00
|
|
|
function pure(callable $callable): string{
|
|
|
|
return $callable();
|
|
|
|
}',
|
2022-02-11 02:51:48 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPureAnnotation'],
|
|
|
|
'safe_types' => true,
|
2022-01-31 20:52:25 +01:00
|
|
|
],
|
2020-08-23 16:28:26 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|