diff --git a/build/target-repository/bootstrap.php b/build/target-repository/bootstrap.php index d6dd77ec353..e8ee6428d0a 100644 --- a/build/target-repository/bootstrap.php +++ b/build/target-repository/bootstrap.php @@ -2,6 +2,34 @@ 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. + */ +$isGlobalPHPUnit = class_exists(Version::class, false); +if ( + // verify PHPUnit is running + defined('PHPUNIT_COMPOSER_INSTALL') + + // no need to preload if Node interface exists + && ! interface_exists(Node::class, false) + + && ! $isGlobalPHPUnit + + // 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;