Skip to content

Commit e51506d

Browse files
committed
Restore preload-split-package.php to fix PHPStan Turbo notice on rector-* packages
1 parent 8526207 commit e51506d

5 files changed

Lines changed: 449 additions & 7 deletions

File tree

‎bin/rector.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
$autoloadIncluder = new AutoloadIncluder();
3333
$autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();
3434

35+
// rector-src on split packages: Turbo resolves php-parser classes through the project autoloader,
36+
// as preload-split-package.php declares them only after Turbo is activated
37+
if (file_exists(__DIR__ . '/../preload-split-package.php') && is_dir(__DIR__ . '/../../../../vendor')) {
38+
$autoloadIncluder->autoloadProjectAutoloaderFile();
39+
}
40+
3541
// PHPStan Turbo replaces php-parser classes, so it is activated before preload.php declares them.
3642
// The main process only gets Turbo when php.ini loads it; the workers get it from WorkerCommandLineFactory.
3743
$phpstanBootstrapFile = __DIR__ . '/../../../phpstan/phpstan/bootstrap.php';
@@ -140,6 +146,11 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void
140146
require_once __DIR__ . '/../preload.php';
141147
}
142148

149+
// require rector-src on split packages
150+
if (file_exists(__DIR__ . '/../preload-split-package.php') && is_dir(__DIR__ . '/../../../../vendor')) {
151+
require_once __DIR__ . '/../preload-split-package.php';
152+
}
153+
143154
$autoloadIncluder->loadIfExistsAndNotLoadedYet(__DIR__ . '/../vendor/scoper-autoload.php');
144155
$autoloadIncluder->autoloadProjectAutoloaderFile();
145156
$autoloadIncluder->autoloadRectorInstalledAsGlobalDependency();

‎build/build-preload.php‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
$preloadBuilder = new PreloadBuilder();
4141
$preloadBuilder->buildPreloadScript($buildDirectory, $buildDirectory . '/preload.php');
42+
$preloadBuilder->buildPreloadScriptForSplitPackage($buildDirectory, $buildDirectory . '/preload-split-package.php');
4243

4344
final class PreloadBuilder
4445
{
@@ -141,6 +142,44 @@ public function buildPreloadScript(string $buildDirectory, string $preloadFile):
141142
$this->buildPreloadScriptPhpDocParser($buildDirectory, $preloadFile);
142143
}
143144

