Skip to content

Commit e819f58

Browse files
authored
Merge branch refs/heads/1.12.x into 2.0.x
2 parents 3dc64a2 + b1b7782 commit e819f58

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ public function specifyTypesInCondition(
235235
$expr->left instanceof FuncCall
236236
&& count($expr->left->getArgs()) >= 1
237237
&& $expr->left->name instanceof Name
238-
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen'], true)
238+
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true)
239239
&& (
240240
!$expr->right instanceof FuncCall
241241
|| !$expr->right->name instanceof Name
242-
|| !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen', 'mb_strlen'], true)
242+
|| !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true)
243243
)
244244
) {
245245
$inverseOperator = $expr instanceof Node\Expr\BinaryOp\Smaller
@@ -328,6 +328,22 @@ public function specifyTypesInCondition(
328328
}
329329
}
330330

331+
if (
332+
!$context->null()
333+
&& $expr->right instanceof FuncCall
334+
&& count($expr->right->getArgs()) >= 3
335+
&& $expr->right->name instanceof Name
336+
&& in_array(strtolower((string) $expr->right->name), ['preg_match'], true)
337+
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes()
338+
) {
339+
return $this->specifyTypesInCondition(
340+
$scope,
341+
new Expr\BinaryOp\NotIdentical($expr->right, new ConstFetch(new Name('false'))),
342+
$context,
343+
$rootExpr,
344+
);
345+
}
346+
331347
if (
332348
!$context->null()
333349
&& $expr->right instanceof FuncCall
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php // lint >= 7.4
2+
3+
namespace Bug11293;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
public function sayHello(string $s): void
10+
{
11+
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) > 0) {
12+
assertType('array{string, non-falsy-string&numeric-string}', $matches);
13+
}
14+
}
15+
16+
public function sayHello2(string $s): void
17+
{
18+
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) === 1) {
19+
assertType('array{string, non-falsy-string&numeric-string}', $matches);
20+
}
21+
}
22+
23+
public function sayHello3(string $s): void
24+
{
25+
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) >= 1) {
26+
assertType('array{string, non-falsy-string&numeric-string}', $matches);
27+
}
28+
}
29+
30+
public function sayHello4(string $s): void
31+
{
32+
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) <= 0) {
33+
assertType('array{}', $matches);
34+
35+
return;
36+
}
37+
38+
assertType('array{string, non-falsy-string&numeric-string}', $matches);
39+
}
40+
41+
public function sayHello5(string $s): void
42+
{
43+
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) < 1) {
44+
assertType('array{}', $matches);
45+
46+
return;
47+
}
48+
49+
assertType('array{string, non-falsy-string&numeric-string}', $matches);
50+
}
51+
52+
public function sayHello6(string $s): void
53+
{
54+
if (1 > preg_match('/data-(\d{6})\.json$/', $s, $matches)) {
55+
assertType('array{}', $matches);
56+
57+
return;
58+
}
59+
60+
assertType('array{string, non-falsy-string&numeric-string}', $matches);
61+
}
62+
}

0 commit comments

Comments
 (0)