Skip to content

Commit

Permalink
Improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Feb 28, 2024
1 parent 02d3ac2 commit 4178478
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
21 changes: 1 addition & 20 deletions psalm-baseline.xml
Expand Up @@ -4,38 +4,19 @@
<MixedArgument>
<code><![CDATA[$baseLanguageFile]]></code>
<code><![CDATA[$currentLocaleDirectoryPath]]></code>
<code><![CDATA[$secondArray[$key]]]></code>
<code><![CDATA[$secondLanguageFile]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$baseLanguageFile]]></code>
<code><![CDATA[$currentLocaleDirectoryPath]]></code>
<code><![CDATA[$secondLanguageFile]]></code>
<code><![CDATA[$value]]></code>
</MixedAssignment>
<MixedReturnTypeCoercion>
<code><![CDATA[$outputDiff]]></code>
<code><![CDATA[list<string>]]></code>
</MixedReturnTypeCoercion>
<NonInvariantDocblockPropertyType>
<code><![CDATA[$description]]></code>
</NonInvariantDocblockPropertyType>
<PossiblyInvalidArgument>
<code><![CDATA[$pathToLocates]]></code>
<code><![CDATA[$this->option('dir')]]></code>
<code><![CDATA[$this->option('dir')]]></code>
<code><![CDATA[$this->option('dir')]]></code>
<code><![CDATA[$secondArray[$key]]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$pathToLocates]]></code>
<code><![CDATA[$this->option('dir')]]></code>
<code><![CDATA[$this->option('dir')]]></code>
<code><![CDATA[$this->option('dir')]]></code>
<code><![CDATA[$this->option('dir')]]></code>
</PossiblyInvalidCast>
<PossiblyNullOperand>
<code><![CDATA[$pathToLocates]]></code>
</PossiblyNullOperand>
<PropertyNotSetInConstructor>
<code><![CDATA[FindMissingTranslations]]></code>
<code><![CDATA[FindMissingTranslations]]></code>
Expand Down
35 changes: 18 additions & 17 deletions src/Commands/FindMissingTranslations.php
Expand Up @@ -20,29 +20,28 @@ class FindMissingTranslations extends Command
* @var string
*/
protected $signature = 'translations:missing
{--dir= : Relative path of lang directory, e.g. "/resources/lang", a directory that contains all supported locales}
{--base= : Base locale, e.g. "en". All other locales are compared to this locale}';
{--dir= : Relative path of lang directory, e.g. "/resources/lang", a directory that contains all supported locales}
{--base= : Base locale, e.g. "en". All other locales are compared to this locale}';

/**
* The console command description.
* @var string
*/
protected $description = 'Helps developers to finding words which are not translated, by comparing one base locale to others.';

private int $exitCode = 0;
private int $exitCode = self::SUCCESS;

/** @inheritDoc */
public function handle(): int
{
if ($this->option('dir') === null) {
$pathToLocates = resource_path(self::DEFAULT_LANG_DIRNAME);
} elseif (File::isDirectory($this->option('dir'))) {
$pathToLocates = $this->option('dir');
} elseif (File::isDirectory(base_path($this->option('dir')))) {
$pathToLocates = base_path($this->option('dir'));
} else {
throw new DirectoryNotFoundException("Specified resource directory {$this->option('dir')} does not exist.");
}
/** @var string|null $directoryOption */
$directoryOption = $this->option('dir');

$pathToLocates = match(true) {
$directoryOption === null => resource_path(self::DEFAULT_LANG_DIRNAME),
File::isDirectory($directoryOption) => $directoryOption,
File::isDirectory(base_path($directoryOption)) => base_path($directoryOption),
default => throw new DirectoryNotFoundException("Specified resource directory {$directoryOption} does not exist.")
};

$baseLocale = $this->option('base') ?: config('app.locale');
assert(is_string($baseLocale), 'Invalid base locale');
Expand Down Expand Up @@ -72,8 +71,8 @@ public function handle(): int
}

/**
* @param list<string> $baseLanguageFiles
* @param list<string> $languageFiles
* @param list<string> $baseLanguageFiles Filenames
* @param list<string> $languageFiles Filenames
*/
private function compareLanguages(string $baseLanguagePath, array $baseLanguageFiles, string $languagePath, array $languageFiles, string $languageName): void
{
Expand All @@ -91,7 +90,7 @@ private function compareLanguages(string $baseLanguagePath, array $baseLanguageF
$missingKeys = $this->arrayDiffRecursive($baseLanguageFile, $secondLanguageFile);

if (count($missingKeys) > 0) {
$this->exitCode = 1;
$this->exitCode = self::FAILURE;

$this->error("Found missing translations in /{$languageName}/{$languageFile}:", 'q');

Expand All @@ -107,6 +106,8 @@ private function compareLanguages(string $baseLanguagePath, array $baseLanguageF

/**
* Compare array keys recursively
* @param array<string, string|array<string, string>> $firstArray
* @param array<string, string|array<string, string>> $secondArray
* @return list<string>
*/
private function arrayDiffRecursive(array $firstArray, array $secondArray): array
Expand All @@ -133,7 +134,7 @@ private function arrayDiffRecursive(array $firstArray, array $secondArray): arra

/**
* Get filenames of directory
* @return list<string>
* @return list<string> Filenames in a given directory
*/
private function getFilenames(string $directory): array
{
Expand Down

0 comments on commit 4178478

Please sign in to comment.