145+
public function buildPreloadScriptForSplitPackage(string $buildDirectory, string $preloadFile): void
146+
{
147+
$this->buildPreloadScriptForSplitPhpParser($buildDirectory, $preloadFile);
148+
$this->buildPreloadScriptForSplitPhpDocParser($buildDirectory, $preloadFile);
149+
}
150+
151+
private function buildPreloadScriptForSplitPhpParser(string $buildDirectory, string $preloadFile): void
152+
{
153+
$vendorDir = $buildDirectory . '/vendor';
154+
if (! is_dir($vendorDir . '/nikic/php-parser/lib/PhpParser')) {
155+
return;
156+
}
157+
158+
// 1. find php-parser file infos
159+
$fileInfos = $this->findPhpParserFilesAndSortThem($vendorDir);
160+
161+
// 2. create preload-split-package.php from provided files
162+
$preloadFileContent = $this->createPreloadFileContent($fileInfos, '/../../../vendor/');
163+
164+
file_put_contents($preloadFile, $preloadFileContent);
165+
}
166+
167+
private function buildPreloadScriptForSplitPhpDocParser(string $buildDirectory, string $preloadFile): void
168+
{
169+
$vendorDir = $buildDirectory . '/vendor';
170+
if (! is_dir($vendorDir . '/phpstan/phpdoc-parser')) {
171+
return;
172+
}
173+
174+
// 1. find phpdoc-parser file infos
175+
$fileInfos = $this->findPhpDocParserFilesAndSortThem($vendorDir);
176+
177+
// 2. create preload-split-package.php from provided files
178+
$preloadFileContent = $this->createPreloadFileContent($fileInfos, '/../../../vendor/', true);
179+
180+
file_put_contents($preloadFile, $preloadFileContent, FILE_APPEND);
181+
}
182+
144183
private function buildPreloadScriptPhpDocParser(string $buildDirectory, string $preloadFile): void
145184
{
146185
$vendorDir = $buildDirectory . '/vendor';
@@ -152,7 +191,7 @@ private function buildPreloadScriptPhpDocParser(string $buildDirectory, string $
152191
$fileInfos = $this->findPhpDocParserFilesAndSortThem($vendorDir);
153192

154193
// 2. create preload.php from provided files
155-
$preloadFileContent = $this->createPreloadFileContent($fileInfos, true);
194+
$preloadFileContent = $this->createPreloadFileContent($fileInfos, '/vendor/', true);
156195

157196
file_put_contents($preloadFile, $preloadFileContent, FILE_APPEND);
158197
}
@@ -168,7 +207,7 @@ private function buildPreloadScriptPhpParser(string $buildDirectory, string $pre
168207
$fileInfos = $this->findPhpParserFilesAndSortThem($vendorDir);
169208

170209
// 3. create preload.php from provided files
171-
$preloadFileContent = $this->createPreloadFileContent($fileInfos);
210+
$preloadFileContent = $this->createPreloadFileContent($fileInfos, '/vendor/');
172211

173212
file_put_contents($preloadFile, $preloadFileContent);
174213
}
@@ -207,7 +246,7 @@ private function findPhpDocParserFiles(string $vendorDir): array
207246
/**
208247
* @param SplFileInfo[] $fileInfos
209248
*/
210-
private function createPreloadFileContent(array $fileInfos, bool $append = false): string
249+
private function createPreloadFileContent(array $fileInfos, string $vendorPath, bool $append = false): string
211250
{
212251
$preloadFileContent = $append ? '' : self::PRELOAD_FILE_TEMPLATE . PHP_EOL . PHP_EOL;
213252

@@ -217,23 +256,24 @@ private function createPreloadFileContent(array $fileInfos, bool $append = false
217256
continue;
218257
}
219258

220-
$preloadFileContent .= $this->createRequireOnceFilePathLine($realPath);
259+
$preloadFileContent .= $this->createRequireOnceFilePathLine($realPath, $vendorPath);
221260
}
222261

223262
return $preloadFileContent;
224263
}
225264

226-
private function createRequireOnceFilePathLine(string $realPath): string
265+
private function createRequireOnceFilePathLine(string $realPath, string $vendorPath): string
227266
{
228267
if (! str_contains($realPath, 'vendor')) {
229268
$filePath = '/src/' . Strings::after($realPath, '/src/');
230269
return "require_once __DIR__ . '" . $filePath . "';" . PHP_EOL;
231270
}
232271

233-
$filePath = '/vendor/' . Strings::after($realPath, 'vendor/');
272+
$vendorFilePath = Strings::after($realPath, 'vendor/');
273+
$filePath = $vendorPath . $vendorFilePath;
234274

235275
// PHPStan Turbo declares its native NodeTraverser before this file runs
236-
if ($filePath === '/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php') {
276+
if ($vendorFilePath === 'nikic/php-parser/lib/PhpParser/NodeTraverser.php') {
237277
return 'if (! class_exists(NodeTraverser::class, false)) {' . PHP_EOL
238278
. " require_once __DIR__ . '" . $filePath . "';" . PHP_EOL
239279
. '}' . PHP_EOL

‎ecs.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
// avoid re-running on build
3030
__DIR__ . '/preload.php',
31+
__DIR__ . '/preload-split-package.php',
3132

3233
PhpdocTypesFixer::class => [
3334
// double to Double false positive

0 commit comments

Comments
 (0)