Files
FreshRSS/cli/i18n/I18nUsageValidator.php
Luc SANCHEZ 7f9594b8c7 fix many "Only booleans are allowed in an if condition" (#5501)
* fix many "Only booleans are allowed in an if condition"

* Update cli/create-user.php

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>

* Update cli/i18n/I18nUsageValidator.php

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>

* Fix several regressions and other minor things

* Fix another regression

* Update lib/http-conditional.php

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>

---------

Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
2023-07-07 21:53:17 +02:00

60 lines
1.5 KiB
PHP

<?php
require_once __DIR__ . '/I18nValidatorInterface.php';
class I18nUsageValidator implements I18nValidatorInterface {
/** @var array<string> */
private $code;
/** @var array<string,array<string,string>> */
private $reference;
/** @var int */
private $totalEntries = 0;
/** @var int */
private $failedEntries = 0;
/** @var string */
private $result = '';
/**
* @param array<string,array<string,string>> $reference
* @param array<string> $code
*/
public function __construct(array $reference, array $code) {
$this->code = $code;
$this->reference = $reference;
}
public function displayReport(): string {
if ($this->failedEntries > $this->totalEntries) {
throw new \RuntimeException('The number of unused strings cannot be higher than the number of strings');
}
if ($this->totalEntries === 0) {
return 'There is no data.' . PHP_EOL;
}
return sprintf('%5.1f%% of translation keys are unused.', $this->failedEntries / $this->totalEntries * 100) . PHP_EOL;
}
public function displayResult(): string {
return $this->result;
}
public function validate(): bool {
foreach ($this->reference as $file => $data) {
foreach ($data as $key => $value) {
$this->totalEntries++;
if (preg_match('/\._$/', $key) === 1 && in_array(preg_replace('/\._$/', '', $key), $this->code, true)) {
continue;
}
if (!in_array($key, $this->code, true)) {
$this->result .= sprintf('Unused key %s - %s', $key, $value) . PHP_EOL;
$this->failedEntries++;
continue;
}
}
}
return 0 === $this->failedEntries;
}
}