Skip to content

Add a failing unit test to reproduce #52 #55

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions tests/Type/Php/TypeAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_unchecked.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_checked.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_asserted.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_replace_return.php');
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Type/Php/data/preg_match_asserted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace TheCodingMachine\Safe\PHPStan\Type\Php\data;

// Checking that preg_match and Safe\preg_match are equivalent
$pattern = '/H(.)ll(o) (World)?/';
$string = 'Hello World';

// when the return value is checked, we should have matches,
// unless the match-group itself is optional
$type = "array{0: string, 1: non-empty-string, 2: 'o', 3?: 'World'}";

// @phpstan-ignore-next-line - use of unsafe is intentional
$ret = \preg_match($pattern, $string, $matches);
assert($ret === 1);
\PHPStan\Testing\assertType($type, $matches);

$ret = \Safe\preg_match($pattern, $string, $matches);
assert($ret === 1);
\PHPStan\Testing\assertType($type, $matches);

Check failure on line 20 in tests/Type/Php/data/preg_match_asserted.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Expected type array{0: string, 1: non-empty-string, 2: 'o', 3?: 'World'}, actual: array{0?: string, 1?: non-empty-string, 2?: 'o', 3?: 'World'}

Check failure on line 20 in tests/Type/Php/data/preg_match_asserted.php

View workflow job for this annotation

GitHub Actions / PHP 8.4

Expected type array{0: string, 1: non-empty-string, 2: 'o', 3?: 'World'}, actual: array{0?: string, 1?: non-empty-string, 2?: 'o', 3?: 'World'}
Loading