Skip to content
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
20 changes: 18 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ public function specifyTypesInCondition(
$expr->left instanceof FuncCall
&& count($expr->left->getArgs()) >= 1
&& $expr->left->name instanceof Name
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen'], true)
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true)
&& (
!$expr->right instanceof FuncCall
|| !$expr->right->name instanceof Name
|| !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen', 'mb_strlen'], true)
|| !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true)
)
) {
$inverseOperator = $expr instanceof Node\Expr\BinaryOp\Smaller
Expand Down Expand Up @@ -336,6 +336,22 @@ public function specifyTypesInCondition(
}
}

if (
!$context->null()
&& $expr->right instanceof FuncCall
&& 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()
) {
return $this->specifyTypesInCondition(
$scope,
new Expr\BinaryOp\NotIdentical($expr->right, new ConstFetch(new Name('false'))),
$context,
$rootExpr,
);
}

if (
!$context->null()
&& $expr->right instanceof FuncCall
Expand Down
62 changes: 62 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11293.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php // lint >= 7.4

namespace Bug11293;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function sayHello(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) > 0) {
assertType('array{string, non-falsy-string&numeric-string}', $matches);
}
}

public function sayHello2(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) === 1) {
assertType('array{string, non-falsy-string&numeric-string}', $matches);
}
}

public function sayHello3(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) >= 1) {
assertType('array{string, non-falsy-string&numeric-string}', $matches);
}
}

public function sayHello4(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) <= 0) {
assertType('array{}', $matches);

return;
}

assertType('array{string, non-falsy-string&numeric-string}', $matches);
}

public function sayHello5(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) < 1) {
assertType('array{}', $matches);

return;
}

assertType('array{string, non-falsy-string&numeric-string}', $matches);
}

public function sayHello6(string $s): void
{
if (1 > preg_match('/data-(\d{6})\.json$/', $s, $matches)) {
assertType('array{}', $matches);

return;
}

assertType('array{string, non-falsy-string&numeric-string}', $matches);
}
}
Loading