Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cc1fd4b

Browse files
committedFeb 18, 2025·
Add a failing unit test to reproduce #52
1 parent 33dcbc3 commit cc1fd4b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

‎tests/Type/Php/TypeAssertionsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static function dataFileAsserts(): iterable
1313
{
1414
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_unchecked.php');
1515
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_checked.php');
16+
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_asserted.php');
1617
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_replace_return.php');
1718
}
1819

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace TheCodingMachine\Safe\PHPStan\Type\Php\data;
4+
5+
// Checking that preg_match and Safe\preg_match are equivalent
6+
$pattern = '/H(.)ll(o) (World)?/';
7+
$string = 'Hello World';
8+
9+
// when the return value is checked, we should have matches,
10+
// unless the match-group itself is optional
11+
$type = "array{0: string, 1: non-empty-string, 2: 'o', 3?: 'World'}";
12+
13+
// @phpstan-ignore-next-line - use of unsafe is intentional
14+
$ret = \preg_match($pattern, $string, $matches);
15+
assert($ret === 1);
16+
\PHPStan\Testing\assertType($type, $matches);
17+
18+
$ret = \Safe\preg_match($pattern, $string, $matches);
19+
assert($ret === 1);
20+
\PHPStan\Testing\assertType($type, $matches);

0 commit comments

Comments
 (0)
Please sign in to comment.