1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/NativeUnionsTest.php

117 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class NativeUnionsTest extends TestCase
{
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
/**
2022-01-22 01:00:45 +01:00
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>,php_version?:string}>
*/
public function providerValidCodeParse(): iterable
{
return [
'nativeTypeUnionInConstructor' => [
2022-01-22 01:00:45 +01:00
'code' => '<?php
interface A {
}
interface B {
}
class Foo {
public function __construct(private A|B $self) {}
public function self(): A|B
{
return $this->self;
}
}',
'assertions' => [],
'error_levels' => [],
'php_version' => '8.0'
],
'nativeTypeUnionAsArgument' => [
2022-01-22 01:00:45 +01:00
'code' => '<?php
interface A {
function foo(): void;
}
interface B {
function foo(): void;
}
class C implements A {
function foo(): void {
}
}
function test(A|B $in): void {
$in->foo();
}
test(new C());
',
'assertions' => [],
'error_levels' => [],
'php_version' => '8.0'
],
'unionAndNullableEquivalent' => [
2022-01-22 01:00:45 +01:00
'code' => '<?php
function test(string|null $in): ?string {
return $in;
}
',
'assertions' => [],
'error_levels' => [],
'php_version' => '8.0'
],
];
}
/**
2022-01-22 01:00:45 +01:00
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
*/
public function providerInvalidCodeParse(): iterable
{
return [
'invalidNativeUnionArgument' => [
2022-01-22 01:00:45 +01:00
'code' => '<?php
function test(string|null $in): string|null {
return $in;
}
test(2);
',
'error_message' => 'InvalidScalarArgument',
2022-01-22 01:00:45 +01:00
'ignored_issues' => [],
'php_version' => '8.0'
],
'mismatchDocblockNativeUnionArgument' => [
2022-01-22 01:00:45 +01:00
'code' => '<?php
/**
* @param string|null $in
*/
function test(int|bool $in): bool {
return !!$in;
}
',
'error_message' => 'MismatchingDocblockParamType',
2022-01-22 01:00:45 +01:00
'ignored_issues' => [],
'php_version' => '8.0'
],
'unionsNotAllowedInPHP74' => [
2022-01-22 01:00:45 +01:00
'code' => '<?php
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' => [],
'php_version' => '7.4'
],
];
}
}