Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into test/phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Nov 2, 2023
2 parents f2e2407 + 1bc4dda commit 2bba7a2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 62 deletions.
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 <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* 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 <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* 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

0 comments on commit 2bba7a2

Please sign in to comment.