-
Notifications
You must be signed in to change notification settings - Fork 510
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
Open
stof
wants to merge
1
commit into
phpstan:1.12.x
Choose a base branch
from
stof:better_openssl_types
base: 1.12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Type/Php/OpensslCipherFunctionsReturnTypeExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?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; | ||
|
||
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; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
tests/PHPStan/Analyser/nsrt/openssl-cipher-iv-length-php7.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
tests/PHPStan/Analyser/nsrt/openssl-cipher-iv-length-php8.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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[]