1616use function array_map ;
1717use function array_unique ;
1818use function count ;
19- use function function_exists ;
2019use function in_array ;
21- use function is_null ;
22- use function openssl_get_cipher_methods ;
23- use function strtoupper ;
2420
2521#[AutowiredService]
2622final class OpensslCipherFunctionsReturnTypeExtension implements DynamicFunctionReturnTypeExtension
2723{
2824
29- /** @var string[]|null */
30- private ? array $ supportedAlgorithms = null ;
31-
32- public function __construct ( private PhpVersion $ phpVersion )
25+ public function __construct (
26+ private PhpVersion $ phpVersion ,
27+ private OpenSslCipherMethodsProvider $ cipherMethodsProvider ,
28+ )
3329 {
3430 }
3531
@@ -49,7 +45,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4945 }
5046
5147 $ strings = $ scope ->getType ($ functionCall ->getArgs ()[0 ]->value )->getConstantStrings ();
52- $ results = array_unique (array_map (fn (ConstantStringType $ algorithm ): bool => $ this ->isSupportedAlgorithm ($ algorithm ->getValue ()), $ strings ));
48+ $ results = array_unique (array_map (fn (ConstantStringType $ algorithm ): bool => $ this ->cipherMethodsProvider -> isSupportedCipherMethod ($ algorithm ->getValue ()), $ strings ));
5349
5450 if (count ($ results ) !== 1 ) {
5551 return null ;
@@ -66,25 +62,4 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
6662 : new ConstantBooleanType (false );
6763 }
6864
69- private function isSupportedAlgorithm (string $ algorithm ): bool
70- {
71- return in_array (strtoupper ($ algorithm ), $ this ->getSupportedAlgorithms (), true );
72- }
73-
74- /** @return string[] */
75- private function getSupportedAlgorithms (): array
76- {
77- if (!is_null ($ this ->supportedAlgorithms )) {
78- return $ this ->supportedAlgorithms ;
79- }
80-
81- $ supportedAlgorithms = [];
82- if (function_exists ('openssl_get_cipher_methods ' )) {
83- $ supportedAlgorithms = openssl_get_cipher_methods (true );
84- }
85- $ this ->supportedAlgorithms = array_map ('strtoupper ' , $ supportedAlgorithms );
86-
87- return $ this ->supportedAlgorithms ;
88- }
89-
9065}
0 commit comments