Skip to content

feat: Make the JetBrains' stubs collector and patcher re-usable #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'composer-root-version-checker/bin',
'composer-root-version-checker/src',
'composer-root-version-checker/tests',
'res',
'src',
'tests',
])
Expand Down
47 changes: 47 additions & 0 deletions res/create-scoper-phpstorm-stubs-map-patcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

// should be kept intact except for its namespace, which needs to be prefixed, as otherwise,
// its autoloading would break.

return static function (
?string $stubsMapPath = null,
?string $stubsMapVendorPath = null,
): Closure {
$stubsMapVendorPath ??= 'vendor/jetbrains/phpstorm-stubs/PhpStormStubsMap.php';
$stubsMapPath ??= __DIR__.'/../'.$stubsMapVendorPath;

$stubsMapOriginalContent = file_get_contents($stubsMapPath);

if (!preg_match('/class PhpStormStubsMap([\s\S]+)/', $stubsMapOriginalContent, $matches)) {
throw new InvalidArgumentException('Could not capture the map original content.');
}

$stubsMapClassOriginalContent = $matches[1];

return static function (string $filePath, string $prefix, string $contents) use (
$stubsMapVendorPath,
$stubsMapClassOriginalContent,
): string {
if ($filePath !== $stubsMapVendorPath) {
return $contents;
}

return preg_replace(
'/class PhpStormStubsMap([\s\S]+)/',
'class PhpStormStubsMap'.$stubsMapClassOriginalContent,
$contents,
);
};
};
54 changes: 54 additions & 0 deletions res/get-scoper-phpstorm-stubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

// excluded, see scoper-phpstorm-stubs-map-patcher.php for more information.

$defaultSource = __DIR__.'/../vendor/jetbrains/phpstorm-stubs';

return static function (?string $stubsDir = null) use ($defaultSource): array {
$packageDir = $stubsDir ?? $defaultSource;
$ignoredDirectories = [
$packageDir.'/tests',
$packageDir.'/meta',
];
$files = [];

$collectFiles = static function (RecursiveIteratorIterator $iterator) use (&$files, $ignoredDirectories): void {
foreach ($iterator as $fileInfo) {
/** @var SplFileInfo $fileInfo */
if (str_starts_with($fileInfo->getFilename(), '.')
|| $fileInfo->isDir()
|| !$fileInfo->isReadable()
|| 'php' !== $fileInfo->getExtension()
// The map needs to be excluded from "exclude-files" as otherwise its namespace cannot be corrected
// via a patcher
|| $fileInfo->getFilename() === 'PhpStormStubsMap.php'
) {
continue;
}

foreach ($ignoredDirectories as $ignoredDirectory) {
if (str_starts_with($fileInfo->getPathname(), $ignoredDirectory)) {
continue 2;
}
}

$files[] = $fileInfo->getPathName();
}
};

$collectFiles(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($packageDir)));

return $files;
};
65 changes: 3 additions & 62 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,8 @@
* file that was distributed with this source code.
*/

$jetBrainStubs = (static function (): array {
$packageDir = __DIR__.'/vendor/jetbrains/phpstorm-stubs';
$ignoredDirectories = [
$packageDir.'/tests',
$packageDir.'/meta',
];
$files = [];

$collectFiles = static function (RecursiveIteratorIterator $iterator) use (&$files, $ignoredDirectories): void {
foreach ($iterator as $fileInfo) {
/** @var SplFileInfo $fileInfo */
if (str_starts_with($fileInfo->getFilename(), '.')
|| $fileInfo->isDir()
|| !$fileInfo->isReadable()
|| 'php' !== $fileInfo->getExtension()
// The map needs to be excluded from "exclude-files" as otherwise its namespace cannot be corrected
// via a patcher
|| $fileInfo->getFilename() === 'PhpStormStubsMap.php'
) {
continue;
}

foreach ($ignoredDirectories as $ignoredDirectory) {
if (str_starts_with($fileInfo->getPathname(), $ignoredDirectory)) {
continue 2;
}
}

$files[] = $fileInfo->getPathName();
}
};

$collectFiles(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($packageDir)));

return $files;
})();
$jetBrainStubs = (require __DIR__.'/res/get-scoper-phpstorm-stubs.php')();
$jetBrainStubsPatcher = (require __DIR__.'/res/create-scoper-phpstorm-stubs-map-patcher.php')();

return [
'expose-global-functions' => true,
Expand All @@ -64,32 +30,7 @@
],
'exclude-files' => $jetBrainStubs,
'patchers' => [
//
// PHPStorm stub map: adjust the namespace to fix the autoloading, but keep it
// unchanged otherwise.
//
static function (string $filePath, string $prefix, string $contents): string {
if ('vendor/jetbrains/phpstorm-stubs/PhpStormStubsMap.php' !== $filePath) {
return $contents;
}

return str_replace(
[
$prefix.'\\\\',
$prefix.'\\',
'namespace JetBrains\PHPStormStub;',
],
[
'',
'',
sprintf(
'namespace %s\JetBrains\PHPStormStub;',
$prefix,
),
],
$contents,
);
},
$jetBrainStubsPatcher,
//
// Reflector: leave the registered internal symbols unchanged
//
Expand Down