Skip to content

Commit 96c6421

Browse files
committed
hide fixed static parameter variance behind a feature toggle
1 parent 85bfdc7 commit 96c6421

9 files changed

+15
-5
lines changed

conf/bleedingEdge.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ parameters:
3333
instanceofType: true
3434
paramOutVariance: true
3535
allInvalidPhpDocs: true
36+
strictStaticMethodTemplateTypeVariance: true

conf/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ parameters:
6363
instanceofType: false
6464
paramOutVariance: false
6565
allInvalidPhpDocs: false
66+
strictStaticMethodTemplateTypeVariance: false
6667
fileExtensions:
6768
- php
6869
checkAdvancedIsset: false
@@ -296,6 +297,7 @@ parametersSchema:
296297
instanceofType: bool()
297298
paramOutVariance: bool()
298299
allInvalidPhpDocs: bool()
300+
strictStaticMethodTemplateTypeVariance: bool()
299301
])
300302
fileExtensions: listOf(string())
301303
checkAdvancedIsset: bool()
@@ -1050,6 +1052,7 @@ services:
10501052
class: PHPStan\Rules\Generics\VarianceCheck
10511053
arguments:
10521054
checkParamOutVariance: %featureToggles.paramOutVariance%
1055+
strictStaticVariance: %featureToggles.strictStaticMethodTemplateTypeVariance%
10531056

10541057
-
10551058
class: PHPStan\Rules\IssetCheck

src/Rules/Generics/FunctionSignatureVarianceRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function processNode(Node $node, Scope $scope): array
3737
sprintf('in return type of function %s()', $functionName),
3838
sprintf('in function %s()', $functionName),
3939
false,
40+
false,
4041
);
4142
}
4243

src/Rules/Generics/MethodSignatureVarianceRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function processNode(Node $node, Scope $scope): array
3535
sprintf('in param-out type of parameter %%s of method %s::%s()', SprintfHelper::escapeFormatString($method->getDeclaringClass()->getDisplayName()), SprintfHelper::escapeFormatString($method->getName())),
3636
sprintf('in return type of method %s::%s()', $method->getDeclaringClass()->getDisplayName(), $method->getName()),
3737
sprintf('in method %s::%s()', $method->getDeclaringClass()->getDisplayName(), $method->getName()),
38+
$method->isStatic(),
3839
$method->isPrivate() || $method->getName() === '__construct',
3940
);
4041
}

src/Rules/Generics/VarianceCheck.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class VarianceCheck
1515

1616
public function __construct(
1717
private bool $checkParamOutVariance,
18+
private bool $strictStaticVariance,
1819
)
1920
{
2021
}
@@ -26,6 +27,7 @@ public function checkParametersAcceptor(
2627
string $parameterOutTypeMessage,
2728
string $returnTypeMessage,
2829
string $generalMessage,
30+
bool $isStatic,
2931
bool $isPrivate,
3032
): array
3133
{
@@ -51,7 +53,9 @@ public function checkParametersAcceptor(
5153
}
5254

5355
$covariant = TemplateTypeVariance::createCovariant();
54-
$parameterVariance = TemplateTypeVariance::createContravariant();
56+
$parameterVariance = $isStatic && !$this->strictStaticVariance
57+
? TemplateTypeVariance::createStatic()
58+
: TemplateTypeVariance::createContravariant();
5559

5660
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
5761
$type = $parameterReflection->getType();

tests/PHPStan/Rules/Generics/ClassAncestorsRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected function getRule(): Rule
1717
new GenericAncestorsCheck(
1818
$this->createReflectionProvider(),
1919
new GenericObjectTypeCheck(),
20-
new VarianceCheck(true),
20+
new VarianceCheck(true, true),
2121
true,
2222
[],
2323
),

tests/PHPStan/Rules/Generics/EnumAncestorsRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function getRule(): Rule
1818
new GenericAncestorsCheck(
1919
$this->createReflectionProvider(),
2020
new GenericObjectTypeCheck(),
21-
new VarianceCheck(true),
21+
new VarianceCheck(true, true),
2222
true,
2323
[],
2424
),

tests/PHPStan/Rules/Generics/InterfaceAncestorsRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected function getRule(): Rule
1717
new GenericAncestorsCheck(
1818
$this->createReflectionProvider(),
1919
new GenericObjectTypeCheck(),
20-
new VarianceCheck(true),
20+
new VarianceCheck(true, true),
2121
true,
2222
[],
2323
),

tests/PHPStan/Rules/Generics/UsedTraitsRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function getRule(): Rule
1919
new GenericAncestorsCheck(
2020
$this->createReflectionProvider(),
2121
new GenericObjectTypeCheck(),
22-
new VarianceCheck(true),
22+
new VarianceCheck(true, true),
2323
true,
2424
[],
2525
),

0 commit comments

Comments
 (0)