2022-01-21 13:59:55 +01:00
|
|
|
<?php
|
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-01-21 13:59:55 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
class NativeIntersectionsTest extends TestCase
|
|
|
|
{
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'nativeTypeIntersectionInConstructor' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface A {
|
|
|
|
}
|
|
|
|
interface B {
|
|
|
|
}
|
|
|
|
class Foo {
|
|
|
|
public function __construct(private A&B $self) {}
|
|
|
|
|
|
|
|
public function self(): A&B
|
|
|
|
{
|
|
|
|
return $this->self;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-01-21 13:59:55 +01:00
|
|
|
],
|
|
|
|
'nativeTypeIntersectionAsArgument' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface A {
|
|
|
|
function foo(): void;
|
|
|
|
}
|
|
|
|
interface B {
|
|
|
|
}
|
|
|
|
class C implements A, B {
|
|
|
|
function foo(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function test(A&B $in): void {
|
|
|
|
$in->foo();
|
|
|
|
}
|
|
|
|
test(new C());
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-01-21 13:59:55 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'invalidNativeIntersectionArgument' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface A {
|
|
|
|
function foo(): void;
|
|
|
|
}
|
|
|
|
interface B {
|
|
|
|
}
|
|
|
|
class C implements A {
|
|
|
|
function foo(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function test(A&B $in): void {
|
|
|
|
$in->foo();
|
|
|
|
}
|
|
|
|
test(new C());
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-01-21 13:59:55 +01:00
|
|
|
],
|
|
|
|
'mismatchDocblockNativeIntersectionArgument' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface A {
|
|
|
|
function foo(): void;
|
|
|
|
}
|
|
|
|
interface B {
|
|
|
|
}
|
|
|
|
interface C {
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param A&C $in
|
|
|
|
*/
|
|
|
|
function test(A&B $in): void {
|
|
|
|
$in->foo();
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'MismatchingDocblockParamType',
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-01-21 13:59:55 +01:00
|
|
|
],
|
2022-01-21 18:31:48 +01:00
|
|
|
'intersectionsNotAllowedWithUnions' => [
|
2022-01-22 01:00:45 +01:00
|
|
|
'code' => '<?php
|
2022-01-21 18:31:48 +01:00
|
|
|
interface A {
|
|
|
|
}
|
|
|
|
interface B {
|
|
|
|
}
|
|
|
|
interface C {
|
|
|
|
}
|
|
|
|
function foo (A&B|C $test): A&B|C {
|
|
|
|
return $test;
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParseError',
|
2022-01-22 01:00:45 +01:00
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-01-21 18:31:48 +01:00
|
|
|
],
|
|
|
|
'intersectionsNotAllowedWithNonClasses' => [
|
2022-01-22 01:00:45 +01:00
|
|
|
'code' => '<?php
|
2022-01-21 18:31:48 +01:00
|
|
|
interface A {
|
|
|
|
}
|
|
|
|
function foo (A&string $test): A&string {
|
|
|
|
return $test;
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParseError',
|
2022-01-22 01:00:45 +01:00
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-01-21 18:31:48 +01:00
|
|
|
],
|
|
|
|
'intersectionsNotAllowedInPHP80' => [
|
2022-01-22 01:00:45 +01:00
|
|
|
'code' => '<?php
|
2022-01-21 18:31:48 +01:00
|
|
|
interface A {
|
|
|
|
}
|
|
|
|
interface B {
|
|
|
|
}
|
|
|
|
function foo (A&B $test): A&B {
|
|
|
|
return $test;
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParseError',
|
2022-01-22 01:00:45 +01:00
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.0',
|
2022-01-21 18:31:48 +01:00
|
|
|
],
|
2022-01-21 13:59:55 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|