2021-04-25 16:45:43 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-25 16:45:43 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 01:10:21 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2021-04-25 16:45:43 +02:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
|
|
|
use Psalm\Internal\Type\TypeTokenizer;
|
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
|
|
|
use Psalm\Type;
|
2022-02-22 20:11:27 +01:00
|
|
|
use Psalm\Type\Atomic\TPropertiesOf;
|
2021-04-25 16:45:43 +02:00
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
use function array_diff_key;
|
|
|
|
use function array_keys;
|
|
|
|
use function array_map;
|
|
|
|
|
2021-04-25 16:45:43 +02:00
|
|
|
class TypeComparatorTest extends TestCase
|
|
|
|
{
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2021-04-25 16:45:43 +02:00
|
|
|
{
|
|
|
|
RuntimeCaches::clearAll();
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2021-04-25 16:45:43 +02:00
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$providers = new Providers(
|
2021-04-25 16:45:43 +02:00
|
|
|
$this->file_provider,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
2021-04-25 16:45:43 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2021-04-25 16:45:43 +02:00
|
|
|
$config,
|
2022-12-18 17:15:15 +01:00
|
|
|
$providers,
|
2021-04-25 16:45:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getAllBasicTypes
|
|
|
|
*/
|
|
|
|
public function testTypeAcceptsItself(string $type_string): void
|
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
$type_1 = Type::parseString($type_string);
|
|
|
|
$type_2 = Type::parseString($type_string);
|
2021-04-25 16:45:43 +02:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2021-12-03 20:11:20 +01:00
|
|
|
UnionTypeComparator::isContainedBy(
|
2021-04-25 16:45:43 +02:00
|
|
|
$this->project_analyzer->getCodebase(),
|
|
|
|
$type_1,
|
2022-12-18 17:15:15 +01:00
|
|
|
$type_2,
|
|
|
|
),
|
2021-04-25 16:45:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<array{string}>
|
2021-04-25 16:45:43 +02:00
|
|
|
*/
|
|
|
|
public function getAllBasicTypes(): array
|
|
|
|
{
|
|
|
|
// these types are not valid without generics attached
|
|
|
|
$basic_generic_types = [
|
|
|
|
'key-of' => true,
|
|
|
|
'arraylike-object' => true,
|
|
|
|
'value-of' => true,
|
|
|
|
'class-string-map' => true,
|
|
|
|
'int-mask-of' => true,
|
|
|
|
'int-mask' => true,
|
|
|
|
'pure-Closure' => true,
|
|
|
|
];
|
2022-02-22 20:11:27 +01:00
|
|
|
foreach (TPropertiesOf::tokenNames() as $token_name) {
|
|
|
|
$basic_generic_types[$token_name] = true;
|
|
|
|
}
|
2021-04-25 16:45:43 +02:00
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
$basic_types = array_diff_key(
|
2021-12-03 20:11:20 +01:00
|
|
|
TypeTokenizer::PSALM_RESERVED_WORDS,
|
2021-04-25 16:45:43 +02:00
|
|
|
$basic_generic_types,
|
|
|
|
[
|
|
|
|
'open-resource' => true, // unverifiable
|
|
|
|
'non-empty-countable' => true, // bit weird, maybe a bug?
|
2022-11-05 22:34:42 +01:00
|
|
|
],
|
|
|
|
[
|
2022-11-12 02:14:21 +01:00
|
|
|
'array' => true, // Requires a shape
|
|
|
|
'list' => true, // Requires a shape
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2021-04-25 16:45:43 +02:00
|
|
|
);
|
2022-11-12 02:14:21 +01:00
|
|
|
$basic_types['array{test: 123}'] = true;
|
|
|
|
$basic_types['list{123}'] = true;
|
2022-11-05 22:34:42 +01:00
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
return array_map(
|
2022-01-05 23:45:11 +01:00
|
|
|
fn($type) => [$type],
|
2022-12-18 17:15:15 +01:00
|
|
|
array_keys($basic_types),
|
2021-04-25 16:45:43 +02:00
|
|
|
);
|
|
|
|
}
|
2021-04-27 17:26:27 +02:00
|
|
|
|
|
|
|
/**
|
2023-03-11 05:40:39 +01:00
|
|
|
* @dataProvider getSuccessfulComparisons
|
2021-04-27 17:26:27 +02:00
|
|
|
*/
|
|
|
|
public function testTypeAcceptsType(string $parent_type_string, string $child_type_string): void
|
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
$parent_type = Type::parseString($parent_type_string);
|
|
|
|
$child_type = Type::parseString($child_type_string);
|
2021-04-27 17:26:27 +02:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2021-12-03 20:11:20 +01:00
|
|
|
UnionTypeComparator::isContainedBy(
|
2021-04-27 17:26:27 +02:00
|
|
|
$this->project_analyzer->getCodebase(),
|
|
|
|
$child_type,
|
2022-12-18 17:15:15 +01:00
|
|
|
$parent_type,
|
|
|
|
),
|
2023-03-11 05:40:39 +01:00
|
|
|
'Type ' . $parent_type_string . ' should contain ' . $child_type_string,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getUnsuccessfulComparisons
|
|
|
|
*/
|
|
|
|
public function testTypeDoesNotAcceptType(string $parent_type_string, string $child_type_string): void
|
|
|
|
{
|
|
|
|
$parent_type = Type::parseString($parent_type_string);
|
|
|
|
$child_type = Type::parseString($child_type_string);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
UnionTypeComparator::isContainedBy(
|
|
|
|
$this->project_analyzer->getCodebase(),
|
|
|
|
$child_type,
|
|
|
|
$parent_type,
|
|
|
|
),
|
|
|
|
'Type ' . $parent_type_string . ' should not contain ' . $child_type_string,
|
2021-04-27 17:26:27 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array<array{string, string}>
|
2021-04-27 17:26:27 +02:00
|
|
|
*/
|
2023-03-11 05:40:39 +01:00
|
|
|
public function getSuccessfulComparisons(): array
|
2021-04-27 17:26:27 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'iterableAcceptsArray' => [
|
|
|
|
'iterable',
|
2021-04-30 15:00:49 +02:00
|
|
|
'array',
|
|
|
|
],
|
|
|
|
'listAcceptsEmptyArray' => [
|
|
|
|
'list',
|
2021-10-13 19:37:47 +02:00
|
|
|
'array<never, never>',
|
2021-04-30 15:00:49 +02:00
|
|
|
],
|
|
|
|
'arrayAcceptsEmptyArray' => [
|
|
|
|
'array',
|
2021-10-13 19:37:47 +02:00
|
|
|
'array<never, never>',
|
2021-04-30 15:00:49 +02:00
|
|
|
],
|
|
|
|
'arrayOptionalKeyed1AcceptsEmptyArray' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'array{foo?: string}',
|
2021-10-13 19:37:47 +02:00
|
|
|
'array<never, never>',
|
2021-04-30 15:00:49 +02:00
|
|
|
],
|
|
|
|
'arrayOptionalKeyed2AcceptsEmptyArray' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'array{foo?: string}&array<string, mixed>',
|
2021-10-13 19:37:47 +02:00
|
|
|
'array<never, never>',
|
2021-04-27 17:26:27 +02:00
|
|
|
],
|
2021-05-21 13:44:26 +02:00
|
|
|
'Lowercase-stringAndCallable-string' => [
|
|
|
|
'lowercase-string',
|
|
|
|
'callable-string',
|
|
|
|
],
|
2023-04-19 11:37:04 +02:00
|
|
|
'callableUnionAcceptsCallableUnion' => [
|
2023-04-19 12:37:06 +02:00
|
|
|
'(callable(int,string[]): void)|(callable(int): void)',
|
|
|
|
'(callable(int): void)|(callable(int,string[]): void)',
|
2023-04-19 11:37:04 +02:00
|
|
|
],
|
2021-04-27 17:26:27 +02:00
|
|
|
];
|
|
|
|
}
|
2023-03-11 05:40:39 +01:00
|
|
|
|
|
|
|
/** @return iterable<string, list{string,string}> */
|
|
|
|
public function getUnsuccessfulComparisons(): iterable
|
|
|
|
{
|
|
|
|
yield 'genericListDoesNotAcceptListTupleWithMismatchedTypes' => [
|
|
|
|
'list<int>',
|
|
|
|
'list{int, string}',
|
|
|
|
];
|
|
|
|
yield 'genericListDoesNotAcceptArrayTupleWithMismatchedTypes' => [
|
|
|
|
'list<int>',
|
|
|
|
'array{int, string}',
|
|
|
|
];
|
2023-03-19 05:49:15 +01:00
|
|
|
yield 'nonEmptyMixedDoesNotAcceptMixed' => [
|
|
|
|
'non-empty-mixed',
|
|
|
|
'mixed',
|
|
|
|
];
|
2023-03-11 05:40:39 +01:00
|
|
|
}
|
2021-04-25 16:45:43 +02:00
|
|
|
}
|