Skip to content

Commit

Permalink
Fix phpstan errors, converted UpdateDrushCommands to use custom IO
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Feb 27, 2024
1 parent 7aa942f commit 6874a5a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 56 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ parameters:
paths:
- ./
excludePaths:
- sut
- vendor
- src/Drush/Commands/HelperDrushCommands.php
- src/Drush/Commands/OpenShiftDrushCommands.php
Expand Down
9 changes: 4 additions & 5 deletions src/Drush/Commands/PackageScannerDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -30,15 +29,15 @@ final class PackageScannerDrushCommands extends DrushCommands {
*/
public function __construct(

Check failure on line 30 in src/Drush/Commands/PackageScannerDrushCommands.php

View workflow job for this annotation

GitHub Actions / tests (8.1)

PHPDoc tag @param for parameter $style with type Symfony\Component\Console\Style\StyleInterface is not subtype of native type Symfony\Component\Console\Style\OutputStyle.

Check failure on line 30 in src/Drush/Commands/PackageScannerDrushCommands.php

View workflow job for this annotation

GitHub Actions / tests (8.2)

PHPDoc tag @param for parameter $style with type Symfony\Component\Console\Style\StyleInterface is not subtype of native type Symfony\Component\Console\Style\OutputStyle.
private readonly VersionChecker $versionChecker,
private readonly StyleInterface $style,
private readonly OutputStyle $style,

Check failure on line 32 in src/Drush/Commands/PackageScannerDrushCommands.php

View workflow job for this annotation

GitHub Actions / tests (8.1)

PHPDoc type for property DrupalTools\Drush\Commands\PackageScannerDrushCommands::$style with type Symfony\Component\Console\Style\StyleInterface is not subtype of native type Symfony\Component\Console\Style\OutputStyle.

Check failure on line 32 in src/Drush/Commands/PackageScannerDrushCommands.php

View workflow job for this annotation

GitHub Actions / tests (8.2)

PHPDoc type for property DrupalTools\Drush\Commands\PackageScannerDrushCommands::$style with type Symfony\Component\Console\Style\StyleInterface is not subtype of native type Symfony\Component\Console\Style\OutputStyle.
) {
parent::__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'))
Expand Down
78 changes: 30 additions & 48 deletions src/Drush/Commands/UpdateDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')),
);
}

Expand Down Expand Up @@ -148,15 +130,15 @@ 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')) {
$this->processManager()->process([
'make',
'self-update',
])->run(function (string $type, ?string $output) : void {
$this->io()->write($output);
$this->style->write($output);
});
}
return $this;
Expand Down Expand Up @@ -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
));
Expand All @@ -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',
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/src/Kernel/CheckPackageVersionsCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 6874a5a

Please sign in to comment.