Skip to content

Commit

Permalink
Do not check template type variance in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil committed Apr 1, 2023
1 parent eeccc36 commit 800e0c4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Rules/Generics/MethodSignatureVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function processNode(Node $node, Scope $scope): array
sprintf('in param-out type of parameter %%s of method %s::%s()', SprintfHelper::escapeFormatString($method->getDeclaringClass()->getDisplayName()), SprintfHelper::escapeFormatString($method->getName())),
sprintf('in return type of method %s::%s()', $method->getDeclaringClass()->getDisplayName(), $method->getName()),
sprintf('in method %s::%s()', $method->getDeclaringClass()->getDisplayName(), $method->getName()),
$method->getName() === '__construct' || $method->isStatic(),
$method->isPrivate(),
$method->isStatic(),
$method->isPrivate() || $method->getName() === '__construct',
);
}

Expand Down
5 changes: 0 additions & 5 deletions tests/PHPStan/Generics/data/variance-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
"line": 124,
"ignorable": true
},
{
"message": "Template type T is declared as covariant, but occurs in invariant position in parameter v of method PHPStan\\Generics\\Variance\\ConstructorAndStatic::__construct().",
"line": 142,
"ignorable": true
},
{
"message": "Template type T is declared as covariant, but occurs in invariant position in parameter v of method PHPStan\\Generics\\Variance\\ConstructorAndStatic::create().",
"line": 153,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public function testRule(): void
79,
],
]);

$this->analyse([__DIR__ . '/data/method-signature-variance-constructor.php'], []);
}

public function testBug8880(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace MethodSignatureVariance\Constructor;

/** @template-contravariant T */
interface In {
}

/** @template-covariant T */
interface Out {
}

/** @template T */
interface Invariant {
}

/** @template X */
class A {
/**
* @param X $a
* @param In<X> $b
* @param In<In<X>> $c
* @param In<Out<X>> $d
* @param In<Invariant<X>> $e
* @param Out<X> $f
* @param Out<In<X>> $g
* @param Out<Out<X>> $h
* @param Out<Invariant<X>> $i
* @param Invariant<X> $j
* @param Invariant<In<X>> $k
* @param Invariant<Out<X>> $l
*/
function __construct($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l) {}
}

/** @template-covariant X */
class B {
/**
* @param X $a
* @param In<X> $b
* @param In<In<X>> $c
* @param In<Out<X>> $d
* @param In<Invariant<X>> $e
* @param Out<X> $f
* @param Out<In<X>> $g
* @param Out<Out<X>> $h
* @param Out<Invariant<X>> $i
* @param Invariant<X> $j
* @param Invariant<In<X>> $k
* @param Invariant<Out<X>> $l
*/
function __construct($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l) {}
}

/** @template-contravariant X */
class C {
/**
* @param X $a
* @param In<X> $b
* @param In<In<X>> $c
* @param In<Out<X>> $d
* @param In<Invariant<X>> $e
* @param Out<X> $f
* @param Out<In<X>> $g
* @param Out<Out<X>> $h
* @param Out<Invariant<X>> $i
* @param Invariant<X> $j
* @param Invariant<In<X>> $k
* @param Invariant<Out<X>> $l
*/
function __construct($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l) {}
}

0 comments on commit 800e0c4

Please sign in to comment.