forked from thecodingmachine/phpstan-safe-rule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPregMatchTypeSpecifyingExtension.php
More file actions
93 lines (78 loc) · 2.95 KB
/
PregMatchTypeSpecifyingExtension.php
File metadata and controls
93 lines (78 loc) · 2.95 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
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php declare(strict_types = 1);
/*
Blatantly copy-pasted from PHPStan's source code but with isFunctionSupported changed
*/
namespace TheCodingMachine\Safe\PHPStan\Type\Php;
use PHPStan\Type\Php\RegexArrayShapeMatcher;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use function in_array;
use function strtolower;
final class PregMatchTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private TypeSpecifier $typeSpecifier;
public function __construct(
private RegexArrayShapeMatcher $regexShapeMatcher,
) {
}
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
{
return in_array(strtolower($functionReflection->getName()), ['safe\preg_match', 'safe\preg_match_all'], true) && !$context->null();
}
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$args = $node->getArgs();
$patternArg = $args[0] ?? null;
$matchesArg = $args[2] ?? null;
$flagsArg = $args[3] ?? null;
if ($patternArg === null || $matchesArg === null
) {
return new SpecifiedTypes();
}
$flagsType = null;
if ($flagsArg !== null) {
$flagsType = $scope->getType($flagsArg->value);
}
if ($context->true() && $context->falsey()) {
$wasMatched = TrinaryLogic::createMaybe();
} elseif ($context->true()) {
$wasMatched = TrinaryLogic::createYes();
} else {
$wasMatched = TrinaryLogic::createNo();
}
if ($functionReflection->getName() === 'Safe\preg_match') {
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, $wasMatched, $scope);
} else {
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, $wasMatched, $scope);
}
if ($matchedType === null) {
return new SpecifiedTypes();
}
$overwrite = false;
if ($context->false()) {
$overwrite = true;
$context = $context->negate();
}
$types = $this->typeSpecifier->create(
$matchesArg->value,
$matchedType,
$context,
$scope,
)->setRootExpr($node);
if ($overwrite) {
$types = $types->setAlwaysOverwriteTypes();
}
return $types;
}
}