2021-04-25 16:45:43 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getAllowedChildTypes
|
|
|
|
*/
|
|
|
|
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,
|
|
|
|
),
|
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
|
|
|
*/
|
|
|
|
public function getAllowedChildTypes(): array
|
|
|
|
{
|
|
|
|
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',
|
|
|
|
],
|
2021-04-27 17:26:27 +02:00
|
|
|
];
|
|
|
|
}
|
2021-04-25 16:45:43 +02:00
|
|
|
}
|