2020-07-08 18:03:12 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-07-08 18:03:12 +02:00
|
|
|
namespace Psalm\Tests\FileManipulation;
|
|
|
|
|
2021-01-11 23:30:33 +01:00
|
|
|
class MissingPropertyTypeTest extends FileManipulationTestCase
|
2020-07-08 18:03:12 +02:00
|
|
|
{
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): array
|
2020-07-08 18:03:12 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'addMissingUnionType56' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
} else {
|
|
|
|
$this->v = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var int|string
|
2021-05-28 15:46:11 +02:00
|
|
|
*
|
|
|
|
* @psalm-var \'hello\'|4
|
2020-07-08 18:03:12 +02:00
|
|
|
*/
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
} else {
|
|
|
|
$this->v = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '5.6',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 18:03:12 +02:00
|
|
|
],
|
|
|
|
'addMissingNullableType56' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var int|null
|
2021-05-28 15:46:11 +02:00
|
|
|
*
|
|
|
|
* @psalm-var 4|null
|
2020-07-08 18:03:12 +02:00
|
|
|
*/
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '5.6',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 18:03:12 +02:00
|
|
|
],
|
|
|
|
'addMissingNullableTypeNoDefault74' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var int|null
|
2021-05-28 15:46:11 +02:00
|
|
|
*
|
|
|
|
* @psalm-var 4|null
|
2020-07-08 18:03:12 +02:00
|
|
|
*/
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 18:03:12 +02:00
|
|
|
],
|
|
|
|
'addMissingNullableTypeWithDefault74' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public $v = null;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public ?int $v = null;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 18:03:12 +02:00
|
|
|
],
|
|
|
|
'addMissingUnionTypeSetInBranches74' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
} else {
|
|
|
|
$this->v = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var int|string
|
2021-05-28 15:46:11 +02:00
|
|
|
*
|
|
|
|
* @psalm-var \'hello\'|4
|
2020-07-08 18:03:12 +02:00
|
|
|
*/
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
} else {
|
|
|
|
$this->v = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 18:03:12 +02:00
|
|
|
],
|
|
|
|
'addMissingIntTypeSetInBranches74' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
} else {
|
|
|
|
$this->v = 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 18:03:12 +02:00
|
|
|
class A {
|
|
|
|
public int $v;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->v = 4;
|
|
|
|
} else {
|
|
|
|
$this->v = 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 18:03:12 +02:00
|
|
|
],
|
2020-07-08 20:32:16 +02:00
|
|
|
'addMissingDocblockTypesSpacedProperly' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 20:32:16 +02:00
|
|
|
class A {
|
|
|
|
public $u;
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct(int $i, int $j) {
|
|
|
|
$this->u = $i;
|
|
|
|
$this->v = $j;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 20:32:16 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $u;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct(int $i, int $j) {
|
|
|
|
$this->u = $i;
|
|
|
|
$this->v = $j;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.1',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 20:32:16 +02:00
|
|
|
],
|
|
|
|
'addMissingTypehintsSpacedProperly' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 20:32:16 +02:00
|
|
|
class A {
|
|
|
|
public $u;
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct(int $i, int $j) {
|
|
|
|
$this->u = $i;
|
|
|
|
$this->v = $j;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 20:32:16 +02:00
|
|
|
class A {
|
|
|
|
public int $u;
|
|
|
|
public int $v;
|
|
|
|
|
|
|
|
public function __construct(int $i, int $j) {
|
|
|
|
$this->u = $i;
|
|
|
|
$this->v = $j;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 20:32:16 +02:00
|
|
|
],
|
2020-07-08 20:43:36 +02:00
|
|
|
'addMissingTypehintWithDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-08 20:43:36 +02:00
|
|
|
class A {
|
|
|
|
public $u = false;
|
|
|
|
|
|
|
|
public function bar() {
|
|
|
|
$this->u = true;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-08 20:43:36 +02:00
|
|
|
class A {
|
|
|
|
public bool $u = false;
|
|
|
|
|
|
|
|
public function bar() {
|
|
|
|
$this->u = true;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-08 20:43:36 +02:00
|
|
|
],
|
2020-07-11 01:11:06 +02:00
|
|
|
'dontAddMissingPropertyTypeInTrait' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'input' => '<?php
|
2020-07-11 01:11:06 +02:00
|
|
|
trait T {
|
|
|
|
public $u;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function bar() {
|
|
|
|
$this->u = 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'output' => '<?php
|
2020-07-11 01:11:06 +02:00
|
|
|
trait T {
|
|
|
|
public $u;
|
|
|
|
}
|
|
|
|
class A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function bar() {
|
|
|
|
$this->u = 5;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
2020-07-11 01:11:06 +02:00
|
|
|
],
|
2024-02-04 18:52:20 +01:00
|
|
|
'doNotAddCallablePropertyTypes' => [
|
|
|
|
'input' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
public $u;
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct(?callable $u, callable $v) {
|
|
|
|
$this->u = $u;
|
|
|
|
$this->v = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'output' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var callable|null
|
|
|
|
*/
|
|
|
|
public $u;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
public $v;
|
|
|
|
|
|
|
|
public function __construct(?callable $u, callable $v) {
|
|
|
|
$this->u = $u;
|
|
|
|
$this->v = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
|
|
|
],
|
2024-02-04 19:16:42 +01:00
|
|
|
'addClosurePropertyType' => [
|
|
|
|
'input' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
public $u;
|
|
|
|
public function __construct(Closure $u) {
|
|
|
|
$this->u = $u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'output' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
class A {
|
|
|
|
public Closure $u;
|
|
|
|
public function __construct(Closure $u) {
|
|
|
|
$this->u = $u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'php_version' => '7.4',
|
|
|
|
'issues_to_fix' => ['MissingPropertyType'],
|
|
|
|
'safe_types' => true,
|
|
|
|
],
|
2020-07-08 18:03:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|