2020-05-18 22:22:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
class CloneTest extends TestCase
|
|
|
|
{
|
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2020-05-18 22:22:50 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'cloneCorrect' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
function foo(A $a) : A {
|
|
|
|
return clone $a;
|
|
|
|
}
|
|
|
|
$a = foo(new A());',
|
|
|
|
],
|
|
|
|
'cloneCorrectWithPublicMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __clone() {}
|
|
|
|
}
|
|
|
|
function foo(A $a) : A {
|
|
|
|
return clone $a;
|
|
|
|
}
|
|
|
|
foo(new A());',
|
|
|
|
],
|
|
|
|
'clonePrivateInternally' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private function __clone() {}
|
|
|
|
public function foo(): self {
|
|
|
|
return clone $this;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2020-05-18 22:22:50 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'invalidIntClone' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
clone $a;',
|
|
|
|
'error_message' => 'InvalidClone',
|
|
|
|
],
|
2020-05-18 23:18:13 +02:00
|
|
|
'possiblyInvalidIntClone' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? 5 : new Exception();
|
|
|
|
clone $a;',
|
|
|
|
'error_message' => 'PossiblyInvalidClone',
|
|
|
|
],
|
2020-05-18 22:22:50 +02:00
|
|
|
'invalidMixedClone' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed $a */
|
|
|
|
$a = 5;
|
|
|
|
clone $a;',
|
2020-05-18 23:18:13 +02:00
|
|
|
'error_message' => 'MixedClone',
|
2020-05-18 22:22:50 +02:00
|
|
|
],
|
|
|
|
'notVisibleCloneMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private function __clone() {}
|
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
clone $a;',
|
|
|
|
'error_message' => 'InvalidClone',
|
|
|
|
],
|
|
|
|
'invalidGenericClone' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as int|string
|
|
|
|
* @param T $a
|
|
|
|
*/
|
|
|
|
function foo($a): void {
|
|
|
|
clone $a;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidClone',
|
|
|
|
],
|
|
|
|
'possiblyInvalidGenericClone' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
2020-05-18 23:18:13 +02:00
|
|
|
* @template T as int|Exception
|
2020-05-18 22:22:50 +02:00
|
|
|
* @param T $a
|
|
|
|
*/
|
|
|
|
function foo($a): void {
|
|
|
|
clone $a;
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyInvalidClone',
|
|
|
|
],
|
2020-05-18 23:18:13 +02:00
|
|
|
'mixedGenericClone' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T $a
|
|
|
|
*/
|
|
|
|
function foo($a): void {
|
|
|
|
clone $a;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedClone',
|
|
|
|
],
|
2020-05-18 22:22:50 +02:00
|
|
|
'mixedTypeInferredIfErrors' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
/**
|
|
|
|
* @param A|string $a
|
|
|
|
*/
|
|
|
|
function foo($a): void {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress PossiblyInvalidClone
|
|
|
|
*/
|
|
|
|
$cloned = clone $a;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedAssignment',
|
2020-07-01 16:10:55 +02:00
|
|
|
],
|
|
|
|
'missingClass' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress UndefinedDocblockClass
|
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
* @return Editable
|
|
|
|
*/
|
|
|
|
function get() {}
|
|
|
|
|
|
|
|
/** @psalm-suppress UndefinedDocblockClass */
|
|
|
|
clone get();',
|
|
|
|
'error_message' => 'InvalidClone',
|
|
|
|
],
|
2020-05-18 22:22:50 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|