-
Notifications
You must be signed in to change notification settings - Fork 426
/
HashHmacFunctionsReturnTypeExtension.php
97 lines (86 loc) · 2.24 KB
/
HashHmacFunctionsReturnTypeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
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);
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if ($functionReflection->getName() === 'hash_hmac') {
$defaultReturnType = new StringType();
} else {
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}
if (!isset($functionCall->getArgs()[0])) {
return $defaultReturnType;
}
$argType = $scope->getType($functionCall->getArgs()[0]->value);
if ($argType instanceof MixedType) {
return TypeUtils::toBenevolentUnion($defaultReturnType);
}
$values = TypeUtils::getConstantStrings($argType);
if (count($values) !== 1) {
return TypeUtils::toBenevolentUnion($defaultReturnType);
}
$string = $values[0];
return in_array($string->getValue(), self::HMAC_ALGORITHMS, true) ? $defaultReturnType : new ConstantBooleanType(false);
}
}