Skip to content

Commit 4f03332

Browse files
phpstan-botclaude
andcommitted
Move container re-registration into PHPStanTestCase behind an opt-in flag
Replace the per-class setUp() duplication with a $reinitializeContainerBeforeEachTest flag (default false) on PHPStanTestCase. Tests with PHP-version-dependent reflection assertions opt in by setting it to true, so the runtime container is pinned before each test without each class re-implementing setUp(). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 38dd381 commit 4f03332

5 files changed

Lines changed: 35 additions & 36 deletions

File tree

src/Testing/PHPStanTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Testing;
44

5+
use Override;
56
use PHPStan\Analyser\ConstantResolver;
67
use PHPStan\Analyser\DirectInternalScopeFactoryFactory;
78
use PHPStan\Analyser\Error;
@@ -40,6 +41,24 @@ abstract class PHPStanTestCase extends TestCase
4041

4142
use PHPStanTestCaseTrait;
4243

44+
/**
45+
* Re-register the runtime container as the global static reflection provider before
46+
* every test. Enable this in tests that construct Type objects directly and assert
47+
* PHP-version-dependent reflection results, so a foreign PhpVersion leaked by another
48+
* test can't flake them. See https://github.com/phpstan/phpstan/issues/14860
49+
*/
50+
protected bool $reinitializeContainerBeforeEachTest = false;
51+
52+
#[Override]
53+
protected function setUp(): void
54+
{
55+
if (!$this->reinitializeContainerBeforeEachTest) {
56+
return;
57+
}
58+
59+
self::getContainer();
60+
}
61+
4362
public static function getParser(): Parser
4463
{
4564
/** @var Parser $parser */

tests/PHPStan/Type/Accessory/HasPropertyTypeTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use DateInterval;
7-
use Override;
87
use PHPStan\Testing\PHPStanTestCase;
98
use PHPStan\TrinaryLogic;
109
use PHPStan\Type\CallableType;
@@ -24,14 +23,10 @@
2423
class HasPropertyTypeTest extends PHPStanTestCase
2524
{
2625

27-
#[Override]
28-
protected function setUp(): void
29-
{
30-
// Pin the runtime container so a foreign PhpVersion leaked by another test
31-
// can't flake the version-dependent Closure data set below.
32-
// See https://github.com/phpstan/phpstan/issues/14860
33-
self::getContainer();
34-
}
26+
// Pin the runtime container so a foreign PhpVersion leaked by another test
27+
// can't flake the version-dependent Closure data set below.
28+
// See https://github.com/phpstan/phpstan/issues/14860
29+
protected bool $reinitializeContainerBeforeEachTest = true;
3530

3631
public static function dataIsSuperTypeOf(): array
3732
{

tests/PHPStan/Type/Generic/GenericObjectTypeTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use DateTimeInterface;
77
use Exception;
88
use Iterator;
9-
use Override;
109
use PHPStan\Testing\PHPStanTestCase;
1110
use PHPStan\TrinaryLogic;
1211
use PHPStan\Type\IntegerType;
@@ -33,14 +32,10 @@
3332
class GenericObjectTypeTest extends PHPStanTestCase
3433
{
3534

36-
#[Override]
37-
protected function setUp(): void
38-
{
39-
// Pin the runtime container so a foreign PhpVersion leaked by another test
40-
// can't flake the version-dependent data sets (reflected variance of built-in
41-
// generics). See https://github.com/phpstan/phpstan/issues/14860
42-
self::getContainer();
43-
}
35+
// Pin the runtime container so a foreign PhpVersion leaked by another test
36+
// can't flake the version-dependent data sets (reflected variance of built-in
37+
// generics). See https://github.com/phpstan/phpstan/issues/14860
38+
protected bool $reinitializeContainerBeforeEachTest = true;
4439

4540
public static function dataIsSuperTypeOf(): array
4641
{

tests/PHPStan/Type/ObjectTypeTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Iterator;
2424
use LogicException;
2525
use ObjectTypeEnums\FooEnum;
26-
use Override;
2726
use PHPStan\Testing\PHPStanTestCase;
2827
use PHPStan\TrinaryLogic;
2928
use PHPStan\Type\Accessory\HasMethodType;
@@ -52,14 +51,10 @@
5251
class ObjectTypeTest extends PHPStanTestCase
5352
{
5453

55-
#[Override]
56-
protected function setUp(): void
57-
{
58-
// Pin the runtime container so a foreign PhpVersion leaked by another test
59-
// can't flake the version-dependent Closure data sets (dynamic-property
60-
// handling). See https://github.com/phpstan/phpstan/issues/14860
61-
self::getContainer();
62-
}
54+
// Pin the runtime container so a foreign PhpVersion leaked by another test
55+
// can't flake the version-dependent Closure data sets (dynamic-property
56+
// handling). See https://github.com/phpstan/phpstan/issues/14860
57+
protected bool $reinitializeContainerBeforeEachTest = true;
6358

6459
public static function dataIsIterable(): array
6560
{

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use InvalidArgumentException;
1515
use Iterator;
1616
use ObjectShapesAcceptance\ClassWithFooIntProperty;
17-
use Override;
1817
use PHPStan\DependencyInjection\BleedingEdgeToggle;
1918
use PHPStan\Fixture\FinalClass;
2019
use PHPStan\Generics\FunctionsAssertType\C;
@@ -74,14 +73,10 @@
7473
class TypeCombinatorTest extends PHPStanTestCase
7574
{
7675

77-
#[Override]
78-
protected function setUp(): void
79-
{
80-
// Pin the runtime container so a foreign PhpVersion leaked by another test
81-
// can't flake the version-dependent data sets (dynamic-property handling of
82-
// final classes). See https://github.com/phpstan/phpstan/issues/14860
83-
self::getContainer();
84-
}
76+
// Pin the runtime container so a foreign PhpVersion leaked by another test
77+
// can't flake the version-dependent data sets (dynamic-property handling of
78+
// final classes). See https://github.com/phpstan/phpstan/issues/14860
79+
protected bool $reinitializeContainerBeforeEachTest = true;
8580

8681
public static function dataAddNull(): array
8782
{

0 commit comments

Comments
 (0)