Skip to content

Fix wrong inference on preg_match != false #4223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,15 @@ public function specifyTypesInCondition(
&& count($expr->right->getArgs()) >= 3
&& $expr->right->name instanceof Name
&& in_array(strtolower((string) $expr->right->name), ['preg_match'], true)
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes()
&& (
IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($leftType)->yes()
|| ($expr instanceof Expr\BinaryOp\Smaller && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes())
)
) {
return $this->specifyTypesInCondition(
$scope,
new Expr\BinaryOp\NotIdentical($expr->right, new ConstFetch(new Name('false'))),
$context,
)->setRootExpr($expr);
// 0 < preg_match or 1 <= preg_match becomes 1 === preg_match
$newExpr = new Expr\BinaryOp\Identical($expr->right, new Node\Scalar\Int_(1));

return $this->specifyTypesInCondition($scope, $newExpr, $context)->setRootExpr($expr);
}

if (
Expand Down
12 changes: 10 additions & 2 deletions src/Type/Php/PregMatchTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$flagsType = $scope->getType($flagsArg->value);
}

if ($context->true() && $context->falsey()) {
$wasMatched = TrinaryLogic::createMaybe();
} elseif ($context->true()) {
$wasMatched = TrinaryLogic::createYes();
} else {
$wasMatched = TrinaryLogic::createNo();
}

if ($functionReflection->getName() === 'preg_match') {
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, $wasMatched, $scope);
} else {
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope);
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, $wasMatched, $scope);
}
if ($matchedType === null) {
return new SpecifiedTypes();
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/bug-11293.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function sayHello3(string $s): void
public function sayHello4(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) <= 0) {
assertType('array{}', $matches);
assertType('list{0?: string, 1?: non-falsy-string&numeric-string}', $matches);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be considered as a "regression" but:

BTW array{} is already technically not true since if preg_match return false, $matches is null.


return;
}
Expand All @@ -41,7 +41,7 @@ public function sayHello4(string $s): void
public function sayHello5(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) < 1) {
assertType('array{}', $matches);
assertType('list{0?: string, 1?: non-falsy-string&numeric-string}', $matches);

return;
}
Expand All @@ -52,7 +52,7 @@ public function sayHello5(string $s): void
public function sayHello6(string $s): void
{
if (1 > preg_match('/data-(\d{6})\.json$/', $s, $matches)) {
assertType('array{}', $matches);
assertType('list{0?: string, 1?: non-falsy-string&numeric-string}', $matches);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ public function testBug5365(): void
$this->analyse([__DIR__ . '/data/bug-5365.php'], []);
}

public function testBug11908(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->reportAlwaysTrueInLastCondition = true;
$this->analyse([__DIR__ . '/data/bug-11908.php'], []);
}

public function testBug8555(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11908.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Bug11908;

$matches = false;
if (preg_match('/a/', '', $matches) !== false && $matches) {
var_export($matches);
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,13 @@ public function testBug12771(): void
$this->analyse([__DIR__ . '/data/bug-12771.php'], []);
}

public function testBug11708(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-11708.php'], []);
}

public function testIssetAfterRememberedConstructor(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ public function testBug10577(): void
$this->analyse([__DIR__ . '/data/bug-10577.php'], []);
}

public function testBug11708(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-11708.php'], []);
}

public function testBug10610(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-11708.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug11708;

class HelloWorld
{
public function sayHello(): void
{
$xRequestStart = sprintf('t=%s', uniqid('fake_timestamp_'));

$matches = [];
if (false === preg_match('/^t=(\d+)$/', (string) $xRequestStart, $matches)) {
return;
}

$requestStart = $matches[1] ?? null;
$requestStart2 = isset($matches[1]);
}
}
Loading