Add more precise return types for the openssl cipher functions#4043
Add more precise return types for the openssl cipher functions#4043ondrejmirtes merged 1 commit intophpstan:2.1.xfrom
Conversation
6144182 to
ecfa6c2
Compare
| public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
| { | ||
| $returnType = ParametersAcceptorSelector::selectFromArgs( | ||
| $scope, | ||
| $functionCall->getArgs(), | ||
| $functionReflection->getVariants(), | ||
| )->getReturnType(); | ||
|
|
||
| if (!$this->phpVersion->throwsValueErrorForInternalFunctions()) { | ||
| return $returnType; | ||
| } | ||
|
|
||
| if (count($functionCall->getArgs()) < 1) { | ||
| return $returnType; | ||
| } | ||
|
|
||
| $strings = $scope->getType($functionCall->getArgs()[0]->value)->getConstantStrings(); | ||
| $results = array_unique(array_map(fn (ConstantStringType $algorithm): bool => $this->isSupportedAlgorithm($algorithm->getValue()), $strings)); | ||
|
|
||
| if (count($results) === 1) { | ||
| return $results[0] | ||
| ? TypeCombinator::remove($returnType, new ConstantBooleanType(false)) | ||
| : new ConstantBooleanType(false); | ||
| } | ||
|
|
||
| return $returnType; |
There was a problem hiding this comment.
instead of
$returnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$functionCall->getArgs(),
$functionReflection->getVariants(),
)->getReturnType();
you can just return null; for the cases where the extension cannot provide a better type then the default.
that way you can simplify the extension a bit.
There was a problem hiding this comment.
I refactored the code to return null for all such cases.
| return in_array(strtoupper($algorithm), $this->getSupportedAlgorithms(), true); | ||
| } | ||
|
|
||
| /** @return string[] */ |
There was a problem hiding this comment.
might work to use a more precise return type
| /** @return string[] */ | |
| /** @return uppercase-string[] */ |
There was a problem hiding this comment.
I inspired from MbFunctionsReturnTypeExtensionTrait, which also uses simply string[]
ecfa6c2 to
bb6367d
Compare
|
Needs cs fix |
bb6367d to
1e9efb6
Compare
|
@staabm done |
conf/config.neon
Outdated
| tags: | ||
| - phpstan.dynamicFunctionThrowTypeExtension | ||
|
|
||
| - |
There was a problem hiding this comment.
Please put this on 2.1.x and use AutowiredService attribute instead of changing config.neon. Thanks.
There was a problem hiding this comment.
@ondrejmirtes sorry for the delay. This is now done.
1e9efb6 to
1c9db7a
Compare
|
Thank you. |
|
@ondrejmirtes I discovered that in PHP <8.5, the Maybe this return type extension should be restricted to run only on PHP 8.5+ instead of PHP 8.0+. Or maybe it should perform actual filtering by attempting to read the iv length for PHP 8.0 to 8.4. |
|
Please open an issue, this comment would get lost. |
Those functions return false (and trigger a warning) only when the argument is an unknown algorithm, which is not something worth checking when using them with a known algorithm.
I'm providing this improvement only on PHP 8+, because PHP 7 can have other kind of warnings (that have been upgraded to
ValueErrorin PHP 8)Replaces #3582