From e18b3b47c8e1b91f1844dd3c91d961e912476d86 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 25 Feb 2020 21:57:00 +0100 Subject: [PATCH] PHAR - rename file extensions of PhpStorm stubs --- compiler/composer.json | 3 +- compiler/src/Console/CompileCommand.php | 38 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/compiler/composer.json b/compiler/composer.json index 1b65bb2553..40f061363a 100644 --- a/compiler/composer.json +++ b/compiler/composer.json @@ -8,7 +8,8 @@ "nette/neon": "^3.0.0", "symfony/console": "^4.1", "symfony/process": "^4.1", - "symfony/filesystem": "^4.1" + "symfony/filesystem": "^4.1", + "symfony/finder": "^5.0" }, "autoload": { "psr-4": { diff --git a/compiler/src/Console/CompileCommand.php b/compiler/src/Console/CompileCommand.php index affb6615bf..033b2b4bef 100644 --- a/compiler/src/Console/CompileCommand.php +++ b/compiler/src/Console/CompileCommand.php @@ -48,6 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->processFactory->setOutput($output); $this->fixComposerJson($this->buildDir); + $this->renamePhpStormStubs(); $this->processFactory->create(['composer', 'update', '--no-dev', '--classmap-authoritative'], $this->buildDir); $this->processFactory->create(['php', 'box.phar', 'compile', '--no-parallel'], $this->dataDir); @@ -76,4 +77,41 @@ private function fixComposerJson(string $buildDir): void $this->filesystem->write($buildDir . '/composer.json', $encodedJson); } + private function renamePhpStormStubs(): void + { + $directory = $this->buildDir . '/vendor/jetbrains/phpstorm-stubs'; + if (!is_dir($directory)) { + return; + } + + $stubFinder = \Symfony\Component\Finder\Finder::create(); + $stubsMapPath = $directory . '/PhpStormStubsMap.php'; + foreach ($stubFinder->files()->name('*.php')->in($directory) as $stubFile) { + $path = $stubFile->getPathname(); + if ($path === $stubsMapPath) { + continue; + } + + $renameSuccess = rename( + $path, + dirname($path) . '/' . $stubFile->getBasename('.php') . '.stub' + ); + if ($renameSuccess === false) { + throw new \PHPStan\ShouldNotHappenException(sprintf('Could not rename %s', $path)); + } + } + + $stubsMapContents = file_get_contents($stubsMapPath); + if ($stubsMapContents === false) { + throw new \PHPStan\ShouldNotHappenException(sprintf('Could not read %s', $stubsMapPath)); + } + + $stubsMapContents = str_replace('.php\',', '.stub\',', $stubsMapContents); + + $putSuccess = file_put_contents($directory . '/PhpStormStubsMap.php', $stubsMapContents); + if ($putSuccess === false) { + throw new \PHPStan\ShouldNotHappenException(sprintf('Could not write %s', $stubsMapPath)); + } + } + }