Skip to content

Commit 87770ba

Browse files
committed
TDD test for replication of problem identified in github issue
1 parent e0b1371 commit 87770ba

8 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Forwarding;
6+
7+
/**
8+
* Notice the extra leading template TSelf that shifts T and U by +1
9+
*
10+
* @template TSelf of AbstractBox
11+
* @template T of ItemBase
12+
* @template U of ItemBase
13+
*
14+
* @implements BoxInterface<T, U>
15+
*/
16+
abstract class AbstractBox implements BoxInterface
17+
{
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Forwarding;
6+
7+
final class BoxConsumer
8+
{
9+
/**
10+
* @template T of ItemBase
11+
* @template U of ItemBase
12+
*
13+
* @param BoxInterface<T, U> $box
14+
*/
15+
public function __construct(public readonly BoxInterface $box)
16+
{
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Forwarding;
6+
7+
/**
8+
* @template T of ItemBase
9+
* @template U of ItemBase
10+
*/
11+
interface BoxInterface
12+
{
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Forwarding;
6+
7+
/**
8+
* @extends AbstractBox<ConcreteBox, ItemA, ItemB>
9+
*/
10+
final class ConcreteBox extends AbstractBox
11+
{
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Forwarding;
6+
7+
final class ItemA implements ItemBase
8+
{
9+
public function __construct(public string $name = 'A')
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Forwarding;
6+
7+
final class ItemB implements ItemBase
8+
{
9+
public function __construct(public string $name = 'B')
10+
{
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Forwarding;
6+
7+
interface ItemBase
8+
{
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\TypeChecking\Generics;
6+
7+
use TypePHP\Tests\Fixtures\Forwarding\BoxConsumer;
8+
use TypePHP\Tests\Fixtures\Forwarding\ConcreteBox;
9+
use TypePHP\Tests\Fixtures\Forwarding\ItemA;
10+
use TypePHP\Tests\Fixtures\Forwarding\ItemB;
11+
use TypePHP\TypePHP;
12+
13+
describe('Generic Template Forwarding Across Abstract Hierarchies with Shifted Template Indexes', function () {
14+
test('infers method template arguments T and U from implementing class when abstract parent has extra leading template', function () {
15+
$concreteBox = new ConcreteBox();
16+
17+
$consumer = new BoxConsumer($concreteBox);
18+
19+
expect($consumer->box)->toBe($concreteBox);
20+
});
21+
22+
test('reifies bound generic types on concrete box instance matching both interface and class templates', function () {
23+
$concreteBox = new ConcreteBox();
24+
25+
expect(TypePHP::getGenericType($concreteBox, 'T'))->toBe(ItemA::class)
26+
->and(TypePHP::getGenericType($concreteBox, 'U'))->toBe(ItemB::class)
27+
;
28+
});
29+
});

0 commit comments

Comments
 (0)