Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix position variance of static method parameters #2313

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ parameters:
instanceofType: true
paramOutVariance: true
allInvalidPhpDocs: true
strictStaticMethodTemplateTypeVariance: true
3 changes: 3 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ parameters:
instanceofType: false
paramOutVariance: false
allInvalidPhpDocs: false
strictStaticMethodTemplateTypeVariance: false
fileExtensions:
- php
checkAdvancedIsset: false
Expand Down Expand Up @@ -296,6 +297,7 @@ parametersSchema:
instanceofType: bool()
paramOutVariance: bool()
allInvalidPhpDocs: bool()
strictStaticMethodTemplateTypeVariance: bool()
])
fileExtensions: listOf(string())
checkAdvancedIsset: bool()
Expand Down Expand Up @@ -1050,6 +1052,7 @@ services:
class: PHPStan\Rules\Generics\VarianceCheck
arguments:
checkParamOutVariance: %featureToggles.paramOutVariance%
strictStaticVariance: %featureToggles.strictStaticMethodTemplateTypeVariance%

-
class: PHPStan\Rules\IssetCheck
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Generics/VarianceCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class VarianceCheck

public function __construct(
private bool $checkParamOutVariance,
private bool $strictStaticVariance,
)
{
}
Expand Down Expand Up @@ -52,7 +53,7 @@ public function checkParametersAcceptor(
}

$covariant = TemplateTypeVariance::createCovariant();
$parameterVariance = $isStatic
$parameterVariance = $isStatic && !$this->strictStaticVariance
? TemplateTypeVariance::createStatic()
: TemplateTypeVariance::createContravariant();

Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Generics/data/variance-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
"line": 124,
"ignorable": true
},
{
"message": "Template type T is declared as covariant, but occurs in contravariant position in parameter t of method PHPStan\\Generics\\Variance\\ConstructorAndStatic::create().",
"line": 153,
"ignorable": true
},
{
"message": "Template type T is declared as covariant, but occurs in contravariant position in parameter w of method PHPStan\\Generics\\Variance\\ConstructorAndStatic::create().",
"line": 153,
"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
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Generics/ClassAncestorsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function getRule(): Rule
new GenericAncestorsCheck(
$this->createReflectionProvider(),
new GenericObjectTypeCheck(),
new VarianceCheck(true),
new VarianceCheck(true, true),
true,
[],
),
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Generics/EnumAncestorsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function getRule(): Rule
new GenericAncestorsCheck(
$this->createReflectionProvider(),
new GenericObjectTypeCheck(),
new VarianceCheck(true),
new VarianceCheck(true, true),
true,
[],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function getRule(): Rule
new GenericAncestorsCheck(
$this->createReflectionProvider(),
new GenericObjectTypeCheck(),
new VarianceCheck(true),
new VarianceCheck(true, true),
true,
[],
),
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Generics/MethodSignatureVarianceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ public function testRule(): void
]);

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

$this->analyse([__DIR__ . '/data/method-signature-variance-static.php'], [
[
'Template type X is declared as covariant, but occurs in contravariant position in parameter a of method MethodSignatureVariance\StaticMethod\B::a().',
43,
],
[
'Template type X is declared as covariant, but occurs in contravariant position in parameter c of method MethodSignatureVariance\StaticMethod\B::a().',
43,
],
[
'Template type X is declared as covariant, but occurs in contravariant position in return type of method MethodSignatureVariance\StaticMethod\B::c().',
49,
],
[
'Template type X is declared as contravariant, but occurs in covariant position in parameter b of method MethodSignatureVariance\StaticMethod\C::a().',
62,
],
[
'Template type X is declared as contravariant, but occurs in covariant position in return type of method MethodSignatureVariance\StaticMethod\C::b().',
65,
],
[
'Template type X is declared as contravariant, but occurs in covariant position in return type of method MethodSignatureVariance\StaticMethod\C::d().',
71,
],
]);
}

public function testBug8880(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Generics/UsedTraitsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function getRule(): Rule
new GenericAncestorsCheck(
$this->createReflectionProvider(),
new GenericObjectTypeCheck(),
new VarianceCheck(true),
new VarianceCheck(true, true),
true,
[],
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace MethodSignatureVariance\StaticMethod;

/** @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 Out<X> $c
*/
static function a($a, $b, $c) {}

/** @return X */
static function b() {}

/** @return In<X> */
static function c() {}

/** @return Out<X> */
static function d() {}
}

/** @template-covariant X */
class B {
/**
* @param X $a
* @param In<X> $b
* @param Out<X> $c
*/
static function a($a, $b, $c) {}

/** @return X */
static function b() {}

/** @return In<X> */
static function c() {}

/** @return Out<X> */
static function d() {}
}

/** @template-contravariant X */
class C {
/**
* @param X $a
* @param In<X> $b
* @param Out<X> $c
*/
static function a($a, $b, $c) {}

/** @return X */
static function b() {}

/** @return In<X> */
static function c() {}

/** @return Out<X> */
static function d() {}
}