diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index faba735c4e..4115a752e0 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -1901,6 +1901,16 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif ) { return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr); } + + if ( + $context->true() + && $exprNode instanceof FuncCall + && $exprNode->name instanceof Name + && $exprNode->name->toLowerString() === 'preg_match' + && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes() + ) { + return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr); + } } $leftType = $scope->getType($expr->left); diff --git a/tests/PHPStan/Analyser/nsrt/preg_match_shapes_php74.php b/tests/PHPStan/Analyser/nsrt/preg_match_shapes_php74.php index c5f9e9ded6..46c8373989 100644 --- a/tests/PHPStan/Analyser/nsrt/preg_match_shapes_php74.php +++ b/tests/PHPStan/Analyser/nsrt/preg_match_shapes_php74.php @@ -195,3 +195,27 @@ function testPregMatchIdenticalToOneFalseyContextInverted(string $value): void { assertType('array{string, string}', $matches); } } + +function testPregMatchEqualToOne(string $value): void { + if (preg_match('/%env\((.*)\:.*\)%/U', $value, $matches) == 1) { + assertType('array{string, string}', $matches); + } +} + +function testPregMatchEqualToOneFalseyContext(string $value): void { + if (!(preg_match('/%env\((.*)\:.*\)%/U', $value, $matches) != 1)) { + assertType('array{string, string}', $matches); + } +} + +function testPregMatchEqualToOneInverted(string $value): void { + if (1 == preg_match('/%env\((.*)\:.*\)%/U', $value, $matches)) { + assertType('array{string, string}', $matches); + } +} + +function testPregMatchEqualToOneFalseyContextInverted(string $value): void { + if (!(1 != preg_match('/%env\((.*)\:.*\)%/U', $value, $matches))) { + assertType('array{string, string}', $matches); + } +}