2020-08-30 22:08:22 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-08-30 22:08:22 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2020-08-30 22:08:22 +02:00
|
|
|
class MatchTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2020-08-30 22:08:22 +02:00
|
|
|
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:array<string>}>
|
2020-08-30 22:08:22 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2020-08-30 22:08:22 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'switchTruthy' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 22:08:22 +02:00
|
|
|
class A {
|
|
|
|
public ?string $a = null;
|
|
|
|
public ?string $b = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function f(A $obj): string {
|
|
|
|
return match (true) {
|
|
|
|
$obj->a !== null => $obj->a,
|
|
|
|
$obj->b !== null => $obj->b,
|
|
|
|
default => throw new \InvalidArgumentException("$obj->a or $obj->b must be set"),
|
|
|
|
};
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-08-30 22:08:22 +02:00
|
|
|
],
|
|
|
|
'defaultAboveCase' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 22:08:22 +02:00
|
|
|
function foo(string $a) : string {
|
|
|
|
return match ($a) {
|
|
|
|
"a" => "hello",
|
|
|
|
default => "yellow",
|
|
|
|
"b" => "goodbye",
|
|
|
|
};
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-08-30 22:08:22 +02:00
|
|
|
],
|
2020-08-30 22:23:53 +02:00
|
|
|
'allMatchedNoRedundantCondition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 22:23:53 +02:00
|
|
|
function foo() : string {
|
|
|
|
$a = rand(0, 1) ? "a" : "b";
|
|
|
|
return match ($a) {
|
|
|
|
"a" => "hello",
|
|
|
|
"b" => "goodbye",
|
|
|
|
};
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-08-30 22:23:53 +02:00
|
|
|
],
|
2020-11-13 17:55:42 +01:00
|
|
|
'getClassWithMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-13 17:55:42 +01:00
|
|
|
interface Foo {}
|
|
|
|
|
|
|
|
class Bar implements Foo
|
|
|
|
{
|
|
|
|
public function hello(): string
|
|
|
|
{
|
|
|
|
return "a";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(Foo $value): string {
|
|
|
|
return match (get_class($value)) {
|
|
|
|
Bar::class => $value->hello(),
|
|
|
|
default => "b",
|
|
|
|
};
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-11-13 17:55:42 +01:00
|
|
|
],
|
2020-08-30 22:08:22 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:array<string>,php_version?:string}>
|
2020-08-30 22:08:22 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2020-08-30 22:08:22 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'getClassArgWrongClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 22:08:22 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
2020-08-30 22:08:22 +02:00
|
|
|
|
|
|
|
$a = match (get_class($a)) {
|
|
|
|
A::class => $a->barBar(),
|
|
|
|
};',
|
|
|
|
'error_message' => 'UndefinedMethod',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-08-30 22:08:22 +02:00
|
|
|
],
|
|
|
|
'getClassMissingClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 22:08:22 +02:00
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
2020-08-30 22:08:22 +02:00
|
|
|
|
|
|
|
$a = match (get_class($a)) {
|
|
|
|
C::class => 5,
|
|
|
|
};',
|
|
|
|
'error_message' => 'UndefinedClass',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-08-30 22:08:22 +02:00
|
|
|
],
|
2020-08-30 22:23:53 +02:00
|
|
|
'allMatchedDefaultImpossible' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 22:23:53 +02:00
|
|
|
function foo() : string {
|
|
|
|
$a = rand(0, 1) ? "a" : "b";
|
|
|
|
return match ($a) {
|
|
|
|
"a" => "hello",
|
|
|
|
"b" => "goodbye",
|
|
|
|
default => "impossible",
|
|
|
|
};
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-08-30 22:23:53 +02:00
|
|
|
],
|
|
|
|
'allMatchedAnotherImpossible' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 22:23:53 +02:00
|
|
|
function foo() : string {
|
|
|
|
$a = rand(0, 1) ? "a" : "b";
|
|
|
|
return match ($a) {
|
|
|
|
"a" => "hello",
|
|
|
|
"b" => "goodbye",
|
|
|
|
"c" => "impossible",
|
|
|
|
};
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-08-30 22:23:53 +02:00
|
|
|
],
|
2020-09-01 04:59:47 +02:00
|
|
|
'notAllEnumsMet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-01 04:59:47 +02:00
|
|
|
/**
|
|
|
|
* @param "foo"|"bar" $foo
|
|
|
|
*/
|
|
|
|
function foo(string $foo): string {
|
|
|
|
return match ($foo) {
|
|
|
|
"foo" => "foo",
|
|
|
|
};
|
|
|
|
}',
|
2020-09-01 05:03:36 +02:00
|
|
|
'error_message' => 'UnhandledMatchCondition',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-09-01 04:59:47 +02:00
|
|
|
],
|
2020-09-01 05:23:24 +02:00
|
|
|
'notAllConstEnumsMet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-01 05:23:24 +02:00
|
|
|
class Airport {
|
|
|
|
const JFK = "jfk";
|
|
|
|
const LHR = "lhr";
|
|
|
|
const LGA = "lga";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param self::* $airport
|
|
|
|
*/
|
|
|
|
public static function getName(string $airport): string {
|
|
|
|
return match ($airport) {
|
|
|
|
self::JFK => "John F Kennedy Airport",
|
|
|
|
self::LHR => "London Heathrow",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnhandledMatchCondition',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-09-01 05:23:24 +02:00
|
|
|
],
|
2020-09-03 02:08:09 +02:00
|
|
|
'paradoxWithDuplicateValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-03 02:08:09 +02:00
|
|
|
function foo(int $i) : void {
|
|
|
|
echo match ($i) {
|
|
|
|
1 => 0,
|
|
|
|
1 => 1,
|
|
|
|
};
|
|
|
|
};',
|
|
|
|
'error_message' => 'ParadoxicalCondition',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-09-03 02:08:09 +02:00
|
|
|
],
|
2020-11-09 14:36:59 +01:00
|
|
|
'noCrashWithEmptyMatch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-09 14:36:59 +01:00
|
|
|
function foo(int $i) {
|
|
|
|
match ($i) {
|
|
|
|
|
|
|
|
};
|
|
|
|
}',
|
|
|
|
'error_message' => 'UnhandledMatchCondition',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-11-09 14:36:59 +01:00
|
|
|
],
|
2020-11-09 14:44:03 +01:00
|
|
|
'exitIsLikeThrow' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-09 14:44:03 +01:00
|
|
|
/**
|
|
|
|
* @param 1|2|3 $i
|
|
|
|
*/
|
|
|
|
function foo(int $i): void {
|
|
|
|
$a = match ($i) {
|
|
|
|
1 => exit(),
|
|
|
|
2, 3 => $i,
|
|
|
|
};
|
|
|
|
$a === "aaa";
|
|
|
|
}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-11-09 14:44:03 +01:00
|
|
|
],
|
2020-11-09 16:00:53 +01:00
|
|
|
'matchTrueImpossible' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-09 16:00:53 +01:00
|
|
|
$foo = new \stdClass();
|
|
|
|
$a = match (true) {
|
|
|
|
$foo instanceof \stdClass => 1,
|
|
|
|
$foo instanceof \Exception => 1,
|
|
|
|
};',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-11-09 16:00:53 +01:00
|
|
|
],
|
2020-08-30 22:08:22 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|