Skip to content

Commit d842380

Browse files
staabmondrejmirtes
authored andcommitted
TypeSpecifier - understand preg_match() == 1 same way as === 1
1 parent 721a0a6 commit d842380

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Analyser/TypeSpecifier.php

+10
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,16 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
19011901
) {
19021902
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
19031903
}
1904+
1905+
if (
1906+
$context->true()
1907+
&& $exprNode instanceof FuncCall
1908+
&& $exprNode->name instanceof Name
1909+
&& $exprNode->name->toLowerString() === 'preg_match'
1910+
&& (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes()
1911+
) {
1912+
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
1913+
}
19041914
}
19051915

19061916
$leftType = $scope->getType($expr->left);

tests/PHPStan/Analyser/nsrt/preg_match_shapes_php74.php

+24
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,27 @@ function testPregMatchIdenticalToOneFalseyContextInverted(string $value): void {
195195
assertType('array{string, string}', $matches);
196196
}
197197
}
198+
199+
function testPregMatchEqualToOne(string $value): void {
200+
if (preg_match('/%env\((.*)\:.*\)%/U', $value, $matches) == 1) {
201+
assertType('array{string, string}', $matches);
202+
}
203+
}
204+
205+
function testPregMatchEqualToOneFalseyContext(string $value): void {
206+
if (!(preg_match('/%env\((.*)\:.*\)%/U', $value, $matches) != 1)) {
207+
assertType('array{string, string}', $matches);
208+
}
209+
}
210+
211+
function testPregMatchEqualToOneInverted(string $value): void {
212+
if (1 == preg_match('/%env\((.*)\:.*\)%/U', $value, $matches)) {
213+
assertType('array{string, string}', $matches);
214+
}
215+
}
216+
217+
function testPregMatchEqualToOneFalseyContextInverted(string $value): void {
218+
if (!(1 != preg_match('/%env\((.*)\:.*\)%/U', $value, $matches))) {
219+
assertType('array{string, string}', $matches);
220+
}
221+
}

0 commit comments

Comments
 (0)