Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Hardcode possible hmac algorithms
  • Loading branch information
Pascal Heidmann committed Oct 1, 2020
commit c10c1873963e505b0554c76003b28ade13b0592a
50 changes: 48 additions & 2 deletions src/Type/Php/HashHmacFunctionsReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,53 @@
final class HashHmacFunctionsReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

private const HMAC_ALGORITHMS = [
'md2',
'md4',
'md5',
'sha1',
'sha224',
'sha256',
'sha384',
'sha512/224',
'sha512/256',
'sha512',
'sha3-224',
'sha3-256',
'sha3-384',
'sha3-512',
'ripemd128',
'ripemd160',
'ripemd256',
'ripemd320',
'whirlpool',
'tiger128,3',
'tiger160,3',
'tiger192,3',
'tiger128,4',
'tiger160,4',
'tiger192,4',
'snefru',
'snefru256',
'gost',
'gost-crypto',
'haval128,3',
'haval160,3',
'haval192,3',
'haval224,3',
'haval256,3',
'haval128,4',
'haval160,4',
'haval192,4',
'haval224,4',
'haval256,4',
'haval128,5',
'haval160,5',
'haval192,5',
'haval224,5',
'haval256,5',
];

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['hash_hmac', 'hash_hmac_file'], true);
Expand All @@ -34,9 +81,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return TypeUtils::toBenevolentUnion($defaultReturnType);
}
$string = $values[0];
$availableAlgorithms = function_exists('hash_hmac_algos') ? hash_hmac_algos() : hash_algos();

return in_array($string->getValue(), $availableAlgorithms, true) ? $defaultReturnType : new ConstantBooleanType(false);
return in_array($string->getValue(), self::HMAC_ALGORITHMS, true) ? $defaultReturnType : new ConstantBooleanType(false);
}

}
5 changes: 2 additions & 3 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5914,8 +5914,7 @@ public function dataFunctions(): array
'$hashHmacSha256',
],
[
// can't be tested correctly in PHP < 7.2
(function_exists('hash_hmac_algos') ? 'false' : 'string'),
'false',
'$hashHmacNonCryptographic',
],
[
Expand All @@ -5935,7 +5934,7 @@ public function dataFunctions(): array
'$hashHmacFileSha256',
],
[
(function_exists('hash_hmac_algos') ? 'false' : 'string'),
'false',
'$hashHmacFileNonCryptographic',
],
[
Expand Down