Skip to content

consistently use phpstan's testing framework to test phpstan bits #48

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

Merged
merged 1 commit into from
Feb 10, 2025
Merged
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
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);