Skip to content

Commit 489a040

Browse files
committed
consistently use phpstan's testing framework to test phpstan bits
1 parent a38d64f commit 489a040

6 files changed

+35
-35
lines changed

src/Type/Php/PregMatchTypeSpecifyingExtension.php

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php declare(strict_types = 1);
22

3+
/*
4+
Blatantly copy-pasted from PHPStan's source code but with isFunctionSupported changed
5+
*/
36
namespace TheCodingMachine\Safe\PHPStan\Type\Php;
47

58
use PHPStan\Type\Php\RegexArrayShapeMatcher;

tests/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtensionTest.php

-22
This file was deleted.

tests/Type/Php/PregMatchParameterOutTypeExtensionTest.php renamed to tests/Type/Php/TypeAssertionsTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
use PHPStan\Testing\TypeInferenceTestCase;
66

7-
class PregMatchParameterOutTypeExtensionTest extends TypeInferenceTestCase
7+
class TypeAssertionsTest extends TypeInferenceTestCase
88
{
99
/**
1010
* @return iterable<mixed>
1111
*/
1212
public static function dataFileAsserts(): iterable
1313
{
14-
yield from self::gatherAssertTypes(__DIR__ . '/data/preg.php');
14+
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_unchecked.php');
15+
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_match_checked.php');
16+
yield from self::gatherAssertTypes(__DIR__ . '/data/preg_replace_return.php');
1517
}
1618

1719
/**

tests/Type/Php/data/preg.php renamed to tests/Type/Php/data/preg_match_checked.php

-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66
$pattern = '/H(.)ll(o) (World)?/';
77
$string = 'Hello World';
88

9-
// when return value isn't checked, we may-or-may-not have matches
10-
$type = "array{0?: string, 1?: non-empty-string, 2?: 'o', 3?: 'World'}";
11-
12-
// @phpstan-ignore-next-line - use of unsafe is intentional
13-
\preg_match($pattern, $string, $matches);
14-
\PHPStan\Testing\assertType($type, $matches);
15-
16-
\Safe\preg_match($pattern, $string, $matches);
17-
\PHPStan\Testing\assertType($type, $matches);
18-
19-
209
// when the return value is checked, we should have matches,
2110
// unless the match-group itself is optional
2211
$type = "array{0: string, 1: non-empty-string, 2: 'o', 3?: 'World'}";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 return value isn't checked, we may-or-may-not have matches
10+
$type = "array{0?: string, 1?: non-empty-string, 2?: 'o', 3?: 'World'}";
11+
12+
// @phpstan-ignore-next-line - use of unsafe is intentional
13+
\preg_match($pattern, $string, $matches);
14+
\PHPStan\Testing\assertType($type, $matches);
15+
16+
\Safe\preg_match($pattern, $string, $matches);
17+
\PHPStan\Testing\assertType($type, $matches);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace TheCodingMachine\Safe\PHPStan\Type\Php\data;
4+
5+
// preg_replace with a string pattern should return a string
6+
$x = \Safe\preg_replace('/foo/', 'bar', 'baz');
7+
\PHPStan\Testing\assertType("string", $x);
8+
9+
// preg_replace with an array pattern should return an array
10+
$x = \Safe\preg_replace(['/foo/'], ['bar'], ['baz']);
11+
\PHPStan\Testing\assertType("array", $x);

0 commit comments

Comments
 (0)