Skip to content

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
wants to merge 1 commit into
base: 1.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,11 @@ services:
tags:
- phpstan.dynamicFunctionThrowTypeExtension

-
class: PHPStan\Type\Php\OpensslCipherFunctionsReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\OpenSslEncryptParameterOutTypeExtension
tags:
Expand Down
88 changes: 88 additions & 0 deletions src/Type/Php/OpensslCipherFunctionsReturnTypeExtension.php
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[] */
Copy link
Contributor

@staabm staabm Jun 4, 2025

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

Suggested change
/** @return string[] */
/** @return uppercase-string[] */

Copy link
Contributor Author

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[]

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 tests/PHPStan/Analyser/nsrt/openssl-cipher-iv-length-php7.php
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 tests/PHPStan/Analyser/nsrt/openssl-cipher-iv-length-php8.php
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));
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/openssl-cipher-key-length.php
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));
}

}
Loading