2020-08-23 16:28:26 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
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
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return array<string,array{string,string,string,string[],bool}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2020-08-23 16:28:26 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'addPureAnnotationToFunction' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s): string {
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function foo(string $s): string {
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-23 19:34:32 +02:00
|
|
|
'addPureAnnotationToFunctionWithExistingDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function foo(string $s) {
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @psalm-pure
|
|
|
|
*/
|
|
|
|
function foo(string $s) {
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-23 16:28:26 +02:00
|
|
|
'dontAddPureAnnotationToImpureFunction' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s): string {
|
|
|
|
echo $s;
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
function foo(string $s): string {
|
|
|
|
echo $s;
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-23 16:57:24 +02:00
|
|
|
'dontAddPureAnnotationToMutationFreeMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public string $foo = "hello";
|
|
|
|
|
|
|
|
public function getFoo() : string {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public string $foo = "hello";
|
|
|
|
|
|
|
|
public function getFoo() : string {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-23 19:34:32 +02:00
|
|
|
'dontAddPureAnnotationToFunctionWithImpureCall' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s): string {
|
|
|
|
if (file_exists($s)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
function foo(string $s): string {
|
|
|
|
if (file_exists($s)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-23 20:07:19 +02:00
|
|
|
'dontAddPureAnnotationToFunctionWithImpureClosure' => [
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function foo(array $arr): array {
|
|
|
|
return array_map($arr, function ($s) { echo $s; return $s;});
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function foo(array $arr): array {
|
|
|
|
return array_map($arr, function ($s) { echo $s; return $s;});
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-24 00:41:31 +02:00
|
|
|
'dontAddWhenReferencingThis' => [
|
|
|
|
'<?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 {}',
|
2020-08-24 00:41:31 +02:00
|
|
|
'<?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 {}',
|
2020-08-24 00:41:31 +02:00
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-24 04:16:03 +02:00
|
|
|
'dontAddInChildMethod' => [
|
|
|
|
'<?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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
'doAddInOtherMethod' => [
|
|
|
|
'<?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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'<?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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'7.4',
|
|
|
|
['MissingPureAnnotation'],
|
|
|
|
true,
|
|
|
|
],
|
2020-08-23 16:28:26 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|