From 954ae358f270c7aae7c03068c7be72091f06cbc6 Mon Sep 17 00:00:00 2001 From: yaozm Date: Thu, 11 Jan 2024 10:56:04 +0800 Subject: [PATCH] chore(composer): update composer-fixer script - Updated composer-fixer script to use Composer\Config::disableProcessTimeout - Added new constants for mhash functions - Added DowngradeLevelSetList::DOWN_TO_PHP_74 to Rector config --- composer-fixer | 186 +++++++++++++++++++++++++++++++++++++++++++++ composer-fixer.php | 62 --------------- composer.json | 5 +- rector.php | 6 ++ 4 files changed, 196 insertions(+), 63 deletions(-) create mode 100755 composer-fixer delete mode 100755 composer-fixer.php diff --git a/composer-fixer b/composer-fixer new file mode 100755 index 0000000..61de55c --- /dev/null +++ b/composer-fixer @@ -0,0 +1,186 @@ +#!/usr/bin/env php + + * + * This source file is subject to the MIT license that is bundled. + */ + +use Composer\InstalledVersions; +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Output\ConsoleOutput; +use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\PhpExecutableFinder; +use Symfony\Component\Process\Process; + +require __DIR__.'/vendor/autoload.php'; + +(new class() { + private string $composerJsonFile = __DIR__.'/composer.json'; + + private SymfonyStyle $symfonyStyle; + + public function __construct() + { + $this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput()); + } + + public function __invoke(): void + { + $this + ->updateComposerPackages() + ->updateComposerJsonFile($this->updateComposerDecodedJson()) + ->updateComposerPackages() + ->normalizeComposerJsonFile() + ->success(); + } + + protected function getComposerDecodedJson(): array + { + /** @noinspection JsonEncodingApiUsageInspection */ + return json_decode(file_get_contents($this->composerJsonFile), true); + } + + private function updateComposerPackages(): self + { + $this->createAndMustRunProcess("{$this->findComposerBinary()} update -W --ansi -v"); + + return $this; + } + + private function updateComposerJsonFile(array $composerDecodedJson): self + { + /** @noinspection JsonEncodingApiUsageInspection */ + file_put_contents( + $this->composerJsonFile, + json_encode( + $composerDecodedJson, + JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES + ).PHP_EOL + ); + + return $this; + } + + private function updateComposerDecodedJson(): array + { + $composerOutdatedPackages = $this->getComposerOutdatedPackages(); + $composerDecodedJson = $this->getComposerDecodedJson(); + + foreach ($composerDecodedJson as $option => &$value) { + if (! in_array($option, ['require', 'require-dev'], true)) { + continue; + } + + foreach ($value as $package => &$version) { + if ( + 'php' === $package + || '*' === $version + || str_starts_with($package, 'ext-') + || str_contains($version, '-') + || str_contains($version, '@') + || str_contains($version, '|') + ) { + continue; + } + + // Update composer outdated package version + if (in_array($package, $composerOutdatedPackages, true)) { + $this->updateComposerOutdatedPackage($package, $option); + } + + // Update composer package version + $version = '^'.implode( + '.', + array_slice( + explode('.', InstalledVersions::getVersion($package), 3), + 0, + 2 + ) + ); + } + } + + return $composerDecodedJson; + } + + private function normalizeComposerJsonFile(): self + { + $this->createAndMustRunProcess("{$this->findComposerBinary()} normalize --dry-run --diff --ansi -v"); + + return $this; + } + + /** + * @param null|array|string $message + */ + private function success($message = null): void + { + $this->symfonyStyle->success($message ?: 'Composer packages updated successfully!'); + + exit(0); + } + + private function getComposerOutdatedPackages(): array + { + /** @noinspection JsonEncodingApiUsageInspection */ + return array_map( + static fn (array $package): string => $package['name'], + json_decode( + $this + ->createAndMustRunProcess("{$this->findComposerBinary()} outdated --format=json --direct --ansi -v") + ->getOutput(), + true + )['installed'] + ); + } + + private function updateComposerOutdatedPackage(string $composerOutdatedPackage, string $environment): void + { + try { + $this->createAndMustRunProcess(sprintf( + "{$this->findComposerBinary()} require $composerOutdatedPackage %s --no-scripts -W --ansi -v", + 'require-dev' === $environment ? '--dev' : '' + )); + } catch (ProcessFailedException $processFailedException) { + $this->symfonyStyle->error("Failed to update composer outdated [$composerOutdatedPackage] package version."); + } + } + + private function findComposerBinary(): string + { + return sprintf( + '%s %s', + (new PhpExecutableFinder())->find(), + (new Symfony\Component\Process\ExecutableFinder())->find('composer') + ); + } + + /** + * @param array|string $command + * @param mixed $input The input as stream resource, scalar or \Traversable, or null for no input + */ + private function createAndMustRunProcess( + $command, + ?string $cwd = null, + ?array $env = null, + $input = null, + ?float $timeout = 60 + ): Process { + $process = is_string($command) + ? Process::fromShellCommandline($command, $cwd, $env, $input, $timeout) + : new Process($command, $cwd, $env, $input, $timeout); + + $this->symfonyStyle->warning($process->getCommandLine()); + + return $process + ->setWorkingDirectory(dirname($this->composerJsonFile)) + ->setEnv(['COMPOSER_MEMORY_LIMIT' => -1]) + ->mustRun(fn (string $type, string $buffer) => $this->symfonyStyle->write($buffer)); + } +})(); diff --git a/composer-fixer.php b/composer-fixer.php deleted file mode 100755 index 24f510f..0000000 --- a/composer-fixer.php +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env php - - * - * This source file is subject to the MIT license that is bundled. - */ - -use Symfony\Component\Console\Input\ArgvInput; -use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Style\SymfonyStyle; -use Symfony\Component\Process\Exception\ProcessFailedException; -use Symfony\Component\Process\PhpExecutableFinder; -use Symfony\Component\Process\Process; - -require __DIR__.'/vendor/autoload.php'; - -/** @noinspection JsonEncodingApiUsageInspection */ -$composerJson = json_decode(file_get_contents(__DIR__.'/composer.json'), true); -$symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput()); - -foreach ($composerJson as $option => $value) { - if (! in_array($option, ['require', 'require-dev'], true)) { - continue; - } - - foreach ($value as $package => $version) { - if ( - 'php' === $package - || '*' === $version - || str_starts_with($package, 'ext-') - || str_starts_with($version, 'dev-') - || str_contains($version, '|') - ) { - continue; - } - - $symfonyStyle->warning("Fixing $option $package ..."); - - try { - Process::fromShellCommandline(sprintf( - 'COMPOSER_MEMORY_LIMIT=-1 %s %s require %s %s --ansi -W -v', - (new PhpExecutableFinder())->find(), - (new Symfony\Component\Process\ExecutableFinder())->find('composer'), - $package, - 'require-dev' === $option ? '--dev' : '' - ))->mustRun(static function ($type, $buffer) use ($symfonyStyle): void { - $symfonyStyle->write($buffer); - }); - - $symfonyStyle->warning("Fixing $option $package success."); - } catch (ProcessFailedException $e) { - $symfonyStyle->error("Fixing $option $package failed."); - } - } -} - -$symfonyStyle->success('All done.'); diff --git a/composer.json b/composer.json index 3f93d38..285a2a4 100644 --- a/composer.json +++ b/composer.json @@ -165,7 +165,10 @@ "checks-parallel": "@composer-parallel composer-validate md-lint lint style-lint test psalm", "composer-bin-all-update": "@composer bin all update --ansi -v", "composer-check-platform-reqs": "@composer check-platform-reqs --lock --ansi -v", - "composer-fixer": "@php ./composer-fixer.php", + "composer-fixer": [ + "Composer\\Config::disableProcessTimeout", + "@php ./composer-fixer" + ], "composer-normalize": "@composer normalize --dry-run --diff --ansi -v", "composer-parallel": "@composer parallel --ansi -v", "composer-unused": "@php ./vendor/bin/composer-unused --ansi -v", diff --git a/rector.php b/rector.php index 0d8fafb..63bf643 100644 --- a/rector.php +++ b/rector.php @@ -38,12 +38,17 @@ use Rector\PHPUnit\Set\PHPUnitLevelSetList; use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Renaming\Rector\FuncCall\RenameFunctionRector; +use Rector\Set\ValueObject\DowngradeLevelSetList; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector; use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector; return static function (RectorConfig $rectorConfig): void { + define('MHASH_XXH3', 2 << 0); + define('MHASH_XXH32', 2 << 1); + define('MHASH_XXH64', 2 << 2); + define('MHASH_XXH128', 2 << 3); $rectorConfig->importNames(false, false); $rectorConfig->importShortClasses(false); // $rectorConfig->disableParallel(); @@ -152,6 +157,7 @@ ]); $rectorConfig->sets([ + DowngradeLevelSetList::DOWN_TO_PHP_74, LevelSetList::UP_TO_PHP_74, SetList::PHP_74, SetList::CODE_QUALITY,