From 6874a5ae025ceba6a155314edb8731ec28e0c2ff Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 27 Feb 2024 13:40:34 +0200 Subject: [PATCH] Fix phpstan errors, converted UpdateDrushCommands to use custom IO --- phpstan.neon | 1 + .../Commands/PackageScannerDrushCommands.php | 9 +-- src/Drush/Commands/UpdateDrushCommands.php | 78 +++++++------------ .../CheckPackageVersionsCommandsTest.php | 7 +- 4 files changed, 39 insertions(+), 56 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 41e1542..dbcbd31 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,6 +4,7 @@ parameters: paths: - ./ excludePaths: + - sut - vendor - src/Drush/Commands/HelperDrushCommands.php - src/Drush/Commands/OpenShiftDrushCommands.php diff --git a/src/Drush/Commands/PackageScannerDrushCommands.php b/src/Drush/Commands/PackageScannerDrushCommands.php index 8b8ff7f..eb69757 100644 --- a/src/Drush/Commands/PackageScannerDrushCommands.php +++ b/src/Drush/Commands/PackageScannerDrushCommands.php @@ -10,9 +10,8 @@ use Drush\Attributes\Argument; use Drush\Attributes\Command; use Drush\Commands\DrushCommands; -use Drush\Drush; -use League\Container\Container; -use Symfony\Component\Console\Style\StyleInterface; +use Psr\Container\ContainerInterface as DrushContainer; +use Symfony\Component\Console\Style\OutputStyle; use Symfony\Component\Console\Style\SymfonyStyle; /** @@ -30,7 +29,7 @@ final class PackageScannerDrushCommands extends DrushCommands { */ public function __construct( private readonly VersionChecker $versionChecker, - private readonly StyleInterface $style, + private readonly OutputStyle $style, ) { parent::__construct(); } @@ -38,7 +37,7 @@ public function __construct( /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, Container $drush): self { + public static function create(ContainerInterface $container, DrushContainer $drush): self { return new self( $container->get('helfi_api_base.package_version_checker'), new SymfonyStyle($drush->get('input'), $drush->get('output')) diff --git a/src/Drush/Commands/UpdateDrushCommands.php b/src/Drush/Commands/UpdateDrushCommands.php index e6dd0c9..0ca0991 100644 --- a/src/Drush/Commands/UpdateDrushCommands.php +++ b/src/Drush/Commands/UpdateDrushCommands.php @@ -13,6 +13,9 @@ use Drush\Commands\DrushCommands; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; +use Psr\Container\ContainerInterface; +use Symfony\Component\Console\Style\OutputStyle; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; @@ -23,54 +26,33 @@ final class UpdateDrushCommands extends DrushCommands { private const BASE_URL = 'https://raw.githubusercontent.com/City-of-Helsinki/drupal-helfi-platform/main/'; - /** - * The http client. - * - * @var \GuzzleHttp\ClientInterface - */ - private readonly ClientInterface $httpClient; - - /** - * The filesystem. - * - * @var \Symfony\Component\Filesystem\Filesystem - */ - private readonly Filesystem $filesystem; - - /** - * The update manager. - * - * @var \DrupalTools\Update\UpdateHookManager - */ - private readonly UpdateHookManager $updateHookManager; - - /** - * The file manager. - * - * @var \DrupalTools\Update\FileManager - */ - private readonly FileManager $fileManager; - /** * Constructs a new instance. */ public function __construct( - Filesystem $filesystem = NULL, - ClientInterface $httpClient = NULL, - FileManager $fileManager = NULL, - UpdateHookManager $updateHookManager = NULL, + private readonly Filesystem $filesystem, + private readonly ClientInterface $httpClient, + private readonly FileManager $fileManager, + private readonly UpdateHookManager $updateHookManager, + private readonly OutputStyle $style, ) { parent::__construct(); + } - $this->filesystem = $filesystem ?: new Filesystem(); - $this->httpClient = $httpClient ?: new Client(['base_uri' => self::BASE_URL]); - $this->fileManager = $fileManager ?: new FileManager( - new HttpFileManager($this->httpClient), - $this->filesystem, - ); - $this->updateHookManager = $updateHookManager ?: new UpdateHookManager( - $this->filesystem, - $this->fileManager, + /** + * {@inheritdoc} + */ + public static function createEarly(ContainerInterface $container) : self { + $client = new Client(['base_uri' => self::BASE_URL]); + $fileSystem = new Filesystem(); + $fileManager = new FileManager(new HttpFileManager($client), $fileSystem); + + return new self( + new Filesystem(), + $client, + $fileManager, + new UpdateHookManager($fileSystem, $fileManager), + new SymfonyStyle($container->get('input'), $container->get('output')), ); } @@ -148,7 +130,7 @@ private function updateExternalPackages(UpdateOptions $options, string $root) : if (!$options->updateExternalPackages) { return $this; } - $this->io()->note('Checking external packages ...'); + $this->style->note('Checking external packages ...'); // Update druidfi/tools only if the package exists. if ($this->filesystem->exists($root . '/tools')) { @@ -156,7 +138,7 @@ private function updateExternalPackages(UpdateOptions $options, string $root) : 'make', 'self-update', ])->run(function (string $type, ?string $output) : void { - $this->io()->write($output); + $this->style->write($output); }); } return $this; @@ -205,14 +187,14 @@ private function needsUpdate(UpdateOptions $options) : bool { * The self. */ private function runUpdateHooks(UpdateOptions $options, string $root) : self { - $this->io()->note('Running update hooks ...'); + $this->style->note('Running update hooks ...'); $schemaFile = sprintf('%s/.platform/schema', $root); $results = $this->updateHookManager->run($schemaFile, $options); /** @var \DrupalTools\Update\UpdateResult $result */ foreach ($results as $result) { - $this->io() + $this->style ->writeln(array_map(fn (mixed $message) => $message, $result->messages )); @@ -231,7 +213,7 @@ private function runUpdateHooks(UpdateOptions $options, string $root) : self { * The self. */ private function updateDefaultFiles(UpdateOptions $options) : self { - $this->io()->note('Checking files ...'); + $this->style->note('Checking files ...'); $this->fileManager ->updateFiles($options, [ 'public/sites/default/azure.settings.php', @@ -307,7 +289,7 @@ public function updatePlatform(array $options = [ 'run-migrations' => TRUE, ]) : int { if (empty($options['root'])) { - $this->io()->error('No root found.'); + $this->style->error('No root found.'); } $root = $this->gitRoot($options['root']); $options = $this->parseOptions($options, $root); @@ -319,7 +301,7 @@ public function updatePlatform(array $options = [ chdir($root); if ($this->needsUpdate($options)) { - $this->io()->note('drupal/helfi_drupal_tools is out of date. Please run "composer update drupal/helfi_drupal_tools" to update it and re-run this command.'); + $this->style->note('drupal/helfi_drupal_tools is out of date. Please run "composer update drupal/helfi_drupal_tools" to update it and re-run this command.'); return DrushCommands::EXIT_SUCCESS; } diff --git a/tests/src/Kernel/CheckPackageVersionsCommandsTest.php b/tests/src/Kernel/CheckPackageVersionsCommandsTest.php index 4889c44..6782d92 100644 --- a/tests/src/Kernel/CheckPackageVersionsCommandsTest.php +++ b/tests/src/Kernel/CheckPackageVersionsCommandsTest.php @@ -8,10 +8,11 @@ use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait; use DrupalTools\Drush\Commands\PackageScannerDrushCommands; use Drush\Commands\DrushCommands; +use Drush\Drush; use GuzzleHttp\Psr7\Response; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; -use Symfony\Component\Console\Style\StyleInterface; +use Symfony\Component\Console\Style\OutputStyle; /** * Tests Package version checker commands. @@ -33,7 +34,7 @@ final class CheckPackageVersionsCommandsTest extends KernelTestBase { */ public function testInvalidComposerFile() : void { $this->expectException(\RuntimeException::class); - PackageScannerDrushCommands::create($this->container)->checkVersions('nonexistent.lock'); + PackageScannerDrushCommands::create($this->container, Drush::getContainer())->checkVersions('nonexistent.lock'); } /** @@ -62,7 +63,7 @@ public function testVersionCheck() : void { ], ])), ]); - $io = $this->prophesize(StyleInterface::class); + $io = $this->prophesize(OutputStyle::class); $io->table(Argument::any(), [ [ 'name' => 'drupal/helfi_api_base',