Skip to content

Commit f77b90e

Browse files
author
Kirill Nesmeyanov
committed
Fix FQN name building and add tests
1 parent 02c1272 commit f77b90e

19 files changed

+612
-6
lines changed

src/ReflectionReader.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ private function convertNamedType(\ReflectionNamedType $type): TypeStatement
110110

111111
private function convertNonNullNamedType(\ReflectionNamedType $type): TypeStatement
112112
{
113-
/** @var non-empty-string $name */
114-
$name = $type->getName();
113+
/** @var non-empty-string $literal */
114+
$literal = $type->getName();
115115

116-
$identifier = new Identifier($name);
116+
$name = new Name($literal);
117117

118-
if ($type->isBuiltin() || $identifier->isSpecial() || $identifier->isBuiltin()) {
119-
return new NamedTypeNode(new Name($identifier));
118+
if ($type->isBuiltin() || $name->isSpecial() || $name->isBuiltin()) {
119+
return new NamedTypeNode($name);
120120
}
121121

122-
return new NamedTypeNode(new FullQualifiedName($identifier));
122+
return new NamedTypeNode(new FullQualifiedName($name));
123123
}
124124

125125
/**
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Reader\Tests\Unit\Reader;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Group;
9+
use PHPUnit\Framework\Attributes\RequiresPhp;
10+
use TypeLang\Parser\Node\FullQualifiedName;
11+
use TypeLang\Parser\Node\Identifier;
12+
use TypeLang\Parser\Node\Stmt\IntersectionTypeNode;
13+
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
14+
use TypeLang\Parser\Node\Stmt\UnionTypeNode;
15+
use TypeLang\Reader\ConstantReaderInterface;
16+
use TypeLang\Reader\Tests\Unit\Reader\Stub\__ConstantReaderEnum;
17+
use TypeLang\Reader\Tests\Unit\Reader\Stub\ConstantReaderStub;
18+
19+
#[Group('unit'), Group('type-lang/reader')]
20+
class ConstantReaderTest extends ReaderTestCase
21+
{
22+
#[RequiresPhp('>=8.3')]
23+
#[DataProvider('readersDataProvider')]
24+
public function testSimpleType(ConstantReaderInterface $reader): void
25+
{
26+
$type = $reader->findConstantType(
27+
constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'SINGLE'),
28+
);
29+
30+
self::assertEquals(new NamedTypeNode(
31+
name: new Identifier('int'),
32+
), $type);
33+
}
34+
35+
#[RequiresPhp('>=8.3')]
36+
#[DataProvider('readersDataProvider')]
37+
public function testUnionType(ConstantReaderInterface $reader): void
38+
{
39+
$type = $reader->findConstantType(
40+
constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'UNION'),
41+
);
42+
43+
self::assertEquals(new UnionTypeNode(
44+
a: new NamedTypeNode('string'),
45+
b: new NamedTypeNode('int'),
46+
), $type);
47+
}
48+
49+
#[RequiresPhp('>=8.3')]
50+
#[DataProvider('readersDataProvider')]
51+
public function testIntersectionType(ConstantReaderInterface $reader): void
52+
{
53+
$type = $reader->findConstantType(
54+
constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'INTERSECTION'),
55+
);
56+
57+
self::assertEquals(new IntersectionTypeNode(
58+
a: new NamedTypeNode(new FullQualifiedName(__ConstantReaderEnum::class)),
59+
b: new NamedTypeNode(new FullQualifiedName(\BackedEnum::class)),
60+
), $type);
61+
}
62+
63+
#[RequiresPhp('>=8.3')]
64+
#[DataProvider('readersDataProvider')]
65+
public function testCompositeType(ConstantReaderInterface $reader): void
66+
{
67+
$type = $reader->findConstantType(
68+
constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'COMPOSITE'),
69+
);
70+
71+
self::assertEquals(new UnionTypeNode(
72+
a: new IntersectionTypeNode(
73+
a: new NamedTypeNode(new FullQualifiedName(__ConstantReaderEnum::class)),
74+
b: new NamedTypeNode(new FullQualifiedName(\BackedEnum::class)),
75+
),
76+
b: new NamedTypeNode('array'),
77+
), $type);
78+
}
79+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Reader\Tests\Unit\Reader;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Group;
9+
use PHPUnit\Framework\Attributes\RequiresPhp;
10+
use TypeLang\Parser\Node\FullQualifiedName;
11+
use TypeLang\Parser\Node\Identifier;
12+
use TypeLang\Parser\Node\Stmt\IntersectionTypeNode;
13+
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
14+
use TypeLang\Parser\Node\Stmt\UnionTypeNode;
15+
use TypeLang\Reader\FunctionReaderInterface;
16+
17+
#[Group('unit'), Group('type-lang/reader')]
18+
class FunctionReaderTest extends ReaderTestCase
19+
{
20+
#[DataProvider('readersDataProvider')]
21+
public function testSimpleType(FunctionReaderInterface $reader): void
22+
{
23+
require_once __DIR__ . '/Stub/functions_reader_stub.php';
24+
25+
$type = $reader->findFunctionType(
26+
function: new \ReflectionFunction('TypeLang\Reader\Tests\Unit\Reader\Stub\get_single_type'),
27+
);
28+
29+
self::assertEquals(new NamedTypeNode(
30+
name: new Identifier('int'),
31+
), $type);
32+
}
33+
34+
#[DataProvider('readersDataProvider')]
35+
public function testUnionType(FunctionReaderInterface $reader): void
36+
{
37+
require_once __DIR__ . '/Stub/functions_reader_stub.php';
38+
39+
$type = $reader->findFunctionType(
40+
function: new \ReflectionFunction('TypeLang\Reader\Tests\Unit\Reader\Stub\get_union_type'),
41+
);
42+
43+
self::assertEquals(new UnionTypeNode(
44+
a: new NamedTypeNode('string'),
45+
b: new NamedTypeNode('int'),
46+
), $type);
47+
}
48+
49+
#[DataProvider('readersDataProvider')]
50+
public function testIntersectionType(FunctionReaderInterface $reader): void
51+
{
52+
require_once __DIR__ . '/Stub/functions_reader_stub.php';
53+
54+
$type = $reader->findFunctionType(
55+
function: new \ReflectionFunction('TypeLang\Reader\Tests\Unit\Reader\Stub\get_intersection_type'),
56+
);
57+
58+
self::assertEquals(new IntersectionTypeNode(
59+
a: new NamedTypeNode(new FullQualifiedName(\ArrayAccess::class)),
60+
b: new NamedTypeNode(new FullQualifiedName(\Traversable::class)),
61+
), $type);
62+
}
63+
64+
#[RequiresPhp('>=8.2')]
65+
#[DataProvider('readersDataProvider')]
66+
public function testCompositeType(FunctionReaderInterface $reader): void
67+
{
68+
require_once __DIR__ . '/Stub/functions_reader_stub_php82.php';
69+
70+
$type = $reader->findFunctionType(
71+
function: new \ReflectionFunction('TypeLang\Reader\Tests\Unit\Reader\Stub\get_composite_type'),
72+
);
73+
74+
self::assertEquals(new UnionTypeNode(
75+
a: new IntersectionTypeNode(
76+
a: new NamedTypeNode(new FullQualifiedName(\ArrayAccess::class)),
77+
b: new NamedTypeNode(new FullQualifiedName(\Traversable::class)),
78+
),
79+
b: new NamedTypeNode('array'),
80+
), $type);
81+
}
82+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Reader\Tests\Unit\Reader;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Group;
9+
use PHPUnit\Framework\Attributes\RequiresPhp;
10+
use TypeLang\Parser\Node\FullQualifiedName;
11+
use TypeLang\Parser\Node\Identifier;
12+
use TypeLang\Parser\Node\Stmt\IntersectionTypeNode;
13+
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
14+
use TypeLang\Parser\Node\Stmt\UnionTypeNode;
15+
use TypeLang\Reader\FunctionReaderInterface;
16+
use TypeLang\Reader\Tests\Unit\Reader\Stub\MethodReaderStub;
17+
use TypeLang\Reader\Tests\Unit\Reader\Stub\MethodReaderStubPHP82;
18+
19+
#[Group('unit'), Group('type-lang/reader')]
20+
class MethodReaderTest extends ReaderTestCase
21+
{
22+
#[DataProvider('readersDataProvider')]
23+
public function testSimpleType(FunctionReaderInterface $reader): void
24+
{
25+
$type = $reader->findFunctionType(
26+
function: new \ReflectionMethod(MethodReaderStub::class, 'getSingleType'),
27+
);
28+
29+
self::assertEquals(new NamedTypeNode(
30+
name: new Identifier('int'),
31+
), $type);
32+
}
33+
34+
#[DataProvider('readersDataProvider')]
35+
public function testUnionType(FunctionReaderInterface $reader): void
36+
{
37+
$type = $reader->findFunctionType(
38+
function: new \ReflectionMethod(MethodReaderStub::class, 'getUnionType'),
39+
);
40+
41+
self::assertEquals(new UnionTypeNode(
42+
a: new NamedTypeNode('string'),
43+
b: new NamedTypeNode('int'),
44+
), $type);
45+
}
46+
47+
#[DataProvider('readersDataProvider')]
48+
public function testIntersectionType(FunctionReaderInterface $reader): void
49+
{
50+
$type = $reader->findFunctionType(
51+
function: new \ReflectionMethod(MethodReaderStub::class, 'getIntersectionType'),
52+
);
53+
54+
self::assertEquals(new IntersectionTypeNode(
55+
a: new NamedTypeNode(new FullQualifiedName(\ArrayAccess::class)),
56+
b: new NamedTypeNode(new FullQualifiedName(\Traversable::class)),
57+
), $type);
58+
}
59+
60+
#[RequiresPhp('>=8.2')]
61+
#[DataProvider('readersDataProvider')]
62+
public function testCompositeType(FunctionReaderInterface $reader): void
63+
{
64+
$type = $reader->findFunctionType(
65+
function: new \ReflectionMethod(MethodReaderStubPHP82::class, 'getCompositeType'),
66+
);
67+
68+
self::assertEquals(new UnionTypeNode(
69+
a: new IntersectionTypeNode(
70+
a: new NamedTypeNode(new FullQualifiedName(\ArrayAccess::class)),
71+
b: new NamedTypeNode(new FullQualifiedName(\Traversable::class)),
72+
),
73+
b: new NamedTypeNode('array'),
74+
), $type);
75+
}
76+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Reader\Tests\Unit\Reader;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Group;
9+
use PHPUnit\Framework\Attributes\RequiresPhp;
10+
use TypeLang\Parser\Node\FullQualifiedName;
11+
use TypeLang\Parser\Node\Identifier;
12+
use TypeLang\Parser\Node\Stmt\IntersectionTypeNode;
13+
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
14+
use TypeLang\Parser\Node\Stmt\UnionTypeNode;
15+
use TypeLang\Reader\ParameterReaderInterface;
16+
use TypeLang\Reader\Tests\Unit\Reader\Stub\MethodReaderStub;
17+
use TypeLang\Reader\Tests\Unit\Reader\Stub\ParameterReaderStub;
18+
use TypeLang\Reader\Tests\Unit\Reader\Stub\ParameterReaderStubPHP82;
19+
20+
#[Group('unit'), Group('type-lang/reader')]
21+
class ParameterReaderTest extends ReaderTestCase
22+
{
23+
#[DataProvider('readersDataProvider')]
24+
public function testSimpleType(ParameterReaderInterface $reader): void
25+
{
26+
$type = $reader->findParameterType(
27+
parameter: new \ReflectionParameter(ParameterReaderStub::withSingleType(...), 0),
28+
);
29+
30+
self::assertEquals(new NamedTypeNode(
31+
name: new Identifier('int'),
32+
), $type);
33+
}
34+
35+
#[DataProvider('readersDataProvider')]
36+
public function testUnionType(ParameterReaderInterface $reader): void
37+
{
38+
$type = $reader->findParameterType(
39+
parameter: new \ReflectionParameter(ParameterReaderStub::withUnionType(...), 0),
40+
);
41+
42+
self::assertEquals(new UnionTypeNode(
43+
a: new NamedTypeNode('string'),
44+
b: new NamedTypeNode('int'),
45+
), $type);
46+
}
47+
48+
#[DataProvider('readersDataProvider')]
49+
public function testIntersectionType(ParameterReaderInterface $reader): void
50+
{
51+
$type = $reader->findParameterType(
52+
parameter: new \ReflectionParameter(ParameterReaderStub::withIntersectionType(...), 0),
53+
);
54+
55+
self::assertEquals(new IntersectionTypeNode(
56+
a: new NamedTypeNode(new FullQualifiedName(\ArrayAccess::class)),
57+
b: new NamedTypeNode(new FullQualifiedName(\Traversable::class)),
58+
), $type);
59+
}
60+
61+
#[RequiresPhp('>=8.2')]
62+
#[DataProvider('readersDataProvider')]
63+
public function testCompositeType(ParameterReaderInterface $reader): void
64+
{
65+
$type = $reader->findParameterType(
66+
parameter: new \ReflectionParameter(ParameterReaderStubPHP82::withCompositeType(...), 0),
67+
);
68+
69+
self::assertEquals(new UnionTypeNode(
70+
a: new IntersectionTypeNode(
71+
a: new NamedTypeNode(new FullQualifiedName(\ArrayAccess::class)),
72+
b: new NamedTypeNode(new FullQualifiedName(\Traversable::class)),
73+
),
74+
b: new NamedTypeNode('array'),
75+
), $type);
76+
}
77+
}

0 commit comments

Comments
 (0)