Skip to content

Commit

Permalink
phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
test committed Jul 19, 2023
1 parent b13f8e7 commit 3e49d2d
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 15 deletions.
25 changes: 13 additions & 12 deletions src/Commands/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Psr7\Request;
use StellarWP\Pup\App;
use StellarWP\Pup\Command\Command;
use StellarWP\Pup\I18nConfig;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -49,7 +50,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
if ( $results !== 0 ) {
$io->writeln( '<fg=red>Failed to download language files.</>' );
$io->writeln( '<fg=yellow>Config:</>' );
$io->writeln( json_encode( $i18n_config, JSON_PRETTY_PRINT ) );
$io->writeln( (string) json_encode( $i18n_config, JSON_PRETTY_PRINT ) );
return $results;
}
}
Expand All @@ -64,25 +65,25 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
/**
* Downloads language files.
*
* @param array<int, mixed> $i18n_config
* @param I18nConfig $i18n_config
*
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return int
*/
protected function download_language_files( array $i18n_config ): int {
protected function download_language_files( I18nConfig $i18n_config ): int {
$io = $this->getIO();
$options = (object) [
'domain_path' => $i18n_config['path'] ?? '',
'url' => $i18n_config['url'] ?? '',
'slug' => $i18n_config['slug'] ?? '',
'text_domain' => $i18n_config['textdomain'] ?? '',
'file_format' => $i18n_config['file_format'] ?? '',
'formats' => $i18n_config['formats'] ?? [],
'filter' => (object) $i18n_config['filter'] ?? [],
'domain_path' => $i18n_config->getPath(),
'url' => $i18n_config->getUrl(),
'slug' => $i18n_config->getSlug(),
'text_domain' => $i18n_config->getTextdomain(),
'file_format' => $i18n_config->getFileFormat(),
'formats' => $i18n_config->getFormats(),
'filter' => $i18n_config->getFilter(),
];

$io->writeln( "<fg=yellow>Fetching language files for {$options->text_domain} from {$options->url}</>" );
$io->writeln( "<fg=yellow>Fetching language files for {$options->text_domain} from {$options->url}</>" ); // @phpstan-ignore-line: Those are strings.

$client = new Client();

Expand All @@ -108,7 +109,7 @@ protected function download_language_files( array $i18n_config ): int {
}

// Skip any translation set that doest match our min translated.
if ( $options->filter->minimum_percentage > $translation->percent_translated ) {
if ( $options->filter['minimum_percentage'] > $translation->percent_translated ) {
continue;
}

Expand Down
19 changes: 16 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Config implements \JsonSerializable {
*/
protected $has_invalid_puprc = false;

/**
* @var array<int, I18nConfig>
*/
protected $i18n = [];

/**
* @var string
*/
Expand Down Expand Up @@ -267,9 +272,13 @@ protected function getDefaultConfig() : array {
/**
* Returns the i18n settings.
*
* @return array<int, mixed>
* @return array<int, I18nConfig>
*/
public function getI18n(): array {
if ( ! empty( $this->i18n ) ) {
return $this->i18n;
}

$defaults = $this->getI18nDefaults();
$i18n = $this->config->i18n ? (array) $this->config->i18n : [];

Expand Down Expand Up @@ -298,13 +307,17 @@ public function getI18n(): array {
}
}

return $i18n;
foreach ( $i18n as $i18n_config ) {
$this->i18n[] = new I18nConfig( $i18n_config );
}

return $this->i18n;
}

/**
* Returns the default i18n settings.
*
* @return array<int, mixed>
* @return array<string, string|int|array<int, string>>
*/
public function getI18nDefaults(): array {
return $this->config->i18n_defaults ? (array) $this->config->i18n_defaults : [];
Expand Down
126 changes: 126 additions & 0 deletions src/I18nConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace StellarWP\Pup;

class I18nConfig {
/**
* @var string
*/
protected $file_format = '';

/**
* @var string
*/
protected $path = '';

/**
* @var string
*/
protected $url = '';

/**
* @var string
*/
protected $slug = '';

/**
* @var string
*/
protected $textdomain = '';

/**
* @var array<int, string>
*/
protected $formats = [];

/**
* @var array<string, int|string>
*/
protected $filter = [];

/**
* @param array<string, int|string|array<mixed, int|string>> $args
*/
public function __construct( $args = [] ) {
if ( ! empty( $args['file_format'] ) && is_string( $args['file_format'] ) ) {
$this->file_format = $args['file_format'];
}

if ( ! empty( $args['path'] ) && is_string( $args['path'] ) ) {
$this->path = $args['path'];
}

if ( ! empty( $args['url'] ) && is_string( $args['url'] ) ) {
$this->url = $args['url'];
}

if ( ! empty( $args['slug'] ) && is_string( $args['slug'] ) ) {
$this->slug = $args['slug'];
}

if ( ! empty( $args['textdomain'] ) && is_string( $args['textdomain'] ) ) {
$this->textdomain = $args['textdomain'];
}

if ( ! empty( $args['formats'] ) && is_array( $args['formats'] ) ) {
foreach ( $args['formats'] as $format ) {
$this->formats[] = (string) $format;
}
}

if ( ! empty( $args['filter'] ) && is_array( $args['filter'] ) ) {
foreach ( $args['filter'] as $key => $filter ) {
$this->filter[ (string) $key ] = $filter;
}
}
}

/**
* @return string
*/
public function getFileFormat(): string {
return $this->file_format;
}

/**
* @return string
*/
public function getPath(): string {
return $this->path;
}

/**
* @return string
*/
public function getUrl(): string {
return $this->url;
}

/**
* @return string
*/
public function getSlug(): string {
return $this->slug;
}

/**
* @return string
*/
public function getTextdomain(): string {
return $this->textdomain;
}

/**
* @return array<int, string>
*/
public function getFormats(): array {
return $this->formats;
}

/**
* @return array<string, int|string>
*/
public function getFilter(): array {
return $this->filter;
}
}

0 comments on commit 3e49d2d

Please sign in to comment.