Skip to content
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
7 changes: 6 additions & 1 deletion src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,13 @@ public function assertFileAsserts(
}

/**
* @return array<string, (
Copy link
Contributor Author

@staabm staabm Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goal of this PR is, to have a more precise return type here, so downstream projects which use TypeInferenceTestCase can properly utilize the PHPUnit Data-Provider validation capabilities on level 8

see e.g. phpstan/phpstan-webmozart-assert#193

* array{0: 'type', 1: string, 2: int|float|string|bool|null, 3: string, 4: int, 5?: non-empty-list<non-falsy-string>}|
* array{0: 'superType', 1: string, 2: string, 3: string, 4: bool, 5: int, 6?: non-empty-list<non-falsy-string>}|
* array{0: 'variableCertainty', 1: string, 2: TrinaryLogic, 3: TrinaryLogic, 4: string, 5: int, 6?: non-empty-list<non-falsy-string>}
* )>
*
* @api
* @return array<string, mixed[]>
*/
public static function gatherAssertTypes(string $file): array
{
Expand Down
3 changes: 0 additions & 3 deletions tests/PHPStan/Analyser/LooseConstComparisonPhp7Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
class LooseConstComparisonPhp7Test extends TypeInferenceTestCase
{

/**
* @return iterable<array<string, mixed[]>>
*/
public static function dataFileAsserts(): iterable
{
// compares constants according to the php-version phpstan configuration,
Expand Down
3 changes: 0 additions & 3 deletions tests/PHPStan/Analyser/LooseConstComparisonPhp8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
class LooseConstComparisonPhp8Test extends TypeInferenceTestCase
{

/**
* @return iterable<array<string, mixed[]>>
*/
public static function dataFileAsserts(): iterable
{
// compares constants according to the php-version phpstan configuration,
Expand Down
24 changes: 12 additions & 12 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPUnit\Framework\Attributes\DataProvider;
use stdClass;
use function array_merge;
use function array_shift;
use function define;
use function dirname;
use function implode;
Expand Down Expand Up @@ -270,23 +269,24 @@ public function testFile(string $file): void
$failures = [];

foreach ($asserts as $args) {
$assertType = array_shift($args);
$file = array_shift($args);
if ($args[0] === 'type') {
$file = $args[1];

if ($assertType === 'type') {
Copy link
Contributor Author

@staabm staabm Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan is not able to narrow the array-shape of the tagged union of $args based on array_shift() (which is equivalent to $args[0]).
rewrote the logic so PHPStan is able to narrow the tagged union

$expected = $args[0];
$actual = $args[1];
$expected = $args[2];
$actual = $args[3];

if ($expected !== $actual) {
$failures[] = sprintf("Line %d:\nExpected: %s\nActual: %s\n", $args[2], $expected, $actual);
$failures[] = sprintf("Line %d:\nExpected: %s\nActual: %s\n", $args[4], $expected, $actual);
}
} elseif ($assertType === 'variableCertainty') {
$expectedCertainty = $args[0];
$actualCertainty = $args[1];
$variableName = $args[2];
} elseif ($args[0] === 'variableCertainty') {
$file = $args[1];

$expectedCertainty = $args[2];
$actualCertainty = $args[3];
$variableName = $args[4];

if ($expectedCertainty->equals($actualCertainty) !== true) {
$failures[] = sprintf("Certainty of %s on line %d:\nExpected: %s\nActual: %s\n", $variableName, $args[3], $expectedCertainty->describe(), $actualCertainty->describe());
$failures[] = sprintf("Certainty of %s on line %d:\nExpected: %s\nActual: %s\n", $variableName, $args[5], $expectedCertainty->describe(), $actualCertainty->describe());
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions tests/PHPStan/Analyser/SubstrPhp7Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
class SubstrPhp7Test extends TypeInferenceTestCase
{

/**
* @return iterable<array<string, mixed[]>>
*/
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13129-php7.php');
Expand Down
3 changes: 0 additions & 3 deletions tests/PHPStan/Analyser/SubstrPhp8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
class SubstrPhp8Test extends TypeInferenceTestCase
{

/**
* @return iterable<array<string, mixed[]>>
*/
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13129-php8.php');
Expand Down
Loading