2016-09-09 22:38:32 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function function_exists;
|
|
|
|
use function print_r;
|
2020-08-23 16:32:07 +02:00
|
|
|
|
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Type;
|
|
|
|
use function stripos;
|
2016-09-09 22:38:32 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class TypeParseTest extends TestCase
|
2016-09-09 22:38:32 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2017-12-19 04:07:52 +01:00
|
|
|
{
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2019-02-11 14:41:48 +01:00
|
|
|
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
|
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
|
|
|
|
$providers = new \Psalm\Internal\Provider\Providers(
|
|
|
|
$this->file_provider,
|
|
|
|
new \Psalm\Tests\Internal\Provider\FakeParserCacheProvider()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
|
|
|
|
$config,
|
2019-06-09 18:37:28 +02:00
|
|
|
$providers
|
2019-02-11 14:41:48 +01:00
|
|
|
);
|
2017-12-19 04:07:52 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testThisToStatic(): void
|
2018-03-22 22:55:36 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('static', (string) Type::parseString('$this'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testThisToStaticUnion(): void
|
2018-03-22 22:55:36 +01:00
|
|
|
{
|
2019-10-17 07:14:33 +02:00
|
|
|
$this->assertSame('A|static', (string) Type::parseString('$this|A'));
|
2018-03-22 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntOrString(): void
|
2016-09-09 22:38:32 +02:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('int|string', (string) Type::parseString('int|string'));
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBracketedIntOrString(): void
|
2018-03-23 03:28:06 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('int|string', (string) Type::parseString('(int|string)'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBoolOrIntOrString(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('bool|int|string', (string) Type::parseString('bool|int|string'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNullable(): void
|
2017-11-20 06:32:40 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('null|string', (string) Type::parseString('?string'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNullableUnion(): void
|
2018-08-29 22:03:16 +02:00
|
|
|
{
|
2019-10-17 07:14:33 +02:00
|
|
|
$this->assertSame('int|null|string', (string) Type::parseString('?(string|int)'));
|
2018-08-29 22:03:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNullableFullyQualified(): void
|
2018-05-21 18:55:44 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('null|stdClass', (string) Type::parseString('?\\stdClass'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNullableOrNullable(): void
|
2018-06-09 05:54:07 +02:00
|
|
|
{
|
2019-10-17 07:14:33 +02:00
|
|
|
$this->assertSame('int|null|string', (string) Type::parseString('?string|?int'));
|
2018-06-09 05:54:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadNullableCharacterInUnion(): void
|
2020-06-29 23:52:55 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('int|array|?');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadNullableCharacterInUnionWithFollowing(): void
|
2020-06-29 23:52:55 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('int|array|?|bool');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testArrayWithClosingBracket(): void
|
2016-09-22 06:31:07 +02:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('array<int, int>', (string) Type::parseString('array<int, int>'));
|
2019-06-15 23:57:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testArrayWithoutClosingBracket(): void
|
2019-06-15 23:57:40 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('array<int, int');
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testArrayWithSingleArg(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2019-01-05 06:15:53 +01:00
|
|
|
$this->assertSame('array<array-key, int>', (string) Type::parseString('array<int>'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testArrayWithNestedSingleArg(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2019-01-05 06:15:53 +01:00
|
|
|
$this->assertSame('array<array-key, array<array-key, int>>', (string) Type::parseString('array<array<int>>'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testArrayWithUnion(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('array<int|string, string>', (string) Type::parseString('array<int|string, string>'));
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNonEmptyArrray(): void
|
2019-02-27 15:08:27 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('non-empty-array<array-key, int>', (string) Type::parseString('non-empty-array<int>'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGeneric(): void
|
2016-09-22 06:31:07 +02:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('B<int>', (string) Type::parseString('B<int>'));
|
2016-09-09 22:38:32 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersection(): void
|
2018-03-18 18:57:04 +01:00
|
|
|
{
|
2018-09-07 17:09:07 +02:00
|
|
|
$this->assertSame('I1&I2&I3', (string) Type::parseString('I1&I2&I3'));
|
2018-03-18 18:57:04 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOrNull(): void
|
2018-03-22 22:55:36 +01:00
|
|
|
{
|
2019-10-17 07:14:33 +02:00
|
|
|
$this->assertSame('I1&I2|null', (string) Type::parseString('I1&I2|null'));
|
2018-03-22 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNullOrIntersection(): void
|
2018-03-22 22:55:36 +01:00
|
|
|
{
|
2019-10-17 07:14:33 +02:00
|
|
|
$this->assertSame('I1&I2|null', (string) Type::parseString('null|I1&I2'));
|
2018-03-22 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testInteratorAndTraversable(): void
|
2018-03-22 22:55:36 +01:00
|
|
|
{
|
2019-10-30 19:00:27 +01:00
|
|
|
$this->assertSame('Iterator<mixed, int>&Traversable', (string) Type::parseString('Iterator<int>&Traversable'));
|
2018-03-22 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testStaticAndStatic(): void
|
2020-06-07 02:10:50 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('static', (string) Type::parseString('static&static'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTraversableAndIteratorOrNull(): void
|
2018-03-22 22:55:36 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2019-10-30 19:00:27 +01:00
|
|
|
'Traversable&Iterator<mixed, int>|null',
|
2018-03-22 22:55:36 +01:00
|
|
|
(string) Type::parseString('Traversable&Iterator<int>|null')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIteratorAndTraversableOrNull(): void
|
2020-05-01 05:15:16 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'Iterator<mixed, int>&Traversable|null',
|
|
|
|
(string) Type::parseString('Iterator<mixed, int>&Traversable|null')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionAfterGeneric(): void
|
2018-09-07 17:09:07 +02:00
|
|
|
{
|
2018-12-05 16:36:42 +01:00
|
|
|
$this->assertSame('Countable&iterable<mixed, int>&I', (string) Type::parseString('Countable&iterable<int>&I'));
|
2018-09-07 17:09:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOfIterables(): void
|
2019-05-30 01:58:54 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('iterable<mixed, A>&iterable<mixed, B>', (string) Type::parseString('iterable<A>&iterable<B>'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOfTKeyedArray(): void
|
2020-04-08 15:35:53 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('array{a: int, b: int}', (string) Type::parseString('array{a: int}&array{b: int}'));
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:13:33 +02:00
|
|
|
public function testIntersectionOfTwoDifferentArrays(): void
|
|
|
|
{
|
|
|
|
$this->assertSame('array{a: int}<string, string>', Type::parseString('array{a: int}&array<string, string>')->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIntersectionOfTwoDifferentArraysReversed(): void
|
|
|
|
{
|
|
|
|
$this->assertSame('array{a: int}<string, string>', Type::parseString('array<string, string>&array{a: int}')->getId());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOfTKeyedArrayWithMergedProperties(): void
|
2020-04-08 15:35:53 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('array{a: int}', (string) Type::parseString('array{a: int}&array{a: mixed}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOfTKeyedArrayWithPossiblyUndefinedMergedProperties(): void
|
2020-04-08 15:35:53 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('array{a: int}', (string) Type::parseString('array{a: int}&array{a?: int}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOfTKeyedArrayWithConflictingProperties(): void
|
2020-04-08 15:35:53 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('array{a: string}&array{a: int}');
|
|
|
|
}
|
|
|
|
|
2020-10-07 23:47:17 +02:00
|
|
|
public function testIntersectionOfTwoRegularArrays() : void
|
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('string[]&array<string, string>');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testUnionOfIntersectionOfTKeyedArray(): void
|
2020-04-08 15:35:53 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('array{a: int|string, b?: int}', (string) Type::parseString('array{a: int}|array{a: string}&array{b: int}'));
|
|
|
|
$this->assertSame('array{a: int|string, b?: int}', (string) Type::parseString('array{b: int}&array{a: string}|array{a: int}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOfUnionOfTKeyedArray(): void
|
2020-04-08 15:35:53 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('array{a: int}&array{a: string}|array{b: int}');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIntersectionOfTKeyedArrayAndObject(): void
|
2020-04-08 15:35:53 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('array{a: int}&T1');
|
|
|
|
}
|
|
|
|
|
2020-08-30 17:44:14 +02:00
|
|
|
public function testIterableContainingTKeyedArray() : void
|
2019-06-30 03:46:01 +02:00
|
|
|
{
|
2020-05-11 15:08:53 +02:00
|
|
|
$this->assertSame('iterable<string, array{int}>', Type::parseString('iterable<string, array{int}>')->getId());
|
2019-06-30 03:46:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocSimpleArray(): void
|
2016-10-30 02:57:03 +02:00
|
|
|
{
|
2019-01-05 06:15:53 +01:00
|
|
|
$this->assertSame('array<array-key, A>', (string) Type::parseString('A[]'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocUnionArray(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2019-01-05 06:15:53 +01:00
|
|
|
$this->assertSame('array<array-key, A|B>', (string) Type::parseString('(A|B)[]'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocMultiDimensionalArray(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2019-01-05 06:15:53 +01:00
|
|
|
$this->assertSame('array<array-key, array<array-key, A>>', (string) Type::parseString('A[][]'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocMultidimensionalUnionArray(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2019-01-05 06:15:53 +01:00
|
|
|
$this->assertSame('array<array-key, array<array-key, A|B>>', (string) Type::parseString('(A|B)[][]'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocTKeyedArray(): void
|
2018-03-23 02:21:04 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2019-06-16 15:42:34 +02:00
|
|
|
'array<array-key, array{b: bool, d: string}>',
|
|
|
|
(string) Type::parseString('array{b: bool, d: string}[]')
|
2018-03-23 02:21:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocUnionOfArrays(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2019-01-05 06:15:53 +01:00
|
|
|
$this->assertSame('array<array-key, A|B>', (string) Type::parseString('A[]|B[]'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocUnionOfArraysOrObject(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2019-10-17 07:14:33 +02:00
|
|
|
$this->assertSame('C|array<array-key, A|B>', (string) Type::parseString('A[]|B[]|C'));
|
2016-10-30 02:57:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPsalmOnlyAtomic(): void
|
2018-03-05 22:06:06 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('class-string', (string) Type::parseString('class-string'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testParameterizedClassString(): void
|
2019-01-02 15:00:45 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('class-string<A>', (string) Type::parseString('class-string<A>'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testParameterizedClassStringUnion(): void
|
2019-06-27 17:44:35 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('class-string<A>|class-string<B>', (string) Type::parseString('class-string<A>|class-string<B>'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testInvalidType(): void
|
2017-10-12 20:02:06 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2017-10-12 20:02:06 +02:00
|
|
|
Type::parseString('array(A)');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBracketedUnionAndIntersection(): void
|
2018-03-23 03:28:06 +01:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-03-23 03:28:06 +01:00
|
|
|
Type::parseString('(A|B)&C');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBracketInUnion(): void
|
2018-06-30 19:09:05 +02:00
|
|
|
{
|
|
|
|
Type::parseString('null|(scalar|array|object)');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithSimpleArgs(): void
|
2016-09-22 06:31:07 +02:00
|
|
|
{
|
2019-06-16 15:42:34 +02:00
|
|
|
$this->assertSame('array{a: int, b: string}', (string) Type:: parseString('array{a: int, b: string}'));
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithSpace(): void
|
2020-04-26 23:36:02 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('array{\'a \': int, \'b \': string}', (string) Type:: parseString('array{\'a \': int, \'b \': string}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithQuotedKeys(): void
|
2020-04-26 23:36:02 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('array{\'\\"\': int, \'\\\'\': string}', (string) Type:: parseString('array{\'"\': int, \'\\\'\': string}'));
|
|
|
|
$this->assertSame('array{\'\\"\': int, \'\\\'\': string}', (string) Type:: parseString('array{"\\"": int, "\\\'": string}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithClassConstantKey(): void
|
2020-05-19 20:38:38 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('array{self::FOO: string}');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithQuotedClassConstantKey(): void
|
2020-05-19 20:38:38 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('array{\'self::FOO\': string}', (string) Type:: parseString('array{"self::FOO": string}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithoutClosingBracket(): void
|
2019-06-15 23:57:40 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2019-06-16 15:42:34 +02:00
|
|
|
Type::parseString('array{a: int, b: string');
|
2019-06-15 23:57:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayInType(): void
|
2020-04-28 21:18:50 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('array{a:[]}');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testObjectWithSimpleArgs(): void
|
2019-01-18 06:56:24 +01:00
|
|
|
{
|
|
|
|
$this->assertSame('object{a:int, b:string}', (string) Type::parseString('object{a:int, b:string}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testObjectWithDollarArgs(): void
|
2019-08-09 00:01:08 +02:00
|
|
|
{
|
|
|
|
$this->assertSame('object{a:int, $b:string}', (string) Type::parseString('object{a:int, $b:string}'));
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithUnionArgs(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: int|string, b: string}',
|
|
|
|
(string) Type::parseString('array{a: int|string, b: string}')
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithGenericArgs(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2019-10-17 07:14:33 +02:00
|
|
|
'array{a: array<int, int|string>, b: string}',
|
2019-06-16 15:42:34 +02:00
|
|
|
(string) Type::parseString('array{a: array<int, string|int>, b: string}')
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
2017-12-19 04:07:52 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithIntKeysAndUnionArgs(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2017-12-19 04:07:52 +01:00
|
|
|
$this->assertSame(
|
2020-05-11 15:08:53 +02:00
|
|
|
'array{null|stdClass}',
|
2017-12-19 04:07:52 +01:00
|
|
|
(string)Type::parseString('array{stdClass|null}')
|
|
|
|
);
|
2018-03-21 01:19:26 +01:00
|
|
|
}
|
2017-12-19 04:07:52 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayWithIntKeysAndGenericArgs(): void
|
2018-03-21 01:19:26 +01:00
|
|
|
{
|
2017-12-19 04:07:52 +01:00
|
|
|
$this->assertSame(
|
2020-05-11 15:08:53 +02:00
|
|
|
'array{array<array-key, mixed>}',
|
2017-12-19 04:07:52 +01:00
|
|
|
(string)Type::parseString('array{array}')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
2020-05-11 15:08:53 +02:00
|
|
|
'array{array<int, string>}',
|
2017-12-19 04:07:52 +01:00
|
|
|
(string)Type::parseString('array{array<int, string>}')
|
|
|
|
);
|
2018-03-17 04:37:10 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testTKeyedArrayOptional(): void
|
2018-03-17 04:37:10 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2019-06-16 15:42:34 +02:00
|
|
|
'array{a: int, b?: int}',
|
|
|
|
(string)Type::parseString('array{a: int, b?: int}')
|
2018-03-17 04:37:10 +01:00
|
|
|
);
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2018-03-27 04:13:10 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSimpleCallable(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2018-04-08 18:57:56 +02:00
|
|
|
'callable(int, string):void',
|
2018-03-27 04:13:10 +02:00
|
|
|
(string)Type::parseString('callable(int, string) : void')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithoutClosingBracket(): void
|
2019-06-15 23:57:40 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
Type::parseString('callable(int, string');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithParamNames(): void
|
2019-02-21 22:00:18 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable(int, string):void',
|
|
|
|
(string)Type::parseString('callable(int $foo, string $bar) : void')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableReturningIntersection(): void
|
2018-12-08 20:10:06 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable(int, string):I1&I2',
|
|
|
|
(string)Type::parseString('callable(int, string) : (I1&I2)')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEmptyCallable(): void
|
2018-03-28 16:53:19 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2018-04-08 18:57:56 +02:00
|
|
|
'callable():void',
|
2018-03-28 16:53:19 +02:00
|
|
|
(string)Type::parseString('callable() : void')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithUnionLastType(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2018-04-08 18:57:56 +02:00
|
|
|
'callable(int, int|string):void',
|
2018-03-27 04:13:10 +02:00
|
|
|
(string)Type::parseString('callable(int, int|string) : void')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithVariadic(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2018-04-08 18:57:56 +02:00
|
|
|
'callable(int, string...):void',
|
2018-03-27 06:12:41 +02:00
|
|
|
(string)Type::parseString('callable(int, string...) : void')
|
2018-03-27 04:13:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableThatReturnsACallable(): void
|
2018-04-18 19:23:48 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable():callable():string',
|
|
|
|
(string)Type::parseString('callable() : callable() : string')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableThatReturnsACallableThatReturnsACallable(): void
|
2018-11-06 14:53:14 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable():callable():callable():string',
|
|
|
|
(string)Type::parseString('callable() : callable() : callable() : string')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableOrInt(): void
|
2018-04-19 01:00:08 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable(string):void|int',
|
|
|
|
(string)Type::parseString('callable(string):void|int')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithGoodVariadic(): void
|
2019-08-09 00:01:08 +02:00
|
|
|
{
|
|
|
|
Type::parseString('callable(int, string...) : void');
|
|
|
|
Type::parseString('callable(int,string...) : void');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithSpreadBefore(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
2019-12-19 01:28:32 +01:00
|
|
|
$this->assertSame(
|
|
|
|
'callable(int, string...):void',
|
|
|
|
(string)Type::parseString('callable(int, ...string):void')
|
|
|
|
);
|
2018-03-27 06:12:41 +02:00
|
|
|
}
|
2020-03-22 15:44:48 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithSpaces(): void
|
2020-03-22 15:44:48 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is string ? string : int)',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string) Type::parseString('(T is string ? string : int)', null, ['T' => ['' => Type::getArray()]])
|
2020-03-22 15:44:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithUnion(): void
|
2020-03-22 15:44:48 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is string|true ? int|string : int)',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string) Type::parseString('(T is "hello"|true ? string|int : int)', null, ['T' => ['' => Type::getArray()]])
|
2020-03-22 15:44:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithTKeyedArray(): void
|
2020-05-01 04:02:37 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is array{a: string} ? string : int)',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string) Type::parseString('(T is array{a: string} ? string : int)', null, ['T' => ['' => Type::getArray()]])
|
2020-05-01 04:02:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithGenericIs(): void
|
2020-05-01 04:02:37 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is array<array-key, string> ? string : int)',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string) Type::parseString('(T is array<string> ? string : int)', null, ['T' => ['' => Type::getArray()]])
|
2020-05-01 04:02:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithIntersection(): void
|
2020-05-01 04:02:37 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is A&B ? string : int)',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string) Type::parseString('(T is A&B ? string : int)', null, ['T' => ['' => Type::getArray()]])
|
2020-05-01 04:02:37 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithoutSpaces(): void
|
2020-03-22 15:44:48 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is string ? string : int)',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string) Type::parseString('(T is string?string:int)', null, ['T' => ['' => Type::getArray()]])
|
2020-03-22 15:44:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithCallableElseBool(): void
|
2020-05-01 05:35:13 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2020-11-27 17:43:23 +01:00
|
|
|
Type::parseString('(T is string ? callable() : bool)', null, ['T' => ['' => Type::getArray()]]);
|
2020-05-01 05:35:13 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testConditionalTypeWithCallableReturningBoolElseBool(): void
|
2020-05-01 05:35:13 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is string ? callable():bool : bool)',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string) Type::parseString('(T is string ? (callable() : bool) : bool)', null, ['T' => ['' => Type::getArray()]])
|
2020-05-01 05:35:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-22 15:44:48 +01:00
|
|
|
public function testConditionalTypeWithGenerics() : void
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is string ? string : array<string, string>)',
|
2020-04-28 16:55:17 +02:00
|
|
|
(string) Type::parseString(
|
|
|
|
'(T is string ? string : array<string, string>)',
|
|
|
|
null,
|
2020-11-27 17:43:23 +01:00
|
|
|
['T' => ['' => Type::getArray()]]
|
2020-04-28 16:55:17 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:35:13 +02:00
|
|
|
public function testConditionalTypeWithCallableBracketed() : void
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is string ? callable(string, string):string : callable(mixed...):mixed)',
|
|
|
|
(string) Type::parseString(
|
|
|
|
'(T is string ? (callable(string, string):string) : (callable(mixed...):mixed))',
|
|
|
|
null,
|
2020-11-27 17:43:23 +01:00
|
|
|
['T' => ['' => Type::getArray()]]
|
2020-05-01 05:35:13 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConditionalTypeWithCallableNotBracketed() : void
|
2020-04-28 16:55:17 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'(T is string ? callable(string, string):string : callable(mixed...):mixed)',
|
|
|
|
(string) Type::parseString(
|
|
|
|
'(T is string ? callable(string, string):string : callable(mixed...):mixed)',
|
|
|
|
null,
|
2020-11-27 17:43:23 +01:00
|
|
|
['T' => ['' => Type::getArray()]]
|
2020-04-28 16:55:17 +02:00
|
|
|
)
|
2020-03-22 15:44:48 +01:00
|
|
|
);
|
|
|
|
}
|
2018-03-27 06:12:41 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithTrailingColon(): void
|
2018-04-16 00:16:31 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-04-16 00:16:31 +02:00
|
|
|
Type::parseString('callable(int):');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithAnotherBadVariadic(): void
|
2018-03-27 06:12:41 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-03-27 06:12:41 +02:00
|
|
|
Type::parseString('callable(int, string..) : void');
|
2018-03-27 04:13:10 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 17:01:46 +02:00
|
|
|
public function testCallableWithMissingVariadicType(): void
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable(mixed...):void',
|
|
|
|
(string) Type::parseString('callable(...): void')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithVariadicAndDefault(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-03-27 06:12:41 +02:00
|
|
|
Type::parseString('callable(int, string...=) : void');
|
2018-03-27 04:13:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadVariadic(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-03-27 06:12:41 +02:00
|
|
|
Type::parseString('string...');
|
2018-03-27 04:13:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadFullStop(): void
|
2018-03-27 20:43:39 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-03-27 20:43:39 +02:00
|
|
|
Type::parseString('string.');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadSemicolon(): void
|
2018-04-05 20:11:57 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-04-05 20:11:57 +02:00
|
|
|
Type::parseString('string;');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadGenericString(): void
|
2019-01-29 16:34:31 +01:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2019-01-29 16:34:31 +01:00
|
|
|
Type::parseString('string<T>');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadAmpersand(): void
|
2018-06-11 23:23:28 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-06-11 23:23:28 +02:00
|
|
|
Type::parseString('&array');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadColon(): void
|
2018-06-11 23:23:28 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-06-11 23:23:28 +02:00
|
|
|
Type::parseString(':array');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadBrackets(): void
|
2018-09-06 04:36:32 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-09-06 04:36:32 +02:00
|
|
|
Type::parseString('max(a)');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testMoreBadBrackets(): void
|
2018-09-06 04:40:52 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-09-06 04:40:52 +02:00
|
|
|
Type::parseString('max(a):void');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGeneratorWithWBadBrackets(): void
|
2018-09-06 06:41:07 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-09-06 06:41:07 +02:00
|
|
|
Type::parseString('Generator{string, A}');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadEquals(): void
|
2018-06-11 23:23:28 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-06-11 23:23:28 +02:00
|
|
|
Type::parseString('=array');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadBar(): void
|
2018-06-11 23:23:28 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-06-11 23:23:28 +02:00
|
|
|
Type::parseString('|array');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testBadColonDash(): void
|
2018-06-12 15:11:13 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-06-12 15:11:13 +02:00
|
|
|
Type::parseString('array|string:-');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testDoubleBar(): void
|
2018-03-29 08:20:19 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
2018-03-29 08:20:19 +02:00
|
|
|
Type::parseString('PDO||Closure|numeric');
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithDefault(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2018-04-08 18:57:56 +02:00
|
|
|
'callable(int, string=):void',
|
2018-03-27 04:13:10 +02:00
|
|
|
(string)Type::parseString('callable(int, string=) : void')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNestedCallable(): void
|
2018-11-06 14:53:14 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable(callable(A):B):C',
|
|
|
|
(string)Type::parseString('callable(callable(A):B):C')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCallableWithoutReturn(): void
|
2018-03-27 04:13:10 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'callable(int, string)',
|
|
|
|
(string)Type::parseString('callable(int, string)')
|
|
|
|
);
|
|
|
|
}
|
2018-04-19 01:00:08 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCombineLiteralStringWithClassString(): void
|
2019-01-29 16:34:31 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2019-11-30 18:57:18 +01:00
|
|
|
'class-string|string(array)',
|
|
|
|
Type::parseString('"array"|class-string')->getId()
|
2019-01-29 16:34:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testCombineLiteralClassStringWithClassString(): void
|
2019-01-29 16:34:31 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
2019-12-02 05:09:34 +01:00
|
|
|
'class-string',
|
2019-11-30 18:57:18 +01:00
|
|
|
Type::parseString('A::class|class-string')->getId()
|
2019-01-29 16:34:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testKeyOfClassConstant(): void
|
2019-05-24 04:27:36 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'key-of<Foo\Baz::BAR>',
|
|
|
|
(string)Type::parseString('key-of<Foo\Baz::BAR>')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testKeyOfTemplate(): void
|
2019-05-24 04:27:36 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'key-of<T>',
|
2020-11-27 17:43:23 +01:00
|
|
|
(string)Type::parseString('key-of<T>', null, ['T' => ['' => Type::getArray()]])
|
2019-05-24 04:27:36 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testIndexedAccess(): void
|
2019-05-24 04:27:36 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'T[K]',
|
2020-02-06 22:38:45 +01:00
|
|
|
(string) Type::parseString(
|
2019-05-24 04:27:36 +02:00
|
|
|
'T[K]',
|
|
|
|
null,
|
|
|
|
[
|
2020-11-27 17:43:23 +01:00
|
|
|
'T' => ['' => Type::getArray()],
|
|
|
|
'K' => ['' => new Type\Union([
|
2020-02-06 22:38:45 +01:00
|
|
|
new Type\Atomic\TTemplateKeyOf('T', 'fn-foo', Type::getMixed())
|
2020-11-27 17:43:23 +01:00
|
|
|
])],
|
2019-05-24 04:27:36 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testValueOfClassConstant(): void
|
2019-05-28 16:44:04 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'value-of<Foo\Baz::BAR>',
|
|
|
|
(string)Type::parseString('value-of<Foo\Baz::BAR>')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-27 18:49:28 +01:00
|
|
|
public function testClassStringMap() : void
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'class-string-map<T as Foo, T>',
|
|
|
|
(string)Type::parseString('class-string-map<T as Foo, T>')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testVeryLargeType(): void
|
2018-04-20 21:22:48 +02:00
|
|
|
{
|
2020-02-22 17:28:24 +01:00
|
|
|
$very_large_type = 'array{a: Closure():(array<array-key, mixed>|null), b?: Closure():array<array-key, mixed>, c?: Closure():array<array-key, mixed>, d?: Closure():array<array-key, mixed>, e?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), p?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), q: string, r?: Closure():(array<array-key, mixed>|null), s: array<array-key, mixed>}|null';
|
2018-04-20 21:22:48 +02:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$very_large_type,
|
|
|
|
(string) Type::parseString($very_large_type)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnum(): void
|
2018-05-20 23:19:53 +02:00
|
|
|
{
|
2019-01-28 00:34:13 +01:00
|
|
|
$docblock_type = Type::parseString('( \'foo\\\'with\' | "bar\"bar" | "baz" | "bat\\\\" | \'bang bang\' | 1 | 2 | 3 | 4.5)');
|
2018-05-20 23:19:53 +02:00
|
|
|
|
|
|
|
$resolved_type = new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralString('foo\'with'),
|
|
|
|
new Type\Atomic\TLiteralString('bar"bar'),
|
|
|
|
new Type\Atomic\TLiteralString('baz'),
|
|
|
|
new Type\Atomic\TLiteralString('bat\\'),
|
|
|
|
new Type\Atomic\TLiteralString('bang bang'),
|
|
|
|
new Type\Atomic\TLiteralInt(1),
|
|
|
|
new Type\Atomic\TLiteralInt(2),
|
2019-01-28 00:34:13 +01:00
|
|
|
new Type\Atomic\TLiteralInt(3),
|
2019-03-23 19:27:54 +01:00
|
|
|
new Type\Atomic\TLiteralFloat(4.5),
|
2018-05-20 23:19:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:29:26 +01:00
|
|
|
public function testEmptyString() : void
|
2019-12-11 16:52:46 +01:00
|
|
|
{
|
|
|
|
$docblock_type = Type::parseString('""|"admin"|"fun"');
|
|
|
|
|
|
|
|
$resolved_type = new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralString(''),
|
|
|
|
new Type\Atomic\TLiteralString('admin'),
|
|
|
|
new Type\Atomic\TLiteralString('fun'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
|
|
|
|
|
|
|
|
$docblock_type = Type::parseString('"admin"|""|"fun"');
|
|
|
|
|
|
|
|
$resolved_type = new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralString('admin'),
|
|
|
|
new Type\Atomic\TLiteralString(''),
|
|
|
|
new Type\Atomic\TLiteralString('fun'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
|
|
|
|
|
|
|
|
$docblock_type = Type::parseString('"admin"|"fun"|""');
|
|
|
|
|
|
|
|
$resolved_type = new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralString('admin'),
|
|
|
|
new Type\Atomic\TLiteralString('fun'),
|
|
|
|
new Type\Atomic\TLiteralString(''),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnumWithoutSpaces(): void
|
2018-05-21 06:46:56 +02:00
|
|
|
{
|
2019-01-28 00:34:13 +01:00
|
|
|
$docblock_type = Type::parseString('\'foo\\\'with\'|"bar\"bar"|"baz"|"bat\\\\"|\'bang bang\'|1|2|3|4.5');
|
2018-05-21 06:46:56 +02:00
|
|
|
|
|
|
|
$resolved_type = new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralString('foo\'with'),
|
|
|
|
new Type\Atomic\TLiteralString('bar"bar'),
|
|
|
|
new Type\Atomic\TLiteralString('baz'),
|
|
|
|
new Type\Atomic\TLiteralString('bat\\'),
|
|
|
|
new Type\Atomic\TLiteralString('bang bang'),
|
|
|
|
new Type\Atomic\TLiteralInt(1),
|
|
|
|
new Type\Atomic\TLiteralInt(2),
|
2019-01-28 00:34:13 +01:00
|
|
|
new Type\Atomic\TLiteralInt(3),
|
2019-03-23 19:27:54 +01:00
|
|
|
new Type\Atomic\TLiteralFloat(4.5),
|
2018-05-21 06:46:56 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSingleLiteralString(): void
|
2019-11-21 16:44:24 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'string',
|
|
|
|
(string)Type::parseString('"var"')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSingleLiteralInt(): void
|
2019-11-21 16:44:24 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'int',
|
|
|
|
(string)Type::parseString('6')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSingleLiteralFloat(): void
|
2019-11-21 16:44:24 +01:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'float',
|
|
|
|
(string)Type::parseString('6.315')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnumWithClassConstants(): void
|
2018-05-21 06:46:56 +02:00
|
|
|
{
|
|
|
|
$docblock_type = Type::parseString('("baz" | One2::TWO_THREE | Foo::BAR_BAR | Bat\Bar::BAZ_BAM)');
|
|
|
|
|
|
|
|
$resolved_type = new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralString('baz'),
|
|
|
|
new Type\Atomic\TScalarClassConstant('One2', 'TWO_THREE'),
|
|
|
|
new Type\Atomic\TScalarClassConstant('Foo', 'BAR_BAR'),
|
|
|
|
new Type\Atomic\TScalarClassConstant('Bat\\Bar', 'BAZ_BAM'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:22:43 +01:00
|
|
|
public function testIntMaskWithInts(): void
|
|
|
|
{
|
|
|
|
$docblock_type = Type::parseString('int-mask<0, 1, 2, 4>');
|
|
|
|
|
|
|
|
$this->assertSame('int(0)|int(1)|int(2)|int(3)|int(4)|int(5)|int(6)|int(7)', $docblock_type->getId());
|
|
|
|
|
|
|
|
$docblock_type = Type::parseString('int-mask<1, 2, 4>');
|
|
|
|
|
|
|
|
$this->assertSame('int(1)|int(2)|int(3)|int(4)|int(5)|int(6)|int(7)', $docblock_type->getId());
|
|
|
|
|
|
|
|
$docblock_type = Type::parseString('int-mask<1, 4>');
|
|
|
|
|
|
|
|
$this->assertSame('int(1)|int(4)|int(5)', $docblock_type->getId());
|
|
|
|
|
|
|
|
$docblock_type = Type::parseString('int-mask<PREG_PATTERN_ORDER, PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>');
|
|
|
|
|
|
|
|
$this->assertSame('int(1)|int(256)|int(257)|int(512)|int(513)|int(768)|int(769)', $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIntMaskWithClassConstant(): void
|
|
|
|
{
|
|
|
|
$docblock_type = Type::parseString('int-mask<0, A::FOO, A::BAR>');
|
|
|
|
|
|
|
|
$this->assertSame('int-mask<int(0), scalar-class-constant(A::FOO), scalar-class-constant(A::BAR)>', $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIntMaskWithInvalidClassConstant(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
|
|
|
|
Type::parseString('int-mask<A::*>');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIntMaskOfWithValidClassConstant(): void
|
|
|
|
{
|
|
|
|
$docblock_type = Type::parseString('int-mask-of<A::*>');
|
|
|
|
|
|
|
|
$this->assertSame('int-mask-of<scalar-class-constant(A::*)>', $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIntMaskOfWithInvalidClassConstant(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
|
|
|
|
|
|
|
|
Type::parseString('int-mask-of<A::FOO>');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIntMaskOfWithValidValueOf(): void
|
|
|
|
{
|
|
|
|
$docblock_type = Type::parseString('int-mask-of<value-of<A::FOO>>');
|
|
|
|
|
|
|
|
$this->assertSame('int-mask-of<value-of<A::FOO>>', $docblock_type->getId());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testReflectionTypeParse(): void
|
2019-01-27 23:16:22 +01:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
if (!function_exists('Psalm\Tests\someFunction')) {
|
|
|
|
/** @psalm-suppress UnusedParam */
|
2020-09-07 01:36:47 +02:00
|
|
|
function someFunction(string $param, array $param2, ?int $param3 = null) : string
|
2019-05-17 00:36:36 +02:00
|
|
|
{
|
|
|
|
return 'hello';
|
|
|
|
}
|
2019-01-27 23:16:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$reflectionFunc = new \ReflectionFunction('Psalm\Tests\someFunction');
|
|
|
|
$reflectionParams = $reflectionFunc->getParameters();
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'string',
|
2019-01-27 23:26:32 +01:00
|
|
|
(string) \Psalm\Codebase::getPsalmTypeFromReflection($reflectionParams[0]->getType())
|
2019-01-27 23:16:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'array<array-key, mixed>',
|
2019-01-27 23:26:32 +01:00
|
|
|
(string) \Psalm\Codebase::getPsalmTypeFromReflection($reflectionParams[1]->getType())
|
2019-01-27 23:16:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
2019-01-27 23:27:12 +01:00
|
|
|
'int|null',
|
2019-01-27 23:26:32 +01:00
|
|
|
(string) \Psalm\Codebase::getPsalmTypeFromReflection($reflectionParams[2]->getType())
|
2019-01-27 23:16:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'string',
|
2019-01-27 23:26:32 +01:00
|
|
|
(string) \Psalm\Codebase::getPsalmTypeFromReflection($reflectionFunc->getReturnType())
|
2019-01-27 23:16:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testValidCallMapType(): void
|
2019-02-07 18:25:57 +01:00
|
|
|
{
|
2020-05-25 19:10:06 +02:00
|
|
|
$callmap_types = \Psalm\Internal\Codebase\InternalCallMapHandler::getCallMap();
|
2019-01-09 03:44:50 +01:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
foreach ($callmap_types as $signature) {
|
|
|
|
$return_type = $signature[0] ?? null;
|
|
|
|
$param_type_1 = $signature[1] ?? null;
|
|
|
|
$param_type_2 = $signature[2] ?? null;
|
|
|
|
$param_type_3 = $signature[3] ?? null;
|
|
|
|
$param_type_4 = $signature[4] ?? null;
|
2018-04-19 01:00:08 +02:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
if ($return_type && $return_type !== 'void') {
|
|
|
|
if (stripos($return_type, 'oci-') !== false) {
|
2019-04-09 22:52:32 +02:00
|
|
|
continue;
|
2019-02-07 18:25:57 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 17:02:46 +02:00
|
|
|
try {
|
|
|
|
\Psalm\Type::parseString($return_type);
|
|
|
|
} catch (\Psalm\Exception\TypeParseTreeException $e) {
|
|
|
|
self::assertTrue(false, $e . ' | ' . print_r($signature, true));
|
|
|
|
}
|
2018-04-19 01:00:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
if ($param_type_1 && $param_type_1 !== 'mixed') {
|
|
|
|
if (stripos($param_type_1, 'oci-') !== false) {
|
2019-04-09 22:52:32 +02:00
|
|
|
continue;
|
2019-02-07 18:25:57 +01:00
|
|
|
}
|
2018-04-19 01:00:08 +02:00
|
|
|
|
2019-04-11 17:02:46 +02:00
|
|
|
try {
|
|
|
|
\Psalm\Type::parseString($param_type_1);
|
|
|
|
} catch (\Psalm\Exception\TypeParseTreeException $e) {
|
|
|
|
self::assertTrue(false, $e . ' | ' . print_r($signature, true));
|
|
|
|
}
|
2019-02-07 18:25:57 +01:00
|
|
|
}
|
2018-04-19 01:00:08 +02:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
if ($param_type_2 && $param_type_2 !== 'mixed') {
|
2019-04-11 17:02:46 +02:00
|
|
|
try {
|
|
|
|
\Psalm\Type::parseString($param_type_2);
|
|
|
|
} catch (\Psalm\Exception\TypeParseTreeException $e) {
|
|
|
|
self::assertTrue(false, $e . ' | ' . print_r($signature, true));
|
|
|
|
}
|
2019-02-07 18:25:57 +01:00
|
|
|
}
|
2018-04-19 01:00:08 +02:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
if ($param_type_3 && $param_type_3 !== 'mixed') {
|
2019-04-11 17:02:46 +02:00
|
|
|
try {
|
|
|
|
\Psalm\Type::parseString($param_type_3);
|
|
|
|
} catch (\Psalm\Exception\TypeParseTreeException $e) {
|
|
|
|
self::assertTrue(false, $e . ' | ' . print_r($signature, true));
|
|
|
|
}
|
2019-02-07 18:25:57 +01:00
|
|
|
}
|
2018-04-19 01:00:08 +02:00
|
|
|
|
2019-02-07 18:25:57 +01:00
|
|
|
if ($param_type_4 && $param_type_4 !== 'mixed') {
|
2019-04-11 17:02:46 +02:00
|
|
|
try {
|
|
|
|
\Psalm\Type::parseString($param_type_4);
|
|
|
|
} catch (\Psalm\Exception\TypeParseTreeException $e) {
|
|
|
|
self::assertTrue(false, $e . ' | ' . print_r($signature, true));
|
|
|
|
}
|
2019-02-07 18:25:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 01:00:08 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
}
|