Skip to content

Commit b177eb8

Browse files
authored
[autoload] Add workaround for phpstan + rector tests co-run to avoid duplicated php-parser loading error (#7440)
* [autoload] Add workaround for phpstan + rector tests co-run to avoid duplicated php-parser loading error * rebuild autoload * cs * remove cs from preload
1 parent 89bfc23 commit b177eb8

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

build/build-preload.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,28 @@ final class PreloadBuilder
5151
5252
declare(strict_types=1);
5353
54+
use PhpParser\Node;
55+
use PHPStan\Testing\PHPStanTestCase;
56+
5457
if (defined('__PHPSTAN_RUNNING__')) {
5558
return;
5659
}
5760
61+
// edge case during Rector tests case, happens when
62+
// 1. phpstan autoload test case is triggered first,
63+
// 2. all php-parser classes are loaded,
64+
if (defined('PHPUNIT_COMPOSER_INSTALL') && isPHPStanTestPreloaded()) {
65+
return;
66+
}
5867
68+
function isPHPStanTestPreloaded(): bool
69+
{
70+
if (! class_exists(PHPStanTestCase::class, false)) {
71+
return false;
72+
}
73+
74+
return interface_exists(Node::class, false);
75+
}
5976
CODE_SAMPLE;
6077

6178
/**

ecs.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
'*/Fixture/*',
2525
'*/Expected/*',
2626

27+
// avoid re-running on build
28+
__DIR__ . '/preload.php',
29+
2730
PhpdocTypesFixer::class => [
2831
// double to Double false positive
2932
__DIR__ . '/rules/Php74/Rector/Double/RealToFloatTypeCastRector.php',

preload-split-package.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22

33
declare(strict_types=1);
44

5+
use PhpParser\Node;
6+
use PHPStan\Testing\PHPStanTestCase;
7+
58
if (defined('__PHPSTAN_RUNNING__')) {
69
return;
710
}
811

9-
require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node.php';
12+
// edge case during Rector tests case, happens when
13+
// 1. phpstan autoload test case is triggered first,
14+
// 2. all php-parser classes are loaded,
15+
if (defined('PHPUNIT_COMPOSER_INSTALL') && isPHPStanTestPreloaded()) {
16+
return;
17+
}
18+
19+
function isPHPStanTestPreloaded(): bool
20+
{
21+
if (! class_exists(PHPStanTestCase::class, false)) {
22+
return false;
23+
}
24+
25+
return interface_exists(Node::class, false);
26+
}require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node.php';
1027
require_once __DIR__ . '/src/Contract/PhpParser/Node/StmtsAwareInterface.php';
1128
require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php';
1229
require_once __DIR__ . '/../../../vendor/nikic/php-parser/lib/PhpParser/Node/Expr.php';

preload.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22

33
declare(strict_types=1);
44

5+
use PhpParser\Node;
6+
use PHPStan\Testing\PHPStanTestCase;
7+
58
if (defined('__PHPSTAN_RUNNING__')) {
69
return;
710
}
811

9-
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node.php';
12+
// edge case during Rector tests case, happens when
13+
// 1. phpstan autoload test case is triggered first,
14+
// 2. all php-parser classes are loaded,
15+
if (defined('PHPUNIT_COMPOSER_INSTALL') && isPHPStanTestPreloaded()) {
16+
return;
17+
}
18+
19+
function isPHPStanTestPreloaded(): bool
20+
{
21+
if (! class_exists(PHPStanTestCase::class, false)) {
22+
return false;
23+
}
24+
25+
return interface_exists(Node::class, false);
26+
}require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node.php';
1027
require_once __DIR__ . '/src/Contract/PhpParser/Node/StmtsAwareInterface.php';
1128
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/NodeAbstract.php';
1229
require_once __DIR__ . '/vendor/nikic/php-parser/lib/PhpParser/Node/Expr.php';

src/BetterPhpDocParser/PhpDocManipulator/PhpDocTypeChanger.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node\FunctionLike;
88
use PhpParser\Node\Param;
99
use PhpParser\Node\Stmt;
10-
use PhpParser\Node\Stmt\ClassMethod;
1110
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
1211
use PHPStan\PhpDocParser\Ast\Node;
1312
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;

0 commit comments

Comments
 (0)