Fix pipeline

This commit is contained in:
2024-11-26 14:42:04 +01:00
parent e339623e2d
commit 8f34c841f3
23 changed files with 71 additions and 67 deletions

View File

@@ -17,15 +17,14 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Console\Helper\Table;
class DetectTranslationDuplicatesCommand extends Command
{
protected static $defaultName = 'chill:detect-duplicate-translations';
public function __construct(private readonly TranslatorInterface $translator, private readonly KernelInterface $kernel)
public function __construct(private readonly Translator $translator, private readonly KernelInterface $kernel)
{
parent::__construct();
}
@@ -50,8 +49,8 @@ class DetectTranslationDuplicatesCommand extends Command
$bundlePath = $bundle->getPath();
$translationDir = $this->getTranslationDirectory($bundle->getName(), $bundlePath);
if ($translationDir && is_dir($translationDir)) {
foreach (glob($translationDir . '/*.yaml') as $file) {
if (null !== $translationDir && is_dir($translationDir)) {
foreach (glob($translationDir.'/*.yaml') as $file) {
$this->translator->addResource('yaml', $file, $locale);
}
}
@@ -64,45 +63,45 @@ class DetectTranslationDuplicatesCommand extends Command
// Iterate through each domain in the catalogue
foreach ($catalogue->all() as $domain => $translations) {
foreach ($translations as $key => $value) {
if ($this->isExcludedNamespace("$domain.$key", $excludedNamespaces)) {
if ($this->isExcludedNamespace("{$domain}.{$key}", $excludedNamespaces)) {
continue;
}
if (is_array($value)) {
$this->flattenTranslation($value, "$domain.$key", $allTranslations);
$this->flattenTranslation($value, "{$domain}.{$key}", $allTranslations);
} else {
if (!isset($allTranslations[$value])) {
$allTranslations[$value] = [];
}
$allTranslations[$value][] = "$domain.$key";
$allTranslations[$value][] = "{$domain}.{$key}";
}
}
}
// Detect values that appear in more than one key
$duplicates = array_filter($allTranslations, function ($keys) {
return count($keys) > 1;
});
$duplicates = array_filter($allTranslations, fn($keys) => count($keys) > 1);
$duplicatesHash = $this->generateDuplicatesHash($duplicates);
if ($expectedHash) {
if ('' !== $expectedHash) {
if ($duplicatesHash === $expectedHash) {
$output->writeln('<info>Translations are consistent with the expected hash.</info>');
$output->writeln("<info>Current duplicate hash: $duplicatesHash</info>");
return Command::SUCCESS;
} else {
$output->writeln('<error>Translation hash mismatch! Potential duplicate added.</error>');
$this->renderDuplicatesTable($output, $duplicates, $locale);
$output->writeln("<info>Current duplicate hash: {$duplicatesHash}</info>");
$output->writeln("<info>Current duplicate hash: $duplicatesHash</info>");
return Command::FAILURE;
return Command::SUCCESS;
}
$output->writeln('<error>Translation hash mismatch! Potential duplicate added.</error>');
$this->renderDuplicatesTable($output, $duplicates, $locale);
$output->writeln("<info>Current duplicate hash: {$duplicatesHash}</info>");
return Command::FAILURE;
}
$this->renderDuplicatesTable($output, $duplicates, $locale);
$output->writeln("<info>Current duplicate hash: $duplicatesHash</info>");
$output->writeln("<info>Current duplicate hash: {$duplicatesHash}</info>");
return Command::SUCCESS;
}
@@ -110,7 +109,7 @@ class DetectTranslationDuplicatesCommand extends Command
private function flattenTranslation(array $translations, string $prefix, array &$allTranslations): void
{
foreach ($translations as $key => $value) {
$fullKey = "$prefix.$key";
$fullKey = "{$prefix}.{$key}";
if (is_array($value)) {
$this->flattenTranslation($value, $fullKey, $allTranslations);
} else {
@@ -124,10 +123,10 @@ class DetectTranslationDuplicatesCommand extends Command
private function getTranslationDirectory(string $bundleName, string $bundlePath): ?string
{
$translationDir = $bundlePath . '/translations';
$translationDir = $bundlePath.'/translations';
if ($bundleName === 'ChillAsideActivityBundle') {
$translationDir = $bundlePath . '/src/translations';
if ('ChillAsideActivityBundle' === $bundleName) {
$translationDir = $bundlePath.'/src/translations';
}
return is_dir($translationDir) ? $translationDir : null;
@@ -141,10 +140,11 @@ class DetectTranslationDuplicatesCommand extends Command
private function isExcludedNamespace(string $key, array $excludedNamespaces): bool
{
foreach ($excludedNamespaces as $namespace) {
if (str_starts_with($key, $namespace)) {
if (str_starts_with($key, (string) $namespace)) {
return true;
}
}
return false;
}
@@ -160,12 +160,13 @@ class DetectTranslationDuplicatesCommand extends Command
private function renderDuplicatesTable(OutputInterface $output, array $duplicates, string $locale): void
{
if (empty($duplicates)) {
$output->writeln("<info>No duplicate translations found for locale '$locale'.</info>");
if ([] === $duplicates) {
$output->writeln("<info>No duplicate translations found for locale '{$locale}'.</info>");
return;
}
$output->writeln("<comment>Duplicate translations found for locale '$locale':</comment>");
$output->writeln("<comment>Duplicate translations found for locale '{$locale}':</comment>");
$table = new Table($output);
$table->setHeaders(['Translation', 'Used in Keys']);