Skip to content

Commit 188b90a

Browse files
authored
[Scoped] Load early preload.php on scoped bootstrap.php on phpunit 12+ running (#7451)
* [Scoped] Load early preload.php on scoped bootstrap.php on phpunit running * ensure verify Node interface exists * handling on phpunit 12.+ only * final touch: better note example use of phpstan/phpdoc-parser that conflict can be early * final touch: better note example use of phpstan/phpdoc-parser that conflict can be early
1 parent 7ccb174 commit 188b90a

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

build/target-repository/bootstrap.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
4+
5+
use PHPParser\Node;
6+
use PHPUnit\Runner\Version;
7+
8+
/**
9+
* The preload.php contains 2 dependencies
10+
* - phpstan/phpdoc-parser
11+
* - nikic/php-parser
12+
*
13+
* They need to be loaded early to avoid conflict version between rector prefixed vendor and Project vendor
14+
* 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.
15+
*/
16+
if (
17+
// verify PHPUnit is running
18+
defined('PHPUNIT_COMPOSER_INSTALL')
19+
20+
// no need to preload if Node interface exists
21+
&& ! interface_exists(Node::class, false)
22+
23+
// ensure force autoload version with class_exists() with true argument as not yet loaded
24+
// for phpunit 12+ only
25+
&& class_exists(Version::class, true) && (int) Version::id() >= 12
26+
) {
27+
require_once __DIR__ . '/preload.php';
28+
}
429

530
// inspired by https://github.com/phpstan/phpstan/blob/master/bootstrap.php
631
spl_autoload_register(function (string $class): void {

0 commit comments

Comments
 (0)