2020-08-25 01:24:27 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-25 01:24:27 +02:00
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
2021-01-11 23:30:33 +01:00
|
|
|
class ImmutableAnnotationAdditionTest extends FileManipulationTestCase
|
2020-08-25 01:24:27 +02:00
|
|
|
{
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2020-08-25 01:24:27 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'addPureAnnotationToFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
class A {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingImmutableAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-25 01:24:27 +02:00
|
|
|
],
|
|
|
|
'addPureAnnotationToFunctionWithExistingDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
/**
|
|
|
|
* This is a class
|
|
|
|
* that is cool
|
2020-09-03 01:39:14 +02:00
|
|
|
*
|
|
|
|
* @Foo\Bar
|
2020-08-25 01:24:27 +02:00
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
/**
|
|
|
|
* This is a class
|
|
|
|
* that is cool
|
|
|
|
*
|
2020-09-03 01:39:14 +02:00
|
|
|
* @Foo\Bar
|
|
|
|
*
|
2020-08-25 01:24:27 +02:00
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingImmutableAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-25 01:24:27 +02:00
|
|
|
],
|
|
|
|
'dontAddPureAnnotationWhenMethodHasImpurity' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
class A {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
echo $this->i;
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
class A {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
echo $this->i;
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingImmutableAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-25 01:24:27 +02:00
|
|
|
],
|
2020-09-03 21:32:14 +02:00
|
|
|
'addPureAnnotationWhenClassCanHoldMutableData' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
class B {
|
|
|
|
public int $i = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public B $b;
|
|
|
|
|
|
|
|
public function __construct(B $b) {
|
|
|
|
$this->b = $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->b->i + 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
|
|
|
|
$a = new A($b);
|
|
|
|
|
|
|
|
echo $a->getPlus5();
|
|
|
|
|
|
|
|
$b->i = 6;
|
|
|
|
|
|
|
|
echo $a->getPlus5();',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-08-25 01:24:27 +02:00
|
|
|
class B {
|
|
|
|
public int $i = 5;
|
|
|
|
}
|
|
|
|
|
2020-09-03 21:32:14 +02:00
|
|
|
/**
|
|
|
|
* @psalm-immutable
|
|
|
|
*/
|
2020-08-25 01:24:27 +02:00
|
|
|
class A {
|
|
|
|
public B $b;
|
|
|
|
|
|
|
|
public function __construct(B $b) {
|
|
|
|
$this->b = $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->b->i + 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
|
|
|
|
$a = new A($b);
|
|
|
|
|
|
|
|
echo $a->getPlus5();
|
|
|
|
|
|
|
|
$b->i = 6;
|
|
|
|
|
|
|
|
echo $a->getPlus5();',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingImmutableAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-08-25 01:24:27 +02:00
|
|
|
],
|
2020-09-03 01:39:14 +02:00
|
|
|
'addPureAnnotationToClassThatExtends' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-09-03 01:39:14 +02:00
|
|
|
class AParent {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mutate() : void {
|
|
|
|
echo "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A extends AParent {
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-09-03 01:39:14 +02:00
|
|
|
class AParent {
|
|
|
|
public int $i;
|
|
|
|
|
|
|
|
public function __construct(int $i) {
|
|
|
|
$this->i = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mutate() : void {
|
|
|
|
echo "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A extends AParent {
|
|
|
|
public function getPlus5() {
|
|
|
|
return $this->i + 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingImmutableAnnotation'],
|
|
|
|
'safe_types' => true,
|
2020-09-03 01:39:14 +02:00
|
|
|
],
|
2020-08-25 01:24:27 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|