Skip to content

Commit 08e484d

Browse files
jiripudilondrejmirtes
authored andcommitted
test contravariance composition
1 parent ece68e7 commit 08e484d

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

tests/PHPStan/Type/Generic/GenericObjectTypeTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,76 @@ public function dataGetReferencedTypeArguments(): array
405405
),
406406
],
407407
],
408+
'param: In<T>' => [
409+
TemplateTypeVariance::createContravariant(),
410+
new GenericObjectType(D\In::class, [
411+
$templateType('T'),
412+
]),
413+
[
414+
new TemplateTypeReference(
415+
$templateType('T'),
416+
TemplateTypeVariance::createCovariant(),
417+
),
418+
],
419+
],
420+
'param: In<In<T>>' => [
421+
TemplateTypeVariance::createContravariant(),
422+
new GenericObjectType(D\In::class, [
423+
new GenericObjectType(D\In::class, [
424+
$templateType('T'),
425+
]),
426+
]),
427+
[
428+
new TemplateTypeReference(
429+
$templateType('T'),
430+
TemplateTypeVariance::createContravariant(),
431+
),
432+
],
433+
],
434+
'param: In<In<In<T>>>' => [
435+
TemplateTypeVariance::createContravariant(),
436+
new GenericObjectType(D\In::class, [
437+
new GenericObjectType(D\In::class, [
438+
new GenericObjectType(D\In::class, [
439+
$templateType('T'),
440+
]),
441+
]),
442+
]),
443+
[
444+
new TemplateTypeReference(
445+
$templateType('T'),
446+
TemplateTypeVariance::createCovariant(),
447+
),
448+
],
449+
],
450+
'param: In<Out<T>>' => [
451+
TemplateTypeVariance::createContravariant(),
452+
new GenericObjectType(D\In::class, [
453+
new GenericObjectType(D\Out::class, [
454+
$templateType('T'),
455+
]),
456+
]),
457+
[
458+
new TemplateTypeReference(
459+
$templateType('T'),
460+
TemplateTypeVariance::createCovariant(),
461+
),
462+
],
463+
],
464+
'param: Out<In<T>>' => [
465+
TemplateTypeVariance::createContravariant(),
466+
new GenericObjectType(D\Out::class, [
467+
new GenericObjectType(D\In::class, [
468+
$templateType('T'),
469+
]),
470+
]),
471+
[
472+
new TemplateTypeReference(
473+
$templateType('T'),
474+
TemplateTypeVariance::createCovariant(),
475+
),
476+
],
477+
],
408478
'return: Invariant<T>' => [
409479
TemplateTypeVariance::createCovariant(),
410480
new GenericObjectType(D\Invariant::class, [
@@ -473,6 +543,76 @@ public function dataGetReferencedTypeArguments(): array
473543
),
474544
],
475545
],
546+
'return: In<T>' => [
547+
TemplateTypeVariance::createCovariant(),
548+
new GenericObjectType(D\In::class, [
549+
$templateType('T'),
550+
]),
551+
[
552+
new TemplateTypeReference(
553+
$templateType('T'),
554+
TemplateTypeVariance::createContravariant(),
555+
),
556+
],
557+
],
558+
'return: In<In<T>>' => [
559+
TemplateTypeVariance::createCovariant(),
560+
new GenericObjectType(D\In::class, [
561+
new GenericObjectType(D\In::class, [
562+
$templateType('T'),
563+
]),
564+
]),
565+
[
566+
new TemplateTypeReference(
567+
$templateType('T'),
568+
TemplateTypeVariance::createCovariant(),
569+
),
570+
],
571+
],
572+
'return: In<In<In<T>>>' => [
573+
TemplateTypeVariance::createCovariant(),
574+
new GenericObjectType(D\In::class, [
575+
new GenericObjectType(D\In::class, [
576+
new GenericObjectType(D\In::class, [
577+
$templateType('T'),
578+
]),
579+
]),
580+
]),
581+
[
582+
new TemplateTypeReference(
583+
$templateType('T'),
584+
TemplateTypeVariance::createContravariant(),
585+
),
586+
],
587+
],
588+
'return: In<Out<T>>' => [
589+
TemplateTypeVariance::createCovariant(),
590+
new GenericObjectType(D\In::class, [
591+
new GenericObjectType(D\Out::class, [
592+
$templateType('T'),
593+
]),
594+
]),
595+
[
596+
new TemplateTypeReference(
597+
$templateType('T'),
598+
TemplateTypeVariance::createContravariant(),
599+
),
600+
],
601+
],
602+
'return: Out<In<T>>' => [
603+
TemplateTypeVariance::createCovariant(),
604+
new GenericObjectType(D\Out::class, [
605+
new GenericObjectType(D\In::class, [
606+
$templateType('T'),
607+
]),
608+
]),
609+
[
610+
new TemplateTypeReference(
611+
$templateType('T'),
612+
TemplateTypeVariance::createContravariant(),
613+
),
614+
],
615+
],
476616
];
477617
}
478618

tests/PHPStan/Type/Generic/data/generic-classes-d.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ interface Out {
1313
/** @return T */
1414
public function get();
1515
}
16+
17+
/** @template-contravariant T */
18+
interface In {
19+
/** @return T */
20+
public function get();
21+
}

0 commit comments

Comments
 (0)