diff --git a/build/target-repository/bootstrap.php b/build/target-repository/bootstrap.php index 3dbb6e139eb..d6dd77ec353 100644 --- a/build/target-repository/bootstrap.php +++ b/build/target-repository/bootstrap.php @@ -2,31 +2,6 @@ declare(strict_types=1); -use PHPParser\Node; -use PHPUnit\Runner\Version; - -/** - * The preload.php contains 2 dependencies - * - phpstan/phpdoc-parser - * - nikic/php-parser - * - * They need to be loaded early to avoid conflict version between rector prefixed vendor and Project vendor - * For example, a project may use phpstan/phpdoc-parser v1, while rector uses phpstan/phpdoc-parser uses v2, that will error as class or logic are different. - */ -if ( - // verify PHPUnit is running - defined('PHPUNIT_COMPOSER_INSTALL') - - // no need to preload if Node interface exists - && ! interface_exists(Node::class, false) - - // ensure force autoload version with class_exists() with true argument as not yet loaded - // for phpunit 12+ only - && class_exists(Version::class, true) && (int) Version::id() >= 12 -) { - require_once __DIR__ . '/preload.php'; -} - // inspired by https://github.com/phpstan/phpstan/blob/master/bootstrap.php spl_autoload_register(function (string $class): void { static $composerAutoloader; diff --git a/phpstan.neon b/phpstan.neon index 457698c14fe..b63fa6411e0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -154,6 +154,8 @@ parameters: # for config class reflection - src/Bootstrap/ExtensionConfigResolver.php - src/Validation/RectorConfigValidator.php + # for phpunit version check + - src/Testing/PHPUnit/AbstractLazyTestCase.php # use of internal phpstan classes - diff --git a/scoper.php b/scoper.php index 73d00ecf873..e8893e4b34f 100644 --- a/scoper.php +++ b/scoper.php @@ -33,6 +33,7 @@ 'exclude-classes' => [ 'PHPUnit\Framework\Constraint\IsEqual', 'PHPUnit\Framework\TestCase', + 'PHPUnit\Runner\Version', 'PHPUnit\Framework\ExpectationFailedException', // native class on php 8.3+ diff --git a/src/Testing/PHPUnit/AbstractLazyTestCase.php b/src/Testing/PHPUnit/AbstractLazyTestCase.php index 720990bc0ac..3fc41dd2e3a 100644 --- a/src/Testing/PHPUnit/AbstractLazyTestCase.php +++ b/src/Testing/PHPUnit/AbstractLazyTestCase.php @@ -5,6 +5,7 @@ namespace Rector\Testing\PHPUnit; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; use Rector\Config\RectorConfig; use Rector\DependencyInjection\LazyContainerFactory; @@ -63,7 +64,13 @@ private function includePreloadFilesAndScoperAutoload(): void { if (file_exists(__DIR__ . '/../../../preload.php')) { if (file_exists(__DIR__ . '/../../../vendor')) { - require_once __DIR__ . '/../../../preload.php'; + /** + * On PHPUnit 12+, when classmap autoloaded, it means preload already loaded early + */ + if (! class_exists(Version::class, true) || (int) Version::id() < 12) { + require_once __DIR__ . '/../../../preload.php'; + } + // test case in rector split package } elseif (file_exists(__DIR__ . '/../../../../../../vendor')) { require_once __DIR__ . '/../../../preload-split-package.php';