1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00
psalm/tests/FileManipulation/PureAnnotationAdditionTest.php

227 lines
7.9 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\FileManipulation;
class PureAnnotationAdditionTest extends FileManipulationTestCase
{
public function providerValidCodeParse(): array
{
return [
'addPureAnnotationToFunction' => [
'input' => '<?php
function foo(string $s): string {
return $s;
}',
'output' => '<?php
/**
* @psalm-pure
*/
function foo(string $s): string {
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
'addPureAnnotationToFunctionWithExistingDocblock' => [
'input' => '<?php
/**
* @return string
*/
function foo(string $s) {
return $s;
}',
'output' => '<?php
/**
* @return string
*
* @psalm-pure
*/
function foo(string $s) {
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
'dontAddPureAnnotationToImpureFunction' => [
'input' => '<?php
function foo(string $s): string {
echo $s;
return $s;
}',
'output' => '<?php
function foo(string $s): string {
echo $s;
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
'dontAddPureAnnotationToMutationFreeMethod' => [
'input' => '<?php
class A {
public string $foo = "hello";
public function getFoo() : string {
return $this->foo;
}
}',
'output' => '<?php
class A {
public string $foo = "hello";
public function getFoo() : string {
return $this->foo;
}
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
'dontAddPureAnnotationToFunctionWithImpureCall' => [
'input' => '<?php
function foo(string $s): string {
if (file_exists($s)) {
return "";
}
return $s;
}',
'output' => '<?php
function foo(string $s): string {
if (file_exists($s)) {
return "";
}
return $s;
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
'dontAddPureAnnotationToFunctionWithImpureClosure' => [
'input' => '<?php
/** @param list<string> $arr */
function foo(array $arr): array {
return array_map($arr, function ($s) { echo $s; return $s;});
}',
'output' => '<?php
/** @param list<string> $arr */
function foo(array $arr): array {
return array_map($arr, function ($s) { echo $s; return $s;});
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
2020-08-24 00:41:31 +02:00
'dontAddWhenReferencingThis' => [
'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 {}',
'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 {}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
2020-08-24 00:41:31 +02:00
],
'dontAddInChildMethod' => [
'input' => '<?php
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;
}
}',
'output' => '<?php
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;
}
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
'doAddInOtherMethod' => [
'input' => '<?php
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;
}
}',
'output' => '<?php
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;
}
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
],
2022-01-31 20:52:25 +01:00
'dontAddPureIfCallableNotPure' => [
'input' => '<?php
2022-01-31 20:52:25 +01:00
function pure(callable $callable): string{
return $callable();
}',
'output' => '<?php
2022-01-31 20:52:25 +01:00
function pure(callable $callable): string{
return $callable();
}',
'php_version' => '7.4',
'issues_to_fix' => ['MissingPureAnnotation'],
'safe_types' => true,
2022-01-31 20:52:25 +01:00
],
];
}
}