forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenSslEncryptParameterOutTypeExtension.php
More file actions
80 lines (64 loc) · 2.06 KB
/
OpenSslEncryptParameterOutTypeExtension.php
File metadata and controls
80 lines (64 loc) · 2.06 KB
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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\FunctionParameterOutTypeExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use function in_array;
use function strtolower;
use function substr;
#[AutowiredService]
final class OpenSslEncryptParameterOutTypeExtension implements FunctionParameterOutTypeExtension
{
public function __construct(private OpenSslCipherMethodsProvider $cipherMethodsProvider)
{
}
public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool
{
return $functionReflection->getName() === 'openssl_encrypt' && $parameter->getName() === 'tag';
}
public function getParameterOutTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, ParameterReflection $parameter, Scope $scope): ?Type
{
$args = $funcCall->getArgs();
$cipherArg = $args[1] ?? null;
if ($cipherArg === null) {
return null;
}
$tagTypes = [];
foreach ($scope->getType($cipherArg->value)->getConstantStrings() as $cipherType) {
$cipher = strtolower($cipherType->getValue());
$mode = substr($cipher, -3);
if (!$this->cipherMethodsProvider->isSupportedCipherMethod($cipher)) {
$tagTypes[] = new NullType();
continue;
}
if (in_array($mode, ['gcm', 'ccm'], true)) {
$tagTypes[] = new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
continue;
}
$tagTypes[] = new NullType();
}
if ($tagTypes === []) {
return new UnionType([
new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]),
new NullType(),
]);
}
return TypeCombinator::union(...$tagTypes);
}
}