From f8dc77943fb3d466507f66d6c34fd9806d58befd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 1 Nov 2023 21:02:23 +0100 Subject: [PATCH 1/5] fix: Correctly include all the JetBrains' stubs as excluded files --- scoper.inc.php | 66 ++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/scoper.inc.php b/scoper.inc.php index e99996fd1..13c6e01fd 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -13,33 +13,35 @@ */ $jetBrainStubs = (static function (): array { + $packageDir = __DIR__.'/vendor/jetbrains/phpstorm-stubs'; + $ignoredDirectories = [ + $packageDir.'/tests', + $packageDir.'/meta', + ]; $files = []; - foreach (new DirectoryIterator(__DIR__.'/vendor/jetbrains/phpstorm-stubs') as $directoryInfo) { - if ($directoryInfo->isDot()) { - continue; - } - - if (false === $directoryInfo->isDir()) { - continue; - } - - if (in_array($directoryInfo->getBasename(), ['tests', 'meta'], true)) { - continue; - } - - foreach (new DirectoryIterator($directoryInfo->getPathName()) as $fileInfo) { - if ($fileInfo->isDot()) { + $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() + ) { continue; } - if (1 !== preg_match('/\.php$/', $fileInfo->getBasename())) { - 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; })(); @@ -50,37 +52,15 @@ 'exclude-classes' => [ 'Isolated\Symfony\Component\Finder\Finder', ], + 'exclude-functions' => [ + 'trigger_deprecation', + ], 'exclude-constants' => [ // Symfony global constants '/^SYMFONY\_[\p{L}_]+$/', ], 'exclude-files' => $jetBrainStubs, 'patchers' => [ - // - // PHPStorm stub map: leave it unchanged - // - 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, - ); - }, // // Reflector: leave the registered internal symbols unchanged // From a7f834770ef5108a1adceebbcb7df976acb1aae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 1 Nov 2023 21:05:08 +0100 Subject: [PATCH 2/5] fix cs --- scoper.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scoper.inc.php b/scoper.inc.php index 13c6e01fd..64ebc93b3 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -32,7 +32,7 @@ } foreach ($ignoredDirectories as $ignoredDirectory) { - if (str_starts_with($fileInfo->getPathname(), $ignoredDirectory)) { + if (str_starts_with($fileInfo->getPathname(), $ignoredDirectory)) { continue 2; } } From c653499619c2c031f0928d5fcaf4f8bbd8ba76bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 1 Nov 2023 21:51:03 +0100 Subject: [PATCH 3/5] fix --- scoper.inc.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/scoper.inc.php b/scoper.inc.php index 64ebc93b3..a564a0447 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -46,6 +46,9 @@ return $files; })(); +var_dump(array_map(static fn ($path) => substr($path, 72), $jetBrainStubs)); +exit; + return [ 'expose-global-functions' => true, 'expose-global-classes' => true, @@ -61,6 +64,32 @@ ], '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, + ); + }, // // Reflector: leave the registered internal symbols unchanged // From d35447117f9ae5f378bbbb9caa66da82bf36ddff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 1 Nov 2023 21:51:18 +0100 Subject: [PATCH 4/5] cleanup --- scoper.inc.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/scoper.inc.php b/scoper.inc.php index a564a0447..2a4dbdeb5 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -46,9 +46,6 @@ return $files; })(); -var_dump(array_map(static fn ($path) => substr($path, 72), $jetBrainStubs)); -exit; - return [ 'expose-global-functions' => true, 'expose-global-classes' => true, From 06bf3aaeacf59f89b103ffaa87f06c6884d91d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 1 Nov 2023 23:14:27 +0100 Subject: [PATCH 5/5] fix --- scoper.inc.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scoper.inc.php b/scoper.inc.php index 2a4dbdeb5..f7edf89bc 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -27,6 +27,9 @@ || $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; }