-
Notifications
You must be signed in to change notification settings - Fork 426
/
HashFunctionsReturnTypeExtension.php
144 lines (123 loc) · 3.79 KB
/
HashFunctionsReturnTypeExtension.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?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\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function array_map;
use function hash_algos;
use function in_array;
use function strtolower;
final class HashFunctionsReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
private const SUPPORTED_FUNCTIONS = [
'hash' => [
'cryptographic' => false,
'possiblyFalse' => false,
],
'hash_file' => [
'cryptographic' => false,
'possiblyFalse' => true,
],
'hash_hkdf' => [
'cryptographic' => true,
'possiblyFalse' => false,
],
'hash_hmac' => [
'cryptographic' => true,
'possiblyFalse' => false,
],
'hash_hmac_file' => [
'cryptographic' => true,
'possiblyFalse' => true,
],
'hash_pbkdf2' => [
'cryptographic' => true,
'possiblyFalse' => false,
],
];
private const NON_CRYPTOGRAPHIC_ALGORITHMS = [
'adler32',
'crc32',
'crc32b',
'crc32c',
'fnv132',
'fnv1a32',
'fnv164',
'fnv1a64',
'joaat',
'murmur3a',
'murmur3c',
'murmur3f',
'xxh32',
'xxh64',
'xxh3',
'xxh128',
];
private PhpVersion $phpVersion;
/** @var array<int, non-empty-string> */
private array $hashAlgorithms;
public function __construct(PhpVersion $phpVersion)
{
$this->phpVersion = $phpVersion;
$this->hashAlgorithms = hash_algos();
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
$name = strtolower($functionReflection->getName());
return isset(self::SUPPORTED_FUNCTIONS[$name]);
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
if (!isset($functionCall->getArgs()[0])) {
return $defaultReturnType;
}
$algorithmType = $scope->getType($functionCall->getArgs()[0]->value);
if ($algorithmType instanceof MixedType) {
return TypeUtils::toBenevolentUnion($defaultReturnType);
}
$constantAlgorithmTypes = TypeUtils::getConstantStrings($algorithmType);
if ($constantAlgorithmTypes === []) {
return TypeUtils::toBenevolentUnion($defaultReturnType);
}
$neverType = new NeverType();
$falseType = new ConstantBooleanType(false);
$nonEmptyString = new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
$invalidAlgorithmType = $this->phpVersion->throwsValueErrorForInternalFunctions() ? $neverType : $falseType;
$functionData = self::SUPPORTED_FUNCTIONS[strtolower($functionReflection->getName())];
$returnTypes = array_map(
function (ConstantStringType $type) use ($functionData, $nonEmptyString, $invalidAlgorithmType) {
$algorithm = strtolower($type->getValue());
if (!in_array($algorithm, $this->hashAlgorithms, true)) {
return $invalidAlgorithmType;
}
if ($functionData['cryptographic'] && in_array($algorithm, self::NON_CRYPTOGRAPHIC_ALGORITHMS, true)) {
return $invalidAlgorithmType;
}
return $nonEmptyString;
},
$constantAlgorithmTypes
);
$returnType = TypeCombinator::union(...$returnTypes);
if ($functionData['possiblyFalse'] && !$neverType->isSuperTypeOf($returnType)->yes()) {
$returnType = TypeCombinator::union($returnType, $falseType);
}
return $returnType;
}
}