Skip to content

Commit

Permalink
consistently use phpstan's testing framework to test phpstan bits
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Feb 10, 2025
1 parent a38d64f commit 489a040
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
3 changes: 3 additions & 0 deletions src/Type/Php/PregMatchTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php declare(strict_types = 1);

/*
Blatantly copy-pasted from PHPStan's source code but with isFunctionSupported changed
*/
namespace TheCodingMachine\Safe\PHPStan\Type\Php;

use PHPStan\Type\Php\RegexArrayShapeMatcher;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

use PHPStan\Testing\TypeInferenceTestCase;

class PregMatchParameterOutTypeExtensionTest extends TypeInferenceTestCase
class TypeAssertionsTest extends TypeInferenceTestCase
{
/**
* @return iterable<mixed>
*/
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/preg.php');
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_replace_return.php');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
$pattern = '/H(.)ll(o) (World)?/';
$string = 'Hello World';

// when return value isn't checked, we may-or-may-not have matches
$type = "array{0?: string, 1?: non-empty-string, 2?: 'o', 3?: 'World'}";

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

\Safe\preg_match($pattern, $string, $matches);
\PHPStan\Testing\assertType($type, $matches);


// 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'}";
Expand Down
17 changes: 17 additions & 0 deletions tests/Type/Php/data/preg_match_unchecked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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 return value isn't checked, we may-or-may-not have matches
$type = "array{0?: string, 1?: non-empty-string, 2?: 'o', 3?: 'World'}";

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

\Safe\preg_match($pattern, $string, $matches);
\PHPStan\Testing\assertType($type, $matches);
11 changes: 11 additions & 0 deletions tests/Type/Php/data/preg_replace_return.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

// preg_replace with a string pattern should return a string
$x = \Safe\preg_replace('/foo/', 'bar', 'baz');
\PHPStan\Testing\assertType("string", $x);

// preg_replace with an array pattern should return an array
$x = \Safe\preg_replace(['/foo/'], ['bar'], ['baz']);
\PHPStan\Testing\assertType("array", $x);

0 comments on commit 489a040

Please sign in to comment.