Skip to content

Commit

Permalink
Add rector rule for constructor injection
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Nov 27, 2024
1 parent 1bfcfe5 commit 3a1b932
Show file tree
Hide file tree
Showing 177 changed files with 1,642 additions and 818 deletions.
14 changes: 5 additions & 9 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

declare(strict_types=1);

use Platformsh\Cli\InjectCommandServicesRector;
use Rector\Config\RectorConfig;
use Rector\Symfony\Set\SymfonySetList;
use Rector\Symfony\Symfony61\Rector\Class_\CommandConfigureToAttributeRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withSets([
SymfonySetList::SYMFONY_71,
__DIR__ . '/src/Command',
])
->withRules([
CommandConfigureToAttributeRector::class,
]);
InjectCommandServicesRector::class,
])
->withImportNames(importShortClasses: false, removeUnusedImports: true);
7 changes: 5 additions & 2 deletions src/Command/Activity/ActivityCancelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#[AsCommand(name: 'activity:cancel', description: 'Cancel an activity')]
class ActivityCancelCommand extends ActivityCommandBase
{
public function __construct(private readonly ActivityLoader $activityLoader)
{
parent::__construct();
}
/**
* {@inheritdoc}
*/
Expand All @@ -43,8 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->validateInput($input, $input->getOption('all') || $input->getArgument('id'));

/** @var ActivityLoader $loader */
$loader = $this->getService('activity_loader');
$loader = $this->activityLoader;

$executable = $this->config()->get('application.executable');

Expand Down
13 changes: 7 additions & 6 deletions src/Command/Activity/ActivityGetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#[AsCommand(name: 'activity:get', description: 'View detailed information on a single activity')]
class ActivityGetCommand extends ActivityCommandBase
{
public function __construct(private readonly ActivityLoader $activityLoader, private readonly PropertyFormatter $propertyFormatter, private readonly Table $table)
{
parent::__construct();
}
/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -52,8 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->validateInput($input, $input->getOption('all') || $input->getArgument('id'));

/** @var ActivityLoader $loader */
$loader = $this->getService('activity_loader');
$loader = $this->activityLoader;

if ($this->hasSelectedEnvironment() && !$input->getOption('all')) {
$apiResource = $this->getSelectedEnvironment();
Expand Down Expand Up @@ -84,10 +87,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

/** @var Table $table */
$table = $this->getService('table');
/** @var PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$table = $this->table;
$formatter = $this->propertyFormatter;

$properties = $activity->getProperties();

Expand Down
15 changes: 9 additions & 6 deletions src/Command/Activity/ActivityListCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Platformsh\Cli\Command\Activity;

use Platformsh\Cli\Service\ActivityLoader;
use Platformsh\Cli\Console\AdaptiveTableCell;
use Platformsh\Cli\Console\ArrayArgument;
use Platformsh\Cli\Service\ActivityMonitor;
Expand Down Expand Up @@ -31,6 +32,11 @@ class ActivityListCommand extends ActivityCommandBase
];
private $defaultColumns = ['id', 'created', 'description', 'progress', 'state', 'result'];

public function __construct(private readonly ActivityLoader $activityLoader, private readonly PropertyFormatter $propertyFormatter, private readonly Table $table)
{
parent::__construct();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -86,19 +92,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$apiResource = $project;
}

/** @var \Platformsh\Cli\Service\ActivityLoader $loader */
$loader = $this->getService('activity_loader');
$loader = $this->activityLoader;
$activities = $loader->loadFromInput($apiResource, $input);
if ($activities === []) {
$this->stdErr->writeln('No activities found');

return 1;
}

/** @var \Platformsh\Cli\Service\Table $table */
$table = $this->getService('table');
/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$table = $this->table;
$formatter = $this->propertyFormatter;

$defaultColumns = $this->defaultColumns;

Expand Down
14 changes: 8 additions & 6 deletions src/Command/Activity/ActivityLogCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Platformsh\Cli\Command\Activity;

use Platformsh\Cli\Service\ActivityLoader;
use Platformsh\Cli\Console\ArrayArgument;
use Platformsh\Cli\Service\ActivityMonitor;
use Platformsh\Cli\Service\PropertyFormatter;
Expand All @@ -14,6 +15,10 @@
#[AsCommand(name: 'activity:log', description: 'Display the log for an activity')]
class ActivityLogCommand extends ActivityCommandBase
{
public function __construct(private readonly ActivityLoader $activityLoader, private readonly ActivityMonitor $activityMonitor, private readonly PropertyFormatter $propertyFormatter)
{
parent::__construct();
}
/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -57,8 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->validateInput($input, $input->getOption('all') || $input->getArgument('id'));

/** @var \Platformsh\Cli\Service\ActivityLoader $loader */
$loader = $this->getService('activity_loader');
$loader = $this->activityLoader;

if ($this->hasSelectedEnvironment() && !$input->getOption('all')) {
$apiResource = $this->getSelectedEnvironment();
Expand Down Expand Up @@ -89,8 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$formatter = $this->propertyFormatter;

$this->stdErr->writeln([
sprintf('<info>Activity ID: </info>%s', $activity->id),
Expand All @@ -109,8 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$timestamps = $this->config()->getWithDefault('application.date_format', 'c');
}

/** @var ActivityMonitor $monitor */
$monitor = $this->getService('activity_monitor');
$monitor = $this->activityMonitor;
if ($refresh > 0 && !$this->runningViaMulti && !$activity->isComplete() && $activity->state !== Activity::STATE_CANCELLED) {
$monitor->waitAndLog($activity, $refresh, $timestamps, false, $output);

Expand Down
8 changes: 6 additions & 2 deletions src/Command/ApiCurlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class ApiCurlCommand extends CommandBase
{
protected $hiddenInList = true;

public function __construct(private readonly CurlCli $curlCli)
{
parent::__construct();
}

protected function configure()
{
CurlCli::configureInput($this->getDefinition());
Expand All @@ -24,8 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// (allowing for auto login).
$this->api();

/** @var CurlCli $curl */
$curl = $this->getService('curl_cli');
$curl = $this->curlCli;

return $curl->run($url, $input, $output);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Command/App/AppConfigGetCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Platformsh\Cli\Command\App;

use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Model\AppConfig;
use Platformsh\Cli\Model\Host\LocalHost;
Expand All @@ -12,6 +13,10 @@
#[AsCommand(name: 'app:config-get', description: 'View the configuration of an app')]
class AppConfigGetCommand extends CommandBase
{
public function __construct(private readonly PropertyFormatter $propertyFormatter)
{
parent::__construct();
}
/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -49,8 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->getConfig();
}

/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$formatter = $this->propertyFormatter;
$formatter->displayData($output, $appConfig->getNormalized(), $input->getOption('property'));
return 0;
}
Expand Down
16 changes: 10 additions & 6 deletions src/Command/App/AppListCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Platformsh\Cli\Command\App;

use Platformsh\Cli\Local\ApplicationFinder;
use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Service\Table;
use Platformsh\Client\Model\Deployment\EnvironmentDeployment;
Expand All @@ -15,6 +17,11 @@ class AppListCommand extends CommandBase
private $tableHeader = ['Name', 'Type', 'disk' => 'Disk', 'Size', 'path' => 'Path'];
private $defaultColumns = ['name', 'type'];

public function __construct(private readonly ApplicationFinder $applicationFinder, private readonly PropertyFormatter $propertyFormatter, private readonly Table $table)
{
parent::__construct();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -63,8 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$showLocalPath = false;
$localApps = [];
if (($projectRoot = $this->getProjectRoot()) && $this->selectedProjectIsCurrent() && $this->config()->has('service.app_config_file')) {
/** @var \Platformsh\Cli\Local\ApplicationFinder $finder */
$finder = $this->getService('app_finder');
$finder = $this->applicationFinder;
$localApps = $finder->findApplications($projectRoot);
$showLocalPath = true;
}
Expand All @@ -86,8 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$defaultColumns[] = 'path';
}

/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$formatter = $this->propertyFormatter;

$rows = [];
foreach ($apps as $app) {
Expand All @@ -98,8 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$rows[] = $row;
}

/** @var \Platformsh\Cli\Service\Table $table */
$table = $this->getService('table');
$table = $this->table;
if (!$table->formatIsMachineReadable()) {
$this->stdErr->writeln(sprintf(
'Applications on the project <info>%s</info>, environment <info>%s</info>:',
Expand Down
12 changes: 8 additions & 4 deletions src/Command/Auth/ApiTokenLoginCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Platformsh\Cli\Command\Auth;

use Platformsh\Cli\Service\QuestionHelper;
use Platformsh\Cli\Service\TokenConfig;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Utils;
use League\OAuth2\Client\Token\AccessToken;
Expand All @@ -15,6 +17,10 @@
class ApiTokenLoginCommand extends CommandBase
{

public function __construct(private readonly QuestionHelper $questionHelper, private readonly TokenConfig $tokenConfig)
{
parent::__construct();
}
protected function configure()
{
$service = $this->config()->get('service.name');
Expand Down Expand Up @@ -74,8 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $apiToken;
};

/** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */
$questionHelper = $this->getService('question_helper');
$questionHelper = $this->questionHelper;
$question = new Question("Please enter an API token:\n> ");
$question->setValidator($validator);
$question->setMaxAttempts(5);
Expand All @@ -96,8 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function saveTokens($apiToken, AccessToken $accessToken) {
$this->api()->logout();

/** @var \Platformsh\Cli\Service\TokenConfig $tokenConfig */
$tokenConfig = $this->getService('token_config');
$tokenConfig = $this->tokenConfig;
$tokenConfig->storage()->storeToken($apiToken);

$this->api()
Expand Down
11 changes: 7 additions & 4 deletions src/Command/Auth/AuthInfoCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Platformsh\Cli\Command\Auth;

use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Service\Table;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -13,6 +14,10 @@
#[AsCommand(name: 'auth:info', description: 'Display your account information')]
class AuthInfoCommand extends CommandBase
{
public function __construct(private readonly PropertyFormatter $propertyFormatter, private readonly Table $table)
{
parent::__construct();
}
protected function configure()
{
$this
Expand All @@ -28,8 +33,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');
$formatter = $this->propertyFormatter;

if ($input->getOption('no-auto-login') && !$this->api()->isLoggedIn()) {
$this->stdErr->writeln('Not logged in', OutputInterface::VERBOSITY_VERBOSE);
Expand Down Expand Up @@ -95,8 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$header[] = $property;
}
}
/** @var \Platformsh\Cli\Service\Table $table */
$table = $this->getService('table');
$table = $this->table;
$table->renderSimple($values, $header);

if (!$table->formatIsMachineReadable() && ($this->config()->getSessionId() !== 'default' || count($this->api()->listSessionIds()) > 1)) {
Expand Down
10 changes: 7 additions & 3 deletions src/Command/Auth/BrowserLoginCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Platformsh\Cli\Command\Auth;

use Platformsh\Cli\Service\QuestionHelper;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Request;
Expand All @@ -23,6 +24,10 @@
#[AsCommand(name: 'auth:browser-login', description: 'Log in to via a browser', aliases: ['login'])]
class BrowserLoginCommand extends CommandBase
{
public function __construct(private readonly Url $url)
{
parent::__construct();
}
protected function configure()
{
$applicationName = $this->config()->get('application.name');
Expand Down Expand Up @@ -79,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
));

if ($input->isInteractive()) {
/** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */
/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getService('question_helper');
if (!$questionHelper->confirm('Log in anyway?', false)) {
return 1;
Expand Down Expand Up @@ -175,8 +180,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

// Open the local server URL in a browser (or print the URL).
/** @var \Platformsh\Cli\Service\Url $urlService */
$urlService = $this->getService('url');
$urlService = $this->url;
if ($urlService->openUrl($localUrl, false)) {
$this->stdErr->writeln(sprintf('Opened URL: <info>%s</info>', $localUrl));
$this->stdErr->writeln('Please use the browser to log in.');
Expand Down
Loading

0 comments on commit 3a1b932

Please sign in to comment.