1441 |
ariadna |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
|
|
|
3 |
namespace Composer\Pcre\PHPStan;
|
|
|
4 |
|
|
|
5 |
use Composer\Pcre\Preg;
|
|
|
6 |
use PhpParser\Node\Expr\StaticCall;
|
|
|
7 |
use PHPStan\Analyser\Scope;
|
|
|
8 |
use PHPStan\Reflection\MethodReflection;
|
|
|
9 |
use PHPStan\Reflection\ParameterReflection;
|
|
|
10 |
use PHPStan\TrinaryLogic;
|
|
|
11 |
use PHPStan\Type\Php\RegexArrayShapeMatcher;
|
|
|
12 |
use PHPStan\Type\StaticMethodParameterOutTypeExtension;
|
|
|
13 |
use PHPStan\Type\Type;
|
|
|
14 |
|
|
|
15 |
final class PregMatchParameterOutTypeExtension implements StaticMethodParameterOutTypeExtension
|
|
|
16 |
{
|
|
|
17 |
/**
|
|
|
18 |
* @var RegexArrayShapeMatcher
|
|
|
19 |
*/
|
|
|
20 |
private $regexShapeMatcher;
|
|
|
21 |
|
|
|
22 |
public function __construct(
|
|
|
23 |
RegexArrayShapeMatcher $regexShapeMatcher
|
|
|
24 |
)
|
|
|
25 |
{
|
|
|
26 |
$this->regexShapeMatcher = $regexShapeMatcher;
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool
|
|
|
30 |
{
|
|
|
31 |
return
|
|
|
32 |
$methodReflection->getDeclaringClass()->getName() === Preg::class
|
|
|
33 |
&& in_array($methodReflection->getName(), [
|
|
|
34 |
'match', 'isMatch', 'matchStrictGroups', 'isMatchStrictGroups',
|
|
|
35 |
'matchAll', 'isMatchAll', 'matchAllStrictGroups', 'isMatchAllStrictGroups'
|
|
|
36 |
], true)
|
|
|
37 |
&& $parameter->getName() === 'matches';
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function getParameterOutTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type
|
|
|
41 |
{
|
|
|
42 |
$args = $methodCall->getArgs();
|
|
|
43 |
$patternArg = $args[0] ?? null;
|
|
|
44 |
$matchesArg = $args[2] ?? null;
|
|
|
45 |
$flagsArg = $args[3] ?? null;
|
|
|
46 |
|
|
|
47 |
if (
|
|
|
48 |
$patternArg === null || $matchesArg === null
|
|
|
49 |
) {
|
|
|
50 |
return null;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$flagsType = PregMatchFlags::getType($flagsArg, $scope);
|
|
|
54 |
if ($flagsType === null) {
|
|
|
55 |
return null;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
if (stripos($methodReflection->getName(), 'matchAll') !== false) {
|
|
|
59 |
return $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
return $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
}
|