Skip to content

Commit a5aa2cd

Browse files
mvhirschshish
authored andcommitted
updates extensions to phpstan v2.1.41
1 parent 5c80488 commit a5aa2cd

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/Type/Php/PregMatchParameterOutTypeExtension.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use PHPStan\Reflection\FunctionReflection;
1414
use PHPStan\Reflection\ParameterReflection;
1515
use PHPStan\TrinaryLogic;
16+
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1617
use PHPStan\Type\FunctionParameterOutTypeExtension;
1718
use PHPStan\Type\Type;
19+
use PHPStan\Type\TypeCombinator;
1820
use function in_array;
1921

2022
final class PregMatchParameterOutTypeExtension implements FunctionParameterOutTypeExtension
@@ -50,7 +52,14 @@ public function getParameterOutTypeFromFunctionCall(FunctionReflection $function
5052
}
5153

5254
if ($functionReflection->getName() === 'Safe\preg_match') {
53-
return $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
55+
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createYes(), $scope);
56+
if ($matchedType === null) {
57+
return null;
58+
}
59+
return TypeCombinator::union(
60+
ConstantArrayTypeBuilder::createEmpty()->getArray(),
61+
$matchedType,
62+
);
5463
}
5564
return $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope);
5665
}

src/Type/Php/PregMatchTypeSpecifyingExtension.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,18 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
5555
$flagsType = $scope->getType($flagsArg->value);
5656
}
5757

58+
if ($context->true() && $context->falsey()) {
59+
$wasMatched = TrinaryLogic::createMaybe();
60+
} elseif ($context->true()) {
61+
$wasMatched = TrinaryLogic::createYes();
62+
} else {
63+
$wasMatched = TrinaryLogic::createNo();
64+
}
65+
5866
if ($functionReflection->getName() === 'Safe\preg_match') {
59-
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
67+
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, $wasMatched, $scope);
6068
} else {
61-
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
69+
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, $wasMatched, $scope);
6270
}
6371
if ($matchedType === null) {
6472
return new SpecifiedTypes();

0 commit comments

Comments
 (0)