2022-01-21 13:59:55 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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
|
|
|
],
|
2024-02-19 12:08:46 +01:00
|
|
|
'nativeTypeIntersectionAsClassProperty' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface A {}
|
|
|
|
interface B {}
|
|
|
|
class C implements A, B {}
|
|
|
|
class D {
|
|
|
|
private A&B $intersection;
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->intersection = new C();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
|
|
|
],
|
|
|
|
'nativeTypeIntersectionAsClassPropertyUsingProcessedInterfaces' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface A {}
|
|
|
|
interface B {}
|
|
|
|
class AB implements A, B {}
|
|
|
|
class C {
|
|
|
|
private A&B $other;
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->other = new AB();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
|
|
|
],
|
|
|
|
'nativeTypeIntersectionAsClassPropertyUsingUnprocessedInterfaces' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class StringableJson implements \Stringable, \JsonSerializable {
|
|
|
|
public function jsonSerialize(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return json_encode($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C {
|
|
|
|
private \Stringable&\JsonSerializable $other;
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->other = new StringableJson();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'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
|
|
|
],
|
2024-02-19 12:08:46 +01:00
|
|
|
'nativeTypeIntersectionAsClassPropertyUsingUnknownInterfaces' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class C {
|
|
|
|
private \Example\Unknown\A&\Example\Unknown\B $other;
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->other = new \Example\Unknown\AB();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
// @todo decide whether a fall-back should be implemented, that allows to by-pass this failure (opt-in config)
|
|
|
|
// `UndefinedClass - src/somefile.php:3:33 - Class, interface or enum named Example\Unknown\B does not exist`
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
|
|
|
],
|
2022-01-21 13:59:55 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|