Skip to content

Commit 1bc4dda

Browse files
authored
feat: Make the JetBrains' stubs collector and patcher re-usable (#883)
1 parent 625c41e commit 1bc4dda

File tree

4 files changed

+105
-62
lines changed

4 files changed

+105
-62
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'composer-root-version-checker/bin',
2121
'composer-root-version-checker/src',
2222
'composer-root-version-checker/tests',
23+
'res',
2324
'src',
2425
'tests',
2526
])
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
// should be kept intact except for its namespace, which needs to be prefixed, as otherwise,
16+
// its autoloading would break.
17+
18+
return static function (
19+
?string $stubsMapPath = null,
20+
?string $stubsMapVendorPath = null,
21+
): Closure {
22+
$stubsMapVendorPath ??= 'vendor/jetbrains/phpstorm-stubs/PhpStormStubsMap.php';
23+
$stubsMapPath ??= __DIR__.'/../'.$stubsMapVendorPath;
24+
25+
$stubsMapOriginalContent = file_get_contents($stubsMapPath);
26+
27+
if (!preg_match('/class PhpStormStubsMap([\s\S]+)/', $stubsMapOriginalContent, $matches)) {
28+
throw new InvalidArgumentException('Could not capture the map original content.');
29+
}
30+
31+
$stubsMapClassOriginalContent = $matches[1];
32+
33+
return static function (string $filePath, string $prefix, string $contents) use (
34+
$stubsMapVendorPath,
35+
$stubsMapClassOriginalContent,
36+
): string {
37+
if ($filePath !== $stubsMapVendorPath) {
38+
return $contents;
39+
}
40+
41+
return preg_replace(
42+
'/class PhpStormStubsMap([\s\S]+)/',
43+
'class PhpStormStubsMap'.$stubsMapClassOriginalContent,
44+
$contents,
45+
);
46+
};
47+
};

res/get-scoper-phpstorm-stubs.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
// excluded, see scoper-phpstorm-stubs-map-patcher.php for more information.
16+
17+
$defaultSource = __DIR__.'/../vendor/jetbrains/phpstorm-stubs';
18+
19+
return static function (?string $stubsDir = null) use ($defaultSource): array {
20+
$packageDir = $stubsDir ?? $defaultSource;
21+
$ignoredDirectories = [
22+
$packageDir.'/tests',
23+
$packageDir.'/meta',
24+
];
25+
$files = [];
26+
27+
$collectFiles = static function (RecursiveIteratorIterator $iterator) use (&$files, $ignoredDirectories): void {
28+
foreach ($iterator as $fileInfo) {
29+
/** @var SplFileInfo $fileInfo */
30+
if (str_starts_with($fileInfo->getFilename(), '.')
31+
|| $fileInfo->isDir()
32+
|| !$fileInfo->isReadable()
33+
|| 'php' !== $fileInfo->getExtension()
34+
// The map needs to be excluded from "exclude-files" as otherwise its namespace cannot be corrected
35+
// via a patcher
36+
|| $fileInfo->getFilename() === 'PhpStormStubsMap.php'
37+
) {
38+
continue;
39+
}
40+
41+
foreach ($ignoredDirectories as $ignoredDirectory) {
42+
if (str_starts_with($fileInfo->getPathname(), $ignoredDirectory)) {
43+
continue 2;
44+
}
45+
}
46+
47+
$files[] = $fileInfo->getPathName();
48+
}
49+
};
50+
51+
$collectFiles(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($packageDir)));
52+
53+
return $files;
54+
};

scoper.inc.php

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,8 @@
1212
* file that was distributed with this source code.
1313
*/
1414

15-
$jetBrainStubs = (static function (): array {
16-
$packageDir = __DIR__.'/vendor/jetbrains/phpstorm-stubs';
17-
$ignoredDirectories = [
18-
$packageDir.'/tests',
19-
$packageDir.'/meta',
20-
];
21-
$files = [];
22-
23-
$collectFiles = static function (RecursiveIteratorIterator $iterator) use (&$files, $ignoredDirectories): void {
24-
foreach ($iterator as $fileInfo) {
25-
/** @var SplFileInfo $fileInfo */
26-
if (str_starts_with($fileInfo->getFilename(), '.')
27-
|| $fileInfo->isDir()
28-
|| !$fileInfo->isReadable()
29-
|| 'php' !== $fileInfo->getExtension()
30-
// The map needs to be excluded from "exclude-files" as otherwise its namespace cannot be corrected
31-
// via a patcher
32-
|| $fileInfo->getFilename() === 'PhpStormStubsMap.php'
33-
) {
34-
continue;
35-
}
36-
37-
foreach ($ignoredDirectories as $ignoredDirectory) {
38-
if (str_starts_with($fileInfo->getPathname(), $ignoredDirectory)) {
39-
continue 2;
40-
}
41-
}
42-
43-
$files[] = $fileInfo->getPathName();
44-
}
45-
};
46-
47-
$collectFiles(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($packageDir)));
48-
49-
return $files;
50-
})();
15+
$jetBrainStubs = (require __DIR__.'/res/get-scoper-phpstorm-stubs.php')();
16+
$jetBrainStubsPatcher = (require __DIR__.'/res/create-scoper-phpstorm-stubs-map-patcher.php')();
5117

5218
return [
5319
'expose-global-functions' => true,
@@ -64,32 +30,7 @@
6430
],
6531
'exclude-files' => $jetBrainStubs,
6632
'patchers' => [
67-
//
68-
// PHPStorm stub map: adjust the namespace to fix the autoloading, but keep it
69-
// unchanged otherwise.
70-
//
71-
static function (string $filePath, string $prefix, string $contents): string {
72-
if ('vendor/jetbrains/phpstorm-stubs/PhpStormStubsMap.php' !== $filePath) {
73-
return $contents;
74-
}
75-
76-
return str_replace(
77-
[
78-
$prefix.'\\\\',
79-
$prefix.'\\',
80-
'namespace JetBrains\PHPStormStub;',
81-
],
82-
[
83-
'',
84-
'',
85-
sprintf(
86-
'namespace %s\JetBrains\PHPStormStub;',
87-
$prefix,
88-
),
89-
],
90-
$contents,
91-
);
92-
},
33+
$jetBrainStubsPatcher,
9334
//
9435
// Reflector: leave the registered internal symbols unchanged
9536
//

0 commit comments

Comments
 (0)