mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Daniil Gentili
1986c8b4a8
* Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
255 lines
8.6 KiB
PHP
255 lines
8.6 KiB
PHP
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
class MatchTest extends TestCase
|
|
{
|
|
use InvalidCodeAnalysisTestTrait;
|
|
use ValidCodeAnalysisTestTrait;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function providerValidCodeParse(): iterable
|
|
{
|
|
return [
|
|
'switchTruthy' => [
|
|
'code' => '<?php
|
|
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"),
|
|
};
|
|
}',
|
|
'assertions' => [],
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'defaultAboveCase' => [
|
|
'code' => '<?php
|
|
function foo(string $a) : string {
|
|
return match ($a) {
|
|
"a" => "hello",
|
|
default => "yellow",
|
|
"b" => "goodbye",
|
|
};
|
|
}',
|
|
'assertions' => [],
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'allMatchedNoRedundantCondition' => [
|
|
'code' => '<?php
|
|
function foo() : string {
|
|
$a = rand(0, 1) ? "a" : "b";
|
|
return match ($a) {
|
|
"a" => "hello",
|
|
"b" => "goodbye",
|
|
};
|
|
}',
|
|
'assertions' => [],
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'getClassWithMethod' => [
|
|
'code' => '<?php
|
|
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",
|
|
};
|
|
}',
|
|
'assertions' => [],
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'MatchWithCount' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @return non-empty-array
|
|
*/
|
|
function test(array $array): array
|
|
{
|
|
return match (\count($array)) {
|
|
0 => throw new \InvalidArgumentException,
|
|
default => $array,
|
|
};
|
|
}',
|
|
'assertions' => [],
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.1'
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function providerInvalidCodeParse(): iterable
|
|
{
|
|
return [
|
|
'getClassArgWrongClass' => [
|
|
'code' => '<?php
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
$a = match (get_class($a)) {
|
|
A::class => $a->barBar(),
|
|
};',
|
|
'error_message' => 'UndefinedMethod',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'getClassMissingClass' => [
|
|
'code' => '<?php
|
|
class A {}
|
|
class B {}
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
$a = match (get_class($a)) {
|
|
C::class => 5,
|
|
};',
|
|
'error_message' => 'UndefinedClass',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'allMatchedDefaultImpossible' => [
|
|
'code' => '<?php
|
|
function foo() : string {
|
|
$a = rand(0, 1) ? "a" : "b";
|
|
return match ($a) {
|
|
"a" => "hello",
|
|
"b" => "goodbye",
|
|
default => "impossible",
|
|
};
|
|
}',
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'allMatchedAnotherImpossible' => [
|
|
'code' => '<?php
|
|
function foo() : string {
|
|
$a = rand(0, 1) ? "a" : "b";
|
|
return match ($a) {
|
|
"a" => "hello",
|
|
"b" => "goodbye",
|
|
"c" => "impossible",
|
|
};
|
|
}',
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0'
|
|
],
|
|
'notAllEnumsMet' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @param "foo"|"bar" $foo
|
|
*/
|
|
function foo(string $foo): string {
|
|
return match ($foo) {
|
|
"foo" => "foo",
|
|
};
|
|
}',
|
|
'error_message' => 'UnhandledMatchCondition',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0',
|
|
],
|
|
'notAllConstEnumsMet' => [
|
|
'code' => '<?php
|
|
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',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0',
|
|
],
|
|
'paradoxWithDuplicateValue' => [
|
|
'code' => '<?php
|
|
function foo(int $i) : void {
|
|
echo match ($i) {
|
|
1 => 0,
|
|
1 => 1,
|
|
};
|
|
};',
|
|
'error_message' => 'ParadoxicalCondition',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0',
|
|
],
|
|
'noCrashWithEmptyMatch' => [
|
|
'code' => '<?php
|
|
function foo(int $i) {
|
|
match ($i) {
|
|
|
|
};
|
|
}',
|
|
'error_message' => 'UnhandledMatchCondition',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0',
|
|
],
|
|
'exitIsLikeThrow' => [
|
|
'code' => '<?php
|
|
/**
|
|
* @param 1|2|3 $i
|
|
*/
|
|
function foo(int $i): void {
|
|
$a = match ($i) {
|
|
1 => exit(),
|
|
2, 3 => $i,
|
|
};
|
|
$a === "aaa";
|
|
}',
|
|
'error_message' => 'DocblockTypeContradiction',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0',
|
|
],
|
|
'matchTrueImpossible' => [
|
|
'code' => '<?php
|
|
$foo = new \stdClass();
|
|
$a = match (true) {
|
|
$foo instanceof \stdClass => 1,
|
|
$foo instanceof \Exception => 1,
|
|
};',
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
'ignored_issues' => [],
|
|
'php_version' => '8.0',
|
|
],
|
|
];
|
|
}
|
|
}
|