Skip to content

Commit d2da3a8

Browse files
committed
Use single regex shape matcher call with dynamically determined method
Personally I find it better than if/else with similar calls to different methods with exact same parameters.
1 parent 2f71857 commit d2da3a8

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

dev-tools/phpstan/src/Extension/PregMatchParameterOutExtension.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function isStaticMethodSupported(MethodReflection $methodReflection, Para
3838
{
3939
return
4040
Preg::class === $methodReflection->getDeclaringClass()->getName()
41-
&& in_array($methodReflection->getName(), ['match', 'matchAll'], true)
41+
&& \in_array($methodReflection->getName(), ['match', 'matchAll'], true)
4242
&& 'matches' === $parameter->getName();
4343
}
4444

@@ -60,9 +60,9 @@ public function getParameterOutTypeFromStaticMethodCall(MethodReflection $method
6060
$flagsType = $scope->getType($flagsArg->value);
6161
}
6262

63-
if ($methodReflection->getName() === 'match') {
64-
return $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
65-
}
66-
return $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
63+
$matcherMethod = ('match' === $methodReflection->getName()) ? 'matchExpr' : 'matchAllExpr';
64+
65+
// @phpstan-ignore method.dynamicName
66+
return $this->regexShapeMatcher->{$matcherMethod}($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
6767
}
6868
}

dev-tools/phpstan/src/Extension/PregMatchTypeSpecifyingExtension.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
5050

5151
public function isStaticMethodSupported(MethodReflection $methodReflection, StaticCall $node, TypeSpecifierContext $context): bool
5252
{
53-
return in_array($methodReflection->getName(), ['match', 'matchAll'], true) && !$context->null();
53+
return \in_array($methodReflection->getName(), ['match', 'matchAll'], true) && !$context->null();
5454
}
5555

5656
public function specifyTypes(MethodReflection $methodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
@@ -71,11 +71,15 @@ public function specifyTypes(MethodReflection $methodReflection, StaticCall $nod
7171
$flagsType = $scope->getType($flagsArg->value);
7272
}
7373

74-
if ($methodReflection->getName() === 'match') {
75-
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
76-
} else {
77-
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
78-
}
74+
$matcherMethod = ('match' === $methodReflection->getName()) ? 'matchExpr' : 'matchAllExpr';
75+
76+
/** @phpstan-ignore method.dynamicName */
77+
$matchedType = $this->regexShapeMatcher->{$matcherMethod}(
78+
$patternArg->value,
79+
$flagsType,
80+
TrinaryLogic::createFromBoolean($context->true()),
81+
$scope
82+
);
7983

8084
if (null === $matchedType) {
8185
return new SpecifiedTypes();

0 commit comments

Comments
 (0)