Skip to content

Commit

Permalink
Fix some phpstan level 7 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Dec 18, 2024
1 parent 5fcdb86 commit e525aca
Show file tree
Hide file tree
Showing 40 changed files with 145 additions and 73 deletions.
4 changes: 4 additions & 0 deletions src/Command/Activity/ActivityListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Platformsh\Cli\Service\ActivityMonitor;
use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Service\Table;
use Platformsh\Client\Model\Activity;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -183,6 +184,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

/**
* @param Activity[] $activities
*/
private function suggestExclusions(array $activities): void
{
$counts = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Command/App/AppConfigGetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$prefix = $this->config->getStr('service.env_prefix');
if (getenv($prefix . 'APPLICATION') && !LocalHost::conflictsWithCommandLineOptions($input, $prefix)) {
$this->io->debug('Reading application config from environment variable ' . $prefix . 'APPLICATION');
$decoded = json_decode(base64_decode(getenv($prefix . 'APPLICATION'), true), true);
$decoded = json_decode((string) base64_decode(getenv($prefix . 'APPLICATION'), true), true);
if (!is_array($decoded)) {
throw new \RuntimeException('Failed to decode: ' . $prefix . 'APPLICATION');
}
Expand Down
12 changes: 7 additions & 5 deletions src/Command/Auth/BrowserLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->stdErr->writeln('');

// Wait for the file to be filled with an OAuth2 authorization code.
/** @var array|null $response */
/** @var null|array{code: string, redirect_uri: string}|array{error: string, error_description: string, error_hint: string} $response */
$response = null;
$start = time();
while ($process->isRunning()) {
Expand Down Expand Up @@ -262,7 +262,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (empty($token['refresh_token'])) {
$this->stdErr->writeln('');
$clientId = $this->config->get('api.oauth2_client_id');
$clientId = $this->config->getStr('api.oauth2_client_id');
$this->stdErr->writeln([
'<options=bold;fg=yellow>Warning:</fg>',
'No refresh token is available. This will cause frequent login errors.',
Expand All @@ -275,7 +275,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

/**
* @param array $tokenData
* @param array<string, mixed> $tokenData
* @param SessionInterface $session
*/
private function saveAccessToken(array $tokenData, SessionInterface $session): void
Expand Down Expand Up @@ -306,11 +306,13 @@ private function createDocumentRoot(string $dir): void

/**
* Exchanges the authorization code for an access token.
*
* @return array<string, mixed>
*/
private function getAccessToken(string $authCode, string $codeVerifier, string $redirectUri): array
{
$client = new Client(['verify' => !$this->config->getWithDefault('api.skip_ssl', false)]);
$request = new Request('POST', $this->config->get('api.oauth2_token_url'), body: http_build_query([
$request = new Request('POST', $this->config->getStr('api.oauth2_token_url'), body: http_build_query([
'grant_type' => 'authorization_code',
'code' => $authCode,
'redirect_uri' => $redirectUri,
Expand All @@ -325,7 +327,7 @@ private function getAccessToken(string $authCode, string $codeVerifier, string $
'auth' => [$this->config->get('api.oauth2_client_id'), ''],
]);

return Utils::jsonDecode((string) $response->getBody(), true);
return (array) Utils::jsonDecode((string) $response->getBody(), true);
} catch (BadResponseException $e) {
throw ApiResponseException::create($request, $e->getResponse(), $e);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Command/Auth/VerifyPhoneNumberCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'phone_number' => $number,
],
]);
$sid = Utils::jsonDecode((string) $response->getBody(), true)['sid'];
/** @var array{sid: string} $data */
$data = (array) Utils::jsonDecode((string) $response->getBody(), true);
$sid = $data['sid'];

if ($channel === 'call') {
$this->stdErr->writeln('Calling the number <info>' . $number . '</info> with a verification code.');
Expand All @@ -86,17 +88,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->stdErr->writeln('');

$this->questionHelper->askInput('Please enter the verification code', null, [], function ($code) use ($httpClient, $sid , $myUser): void {
$this->questionHelper->askInput('Please enter the verification code', null, [], function ($code) use ($httpClient, $sid, $myUser): void {
if (!is_numeric($code)) {
throw new InvalidArgumentException('Invalid verification code');
}
try {
$httpClient->post('/users/' . rawurlencode($myUser->id) . '/phonenumber/' . rawurlencode((string) $sid), [
$httpClient->post('/users/' . rawurlencode($myUser->id) . '/phonenumber/' . rawurlencode($sid), [
'json' => ['code' => $code],
]);
} catch (BadResponseException $e) {
if (($response = $e->getResponse()) && $response->getStatusCode() === 400) {
$detail = Utils::jsonDecode((string) $response->getBody(), true);
$detail = (array) Utils::jsonDecode((string) $response->getBody(), true);
throw new InvalidArgumentException(isset($detail['error']) ? ucfirst((string) $detail['error']) : 'Invalid verification code');
}
throw $e;
Expand All @@ -105,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->io->debug('Refreshing phone verification status');
$response = $httpClient->post( '/me/verification?force_refresh=1');
$needsVerify = Utils::jsonDecode((string) $response->getBody(), true);
$needsVerify = (array) Utils::jsonDecode((string) $response->getBody(), true);
$this->stdErr->writeln('');

if ($needsVerify['type'] === 'phone') {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Backup/BackupCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->stdErr->writeln('The environment is not active.');
} else {
try {
if ($this->isUserAdmin($selection->getProject(), $selectedEnvironment, $this->api->getMyUserId())) {
if ($this->isUserAdmin($selection->getProject(), $selectedEnvironment, (string) $this->api->getMyUserId())) {
$this->stdErr->writeln('You must be an administrator to create a backup.');
}
} catch (\Exception $e) {
Expand Down
1 change: 1 addition & 0 deletions src/Command/Backup/BackupRestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#[AsCommand(name: 'backup:restore', description: 'Restore an environment backup')]
class BackupRestoreCommand extends CommandBase
{
/** @var string[] */
private array $validResourcesInitOptions = ['backup', 'parent', 'default', 'minimum'];

public function __construct(private readonly ActivityMonitor $activityMonitor, private readonly Api $api, private readonly Config $config, private readonly Io $io, private readonly PropertyFormatter $propertyFormatter, private readonly QuestionHelper $questionHelper, private readonly ResourcesUtil $resourcesUtil, private readonly Selector $selector)
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BlueGreen/BlueGreenConcludeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$httpClient = $this->api->getHttpClient();
$response = $httpClient->get($environment->getLink('#versions'));
$data = Utils::jsonDecode((string) $response->getBody(), true);
$data = (array) Utils::jsonDecode((string) $response->getBody(), true);
if (count($data) < 2) {
$this->stdErr->writeln(sprintf('Blue/green deployments are not enabled for the environment %s.', $this->api->getEnvironmentLabel($environment, 'error')));
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BlueGreen/BlueGreenDeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$httpClient = $this->api->getHttpClient();
$response = $httpClient->get($environment->getLink('#versions'));
$data = Utils::jsonDecode((string) $response->getBody(), true);
$data = (array) Utils::jsonDecode((string) $response->getBody(), true);
if (count($data) < 2) {
$this->stdErr->writeln(sprintf('Blue/green deployments are not enabled for the environment %s.', $this->api->getEnvironmentLabel($environment, 'error')));
$this->stdErr->writeln(sprintf('Enable blue/green first by running: <info>%s blue-green:enable</info>', $this->config->getStr('application.executable')));
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BlueGreen/BlueGreenEnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$httpClient = $this->api->getHttpClient();
$response = $httpClient->get($environment->getLink('#versions'));
$data = Utils::jsonDecode((string) $response->getBody(), true);
$data = (array) Utils::jsonDecode((string) $response->getBody(), true);
if (count($data) > 1) {
$this->stdErr->writeln(sprintf('Blue/green deployments are already enabled for the environment %s.', $this->api->getEnvironmentLabel($environment)));
$this->stdErr->writeln(sprintf('List versions by running: <info>%s versions</info>', $this->config->getStr('application.executable')));
Expand Down
17 changes: 14 additions & 3 deletions src/Command/BotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$signature = '';
}

$files = scandir($dir);
if (!$files) {
throw new \RuntimeException('Failed to read directory: ' . $dir);
}

$frames = [];
foreach (scandir($dir) as $filename) {
foreach ($files as $filename) {
if ($filename[0] !== '.') {
$frames[] = file_get_contents($dir . '/' . $filename);
$frames[] = (string) file_get_contents($dir . '/' . $filename);
}
}

Expand Down Expand Up @@ -75,6 +80,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

/**
* @param array<string|\Stringable> $frames
* @param string $signature
* @return string[]
*/
private function addSignature(array $frames, string $signature): array
{
$indent = ' ';
Expand All @@ -87,7 +97,8 @@ private function addSignature(array $frames, string $signature): array
}

/**
* @return non-falsy-string[]
* @param string[] $frames
* @return string[]
*/
private function addColor(array $frames): array
{
Expand Down
5 changes: 5 additions & 0 deletions src/Command/Certificate/CertificateListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

/**
* @param Certificate[] $certs
* @param array<string, mixed> $filters
* @return void
*/
protected function filterCerts(array &$certs, array $filters): void
{
foreach ($filters as $filter => $value) {
Expand Down
10 changes: 7 additions & 3 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ abstract class CommandBase extends Command implements MultiAwareInterface
protected bool $runningViaMulti = false;

/**
* @var string[]
* @see self::setHiddenAliases()
*/
private array $hiddenAliases = [];

/**
* The command synopsis.
* @var array<string, string>
*/
private array $synopsis = [];

Expand Down Expand Up @@ -101,8 +103,10 @@ protected function interact(InputInterface $input, OutputInterface $output): voi

/**
* Adds a hidden command option.
*
* @param int-mask-of<InputOption::*>|null $mode
*/
protected function addHiddenOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null): static
protected function addHiddenOption(string $name, string|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null): static
{
$this->getDefinition()->addOption(new HiddenInputOption($name, $shortcut, $mode, $description, $default));

Expand All @@ -114,7 +118,7 @@ protected function addHiddenOption(string $name, string|array|null $shortcut = n
*
* @see parent::setAliases()
*
* @param array $hiddenAliases
* @param string[] $hiddenAliases
*
* @return static
*/
Expand All @@ -129,7 +133,7 @@ protected function setHiddenAliases(array $hiddenAliases): static
/**
* Get aliases that should be visible in help.
*
* @return array
* @return string[]
*/
public function getVisibleAliases(): array
{
Expand Down
6 changes: 5 additions & 1 deletion src/Command/Commit/CommitListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ private function loadCommitList(Environment $environment, Commit $startCommit, i
count($currentCommit->parents) && count($commits) < $limit;) {
foreach (array_reverse($currentCommit->parents) as $parentSha) {
if (!isset($commits[$parentSha])) {
$commits[$parentSha] = $this->gitDataApi->getCommit($environment, $parentSha);
$commit = $this->gitDataApi->getCommit($environment, $parentSha);
if (!$commit) {
throw new \RuntimeException(sprintf('Commit not found: %s', $parentSha));
}
$commits[$parentSha] = $commit;
}
$currentCommit = $commits[$parentSha];
$progress->advance();
Expand Down
3 changes: 3 additions & 0 deletions src/Command/Db/DbDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/**
* Generates the default dump filename.
*
* @param string[] $includedTables
* @param string[] $excludedTables
*/
private function getDefaultFilename(
?Environment $environment = null,
Expand Down
27 changes: 19 additions & 8 deletions src/Command/Db/DbSizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/**
* Returns a list of cleanup queries for a list of tables.
*
* @param array $rows
* @param string[] $rows
*
* @see DbSizeCommand::checkInnoDbTablesInNeedOfOptimizing()
*
* @return array
* @return string[]
*/
private function getCleanupQueries(array $rows): array {
return array_filter(
Expand All @@ -138,6 +138,8 @@ private function getCleanupQueries(array $rows): array {

/**
* Displays a list of InnoDB tables that can be usefully cleaned up.
*
* @param array<string, mixed> $database
*/
private function checkInnoDbTablesInNeedOfOptimizing(HostInterface $host, array $database, InputInterface $input): void {
$tablesNeedingCleanup = $host->runCommand($this->getMysqlCommand($database), true, true, $this->mysqlTablesInNeedOfOptimizing());
Expand Down Expand Up @@ -177,7 +179,7 @@ private function checkInnoDbTablesInNeedOfOptimizing(HostInterface $host, array
* Shows a warning about schemas not accessible through this relationship.
*
* @param Service $service
* @param array $database
* @param array<string, mixed> $database
*
* @return void
*/
Expand Down Expand Up @@ -245,7 +247,7 @@ private function psqlQuery(): string
/**
* Returns the psql CLI client command.
*
* @param array $database
* @param array<string, mixed> $database
*
* @return string
*/
Expand All @@ -258,6 +260,10 @@ private function getPsqlCommand(array $database): string {
);
}

/**
* @param array<string, mixed> $database
* @return string
*/
private function getMongoDbCommand(array $database): string {
$dbUrl = $this->relationships->getDbCommandArgs('mongo', $database);

Expand All @@ -272,7 +278,7 @@ private function getMongoDbCommand(array $database): string {
/**
* Returns the mysql CLI client command.
*
* @param array $database
* @param array<string, mixed> $database
*
* @return string
*/
Expand Down Expand Up @@ -341,7 +347,7 @@ private function mysqlTablesInNeedOfOptimizing(): string {
* Estimates usage of a database.
*
* @param HostInterface $host
* @param array $database
* @param array<string, mixed> $database
*
* @return float Estimated usage in bytes.
*/
Expand All @@ -357,14 +363,19 @@ private function getEstimatedUsage(HostInterface $host, array $database): float
* Estimates usage of a PostgreSQL database.
*
* @param HostInterface $host
* @param array $database
* @param array<string, mixed> $database
*
* @return float Estimated usage in bytes
*/
private function getPgSqlUsage(HostInterface $host, array $database): float {
return (float) $host->runCommand($this->getPsqlCommand($database), true, true, $this->psqlQuery());
}

/**
* @param HostInterface $host
* @param array<string, mixed> $database
* @return float
*/
private function getMongoDbUsage(HostInterface $host, array $database): float {
return (float) $host->runCommand($this->getMongoDbCommand($database));
}
Expand All @@ -373,7 +384,7 @@ private function getMongoDbUsage(HostInterface $host, array $database): float {
* Estimates usage of a MySQL database.
*
* @param HostInterface $host
* @param array $database
* @param array<string, mixed> $database
*
* @return float Estimated usage in bytes
*/
Expand Down
Loading

0 comments on commit e525aca

Please sign in to comment.