Skip to content

Commit

Permalink
hide fixed static parameter variance behind a feature toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil committed Apr 2, 2023
1 parent 85bfdc7 commit 9ac499d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 1 deletion.
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
1 change: 1 addition & 0 deletions src/Rules/Generics/FunctionSignatureVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function processNode(Node $node, Scope $scope): array
sprintf('in return type of function %s()', $functionName),
sprintf('in function %s()', $functionName),
false,
false,
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Rules/Generics/MethodSignatureVarianceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ 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->isStatic(),
$method->isPrivate() || $method->getName() === '__construct',
);
}
Expand Down
6 changes: 5 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 All @@ -26,6 +27,7 @@ public function checkParametersAcceptor(
string $parameterOutTypeMessage,
string $returnTypeMessage,
string $generalMessage,
bool $isStatic,
bool $isPrivate,
): array
{
Expand All @@ -51,7 +53,9 @@ public function checkParametersAcceptor(
}

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

foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$type = $parameterReflection->getType();
Expand Down

0 comments on commit 9ac499d

Please sign in to comment.