-
Notifications
You must be signed in to change notification settings - Fork 540
Add more precise return types for the openssl cipher functions #4043
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function array_map; | ||
use function array_unique; | ||
use function count; | ||
use function function_exists; | ||
use function in_array; | ||
use function is_null; | ||
use function openssl_get_cipher_methods; | ||
use function strtoupper; | ||
|
||
#[AutowiredService] | ||
Check failure on line 24 in src/Type/Php/OpensslCipherFunctionsReturnTypeExtension.php
|
||
final class OpensslCipherFunctionsReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
/** @var string[]|null */ | ||
private ?array $supportedAlgorithms = null; | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return in_array($functionReflection->getName(), ['openssl_cipher_iv_length', 'openssl_cipher_key_length'], true); | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type | ||
{ | ||
if (!$this->phpVersion->throwsValueErrorForInternalFunctions()) { | ||
return null; | ||
} | ||
|
||
if (count($functionCall->getArgs()) < 1) { | ||
return null; | ||
} | ||
|
||
$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 null; | ||
} | ||
|
||
$returnType = ParametersAcceptorSelector::selectFromArgs( | ||
$scope, | ||
$functionCall->getArgs(), | ||
$functionReflection->getVariants(), | ||
)->getReturnType(); | ||
|
||
return $results[0] | ||
? TypeCombinator::remove($returnType, new ConstantBooleanType(false)) | ||
: new ConstantBooleanType(false); | ||
} | ||
|
||
private function isSupportedAlgorithm(string $algorithm): bool | ||
{ | ||
return in_array(strtoupper($algorithm), $this->getSupportedAlgorithms(), true); | ||
} | ||
|
||
/** @return string[] */ | ||
private function getSupportedAlgorithms(): array | ||
{ | ||
if (!is_null($this->supportedAlgorithms)) { | ||
return $this->supportedAlgorithms; | ||
} | ||
|
||
$supportedAlgorithms = []; | ||
if (function_exists('openssl_get_cipher_methods')) { | ||
$supportedAlgorithms = openssl_get_cipher_methods(true); | ||
} | ||
$this->supportedAlgorithms = array_map('strtoupper', $supportedAlgorithms); | ||
|
||
return $this->supportedAlgorithms; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php // lint < 8.0 | ||
|
||
namespace OpensslCipherIvLengthPhp7; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class OpensslCipher | ||
{ | ||
|
||
/** | ||
* @param 'aes-256-cbc'|'aes128'|'aes-128-cbc' $validAlgorithms | ||
* @param 'aes-256-cbc'|'invalid' $validAndInvalidAlgorithms | ||
*/ | ||
public function doFoo(string $s, $validAlgorithms, $validAndInvalidAlgorithms) | ||
{ | ||
assertType('int|false', openssl_cipher_iv_length('aes-256-cbc')); | ||
assertType('int|false', openssl_cipher_iv_length('AES-256-CBC')); | ||
assertType('int|false', openssl_cipher_iv_length('unsupported')); | ||
assertType('int|false', openssl_cipher_iv_length($s)); | ||
assertType('int|false', openssl_cipher_iv_length($validAlgorithms)); | ||
assertType('int|false', openssl_cipher_iv_length($validAndInvalidAlgorithms)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace OpensslCipherIvLengthPhp8; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class OpensslCipher | ||
{ | ||
|
||
/** | ||
* @param 'aes-256-cbc'|'aes128'|'aes-128-cbc' $validAlgorithms | ||
* @param 'aes-256-cbc'|'invalid' $validAndInvalidAlgorithms | ||
*/ | ||
public function doFoo(string $s, $validAlgorithms, $validAndInvalidAlgorithms) | ||
{ | ||
assertType('int', openssl_cipher_iv_length('aes-256-cbc')); | ||
assertType('int', openssl_cipher_iv_length('AES-256-CBC')); | ||
assertType('false', openssl_cipher_iv_length('unsupported')); | ||
assertType('int|false', openssl_cipher_iv_length($s)); | ||
assertType('int', openssl_cipher_iv_length($validAlgorithms)); | ||
assertType('int|false', openssl_cipher_iv_length($validAndInvalidAlgorithms)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php // lint >= 8.2 | ||
|
||
namespace OpensslCipherKeyLength; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class OpensslCipher | ||
{ | ||
|
||
/** | ||
* @param 'aes-256-cbc'|'aes128'|'aes-128-cbc' $validAlgorithms | ||
* @param 'aes-256-cbc'|'invalid' $validAndInvalidAlgorithms | ||
*/ | ||
public function doFoo(string $s, $validAlgorithms, $validAndInvalidAlgorithms) | ||
{ | ||
assertType('int', openssl_cipher_key_length('aes-256-cbc')); | ||
assertType('int', openssl_cipher_key_length('AES-256-CBC')); | ||
assertType('false', openssl_cipher_key_length('unsupported')); | ||
assertType('int|false', openssl_cipher_key_length($s)); | ||
assertType('int', openssl_cipher_key_length($validAlgorithms)); | ||
assertType('int|false', openssl_cipher_key_length($validAndInvalidAlgorithms)); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might work to use a more precise return type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I inspired from MbFunctionsReturnTypeExtensionTrait, which also uses simply
string[]