mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-07-17 18:43:05 -04:00
PHPstan level 6 for I18nFile.php (#5291)
* PHPstan level 6 for I18nFile.php * Minor syntax * PHPstan level 6 for I18nFiles * PHPstan level 6 for I18nFiles * PHPstan level 6 for I18n Files * PHPstan level 6 for I18n Files * Fix several type errors --------- Co-authored-by: Luc <sanchezluc+freshrss@gmail.com> Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
class I18nData {
|
||||
|
||||
const REFERENCE_LANGUAGE = 'en';
|
||||
public const REFERENCE_LANGUAGE = 'en';
|
||||
|
||||
private $data = [];
|
||||
/** @var array<string,array<string,array<string,I18nValue>>> */
|
||||
private $data;
|
||||
|
||||
/** @param array<string,array<string,array<string,I18nValue>>> $data */
|
||||
public function __construct(array $data) {
|
||||
$this->data = $data;
|
||||
|
||||
@@ -14,11 +16,14 @@ class I18nData {
|
||||
$this->processValueStates();
|
||||
}
|
||||
|
||||
public function getData() {
|
||||
/**
|
||||
* @return array<string,array<string,array<string,I18nValue>>>
|
||||
*/
|
||||
public function getData(): array {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
private function addMissingKeysFromReference() {
|
||||
private function addMissingKeysFromReference(): void {
|
||||
$reference = $this->getReferenceLanguage();
|
||||
$languages = $this->getNonReferenceLanguages();
|
||||
|
||||
@@ -37,7 +42,7 @@ class I18nData {
|
||||
}
|
||||
}
|
||||
|
||||
private function removeExtraKeysFromOtherLanguages() {
|
||||
private function removeExtraKeysFromOtherLanguages(): void {
|
||||
$reference = $this->getReferenceLanguage();
|
||||
foreach ($this->getNonReferenceLanguages() as $language) {
|
||||
foreach ($this->getLanguage($language) as $file => $values) {
|
||||
@@ -50,7 +55,7 @@ class I18nData {
|
||||
}
|
||||
}
|
||||
|
||||
private function processValueStates() {
|
||||
private function processValueStates(): void {
|
||||
$reference = $this->getReferenceLanguage();
|
||||
$languages = $this->getNonReferenceLanguages();
|
||||
|
||||
@@ -73,10 +78,9 @@ class I18nData {
|
||||
|
||||
/**
|
||||
* Return the available languages
|
||||
*
|
||||
* @return array
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getAvailableLanguages() {
|
||||
public function getAvailableLanguages(): array {
|
||||
$languages = array_keys($this->data);
|
||||
sort($languages);
|
||||
|
||||
@@ -85,23 +89,19 @@ class I18nData {
|
||||
|
||||
/**
|
||||
* Return all available languages without the reference language
|
||||
*
|
||||
* @return array
|
||||
* @return array<string>
|
||||
*/
|
||||
private function getNonReferenceLanguages() {
|
||||
return array_filter(array_keys($this->data), function ($value) {
|
||||
private function getNonReferenceLanguages(): array {
|
||||
return array_filter(array_keys($this->data), static function ($value) {
|
||||
return static::REFERENCE_LANGUAGE !== $value;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new language. It’s a copy of the reference language.
|
||||
*
|
||||
* @param string $language
|
||||
* @param string $reference
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addLanguage($language, $reference = null) {
|
||||
public function addLanguage(string $language, string $reference = null): void {
|
||||
if (array_key_exists($language, $this->data)) {
|
||||
throw new Exception('The selected language already exist.');
|
||||
}
|
||||
@@ -113,11 +113,8 @@ class I18nData {
|
||||
|
||||
/**
|
||||
* Check if the key is known.
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function isKnown($key) {
|
||||
public function isKnown(string $key): bool {
|
||||
return array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) &&
|
||||
array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
|
||||
}
|
||||
@@ -128,18 +125,18 @@ class I18nData {
|
||||
* is separated into sections. The parent of a section is the concatenation of
|
||||
* all sections before the selected key. For instance, if the key is 'a.b.c.d.e',
|
||||
* the parent key is 'a.b.c.d'.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getParentKey($key) {
|
||||
private function getParentKey(string $key): string {
|
||||
return substr($key, 0, strrpos($key, '.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the siblings for a specified key.
|
||||
* To get the siblings, we need to find all matches with the parent.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
private function getSiblings($key) {
|
||||
private function getSiblings(string $key): array {
|
||||
if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) {
|
||||
return [];
|
||||
}
|
||||
@@ -147,7 +144,7 @@ class I18nData {
|
||||
$keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]);
|
||||
$parent = $this->getParentKey($key);
|
||||
|
||||
return array_values(array_filter($keys, function ($element) use ($parent) {
|
||||
return array_values(array_filter($keys, static function ($element) use ($parent) {
|
||||
return false !== strpos($element, $parent);
|
||||
}));
|
||||
}
|
||||
@@ -156,10 +153,8 @@ class I18nData {
|
||||
* Check if the key is an only child.
|
||||
* To be an only child, there must be only one sibling and that sibling must
|
||||
* be the empty sibling. The empty sibling is the parent.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isOnlyChild($key) {
|
||||
private function isOnlyChild(string $key): bool {
|
||||
$siblings = $this->getSiblings($key);
|
||||
|
||||
if (1 !== count($siblings)) {
|
||||
@@ -173,7 +168,7 @@ class I18nData {
|
||||
* When a key has children, it cannot have its value directly. The value
|
||||
* needs to be attached to an empty sibling represented by "_".
|
||||
*/
|
||||
private function getEmptySibling($key) {
|
||||
private function getEmptySibling(string $key): string {
|
||||
return "{$key}._";
|
||||
}
|
||||
|
||||
@@ -182,7 +177,7 @@ class I18nData {
|
||||
* To be a parent key, there must be at least one key starting with the key
|
||||
* under test. Of course, it cannot be itself.
|
||||
*/
|
||||
private function isParent($key) {
|
||||
private function isParent(string $key): bool {
|
||||
if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) {
|
||||
return false;
|
||||
}
|
||||
@@ -200,12 +195,9 @@ class I18nData {
|
||||
|
||||
/**
|
||||
* Add a new key to all languages.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addKey($key, $value) {
|
||||
public function addKey(string $key, string $value): void {
|
||||
if ($this->isParent($key)) {
|
||||
$key = $this->getEmptySibling($key);
|
||||
}
|
||||
@@ -241,12 +233,9 @@ class I18nData {
|
||||
/**
|
||||
* Add a value for a key for the selected language.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param string $language
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addValue($key, $value, $language) {
|
||||
public function addValue(string $key, string $value, string $language): void {
|
||||
if (!in_array($language, $this->getAvailableLanguages())) {
|
||||
throw new Exception('The selected language does not exist.');
|
||||
}
|
||||
@@ -271,11 +260,8 @@ class I18nData {
|
||||
|
||||
/**
|
||||
* Remove a key in all languages
|
||||
*
|
||||
* @param string $key
|
||||
* @throws Exception
|
||||
*/
|
||||
public function removeKey($key) {
|
||||
public function removeKey(string $key): void {
|
||||
if (!$this->isKnown($key) && !$this->isKnown($this->getEmptySibling($key))) {
|
||||
throw new Exception('The selected key does not exist.');
|
||||
}
|
||||
@@ -302,12 +288,8 @@ class I18nData {
|
||||
|
||||
/**
|
||||
* Ignore a key from a language, or reverse it.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $language
|
||||
* @param boolean $reverse
|
||||
*/
|
||||
public function ignore($key, $language, $reverse = false) {
|
||||
public function ignore(string $key, string $language, bool $reverse = false): void {
|
||||
$value = $this->data[$language][$this->getFilenamePrefix($key)][$key];
|
||||
if ($reverse) {
|
||||
$value->markAsIgnore();
|
||||
@@ -318,16 +300,13 @@ class I18nData {
|
||||
|
||||
/**
|
||||
* Ignore all unmodified keys from a language, or reverse it.
|
||||
*
|
||||
* @param string $language
|
||||
* @param boolean $reverse
|
||||
*/
|
||||
public function ignore_unmodified($language, $reverse = false) {
|
||||
public function ignore_unmodified(string $language, bool $reverse = false): void {
|
||||
$my_language = $this->getLanguage($language);
|
||||
foreach ($this->getReferenceLanguage() as $file => $ref_language) {
|
||||
foreach ($ref_language as $key => $ref_value) {
|
||||
if (array_key_exists($key, $my_language[$file])) {
|
||||
if($ref_value->equal($my_language[$file][$key])) {
|
||||
if ($ref_value->equal($my_language[$file][$key])) {
|
||||
$this->ignore($key, $language, $reverse);
|
||||
}
|
||||
}
|
||||
@@ -335,19 +314,21 @@ class I18nData {
|
||||
}
|
||||
}
|
||||
|
||||
public function getLanguage($language) {
|
||||
/**
|
||||
* @return array<string,array<string,I18nValue>>
|
||||
*/
|
||||
public function getLanguage(string $language): array {
|
||||
return $this->data[$language];
|
||||
}
|
||||
|
||||
public function getReferenceLanguage() {
|
||||
/**
|
||||
* @return array<string,array<string,I18nValue>>
|
||||
*/
|
||||
public function getReferenceLanguage(): array {
|
||||
return $this->getLanguage(static::REFERENCE_LANGUAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
private function getFilenamePrefix($key) {
|
||||
private function getFilenamePrefix(string $key): string {
|
||||
return preg_replace('/\..*/', '.php', $key);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
require_once __DIR__ . '/I18nValue.php';
|
||||
|
||||
class I18nFile {
|
||||
public function load() {
|
||||
/**
|
||||
* @return array<string,array<string,string|array<string,I18nValue>>>
|
||||
*/
|
||||
public function load(): array {
|
||||
$i18n = array();
|
||||
$dirs = new DirectoryIterator(I18N_PATH);
|
||||
foreach ($dirs as $dir) {
|
||||
@@ -23,7 +26,10 @@ class I18nFile {
|
||||
return $i18n;
|
||||
}
|
||||
|
||||
public function dump(array $i18n) {
|
||||
/**
|
||||
* @param array<string,array<array<string>>> $i18n
|
||||
*/
|
||||
public function dump(array $i18n): void {
|
||||
foreach ($i18n as $language => $file) {
|
||||
$dir = I18N_PATH . DIRECTORY_SEPARATOR . $language;
|
||||
if (!file_exists($dir)) {
|
||||
@@ -38,13 +44,11 @@ class I18nFile {
|
||||
|
||||
/**
|
||||
* Process the content of an i18n file
|
||||
*
|
||||
* @param string $filename
|
||||
* @return array
|
||||
* @return array<string,array<string,I18nValue>>
|
||||
*/
|
||||
private function process(string $filename) {
|
||||
$content = file_get_contents($filename);
|
||||
$content = str_replace('<?php', '', $content);
|
||||
private function process(string $filename): array {
|
||||
$fileContent = file_get_contents($filename) ?: [];
|
||||
$content = str_replace('<?php', '', $fileContent);
|
||||
|
||||
$content = preg_replace([
|
||||
"#',\s*//\s*TODO.*#i",
|
||||
@@ -76,11 +80,11 @@ class I18nFile {
|
||||
/**
|
||||
* Flatten an array of translation
|
||||
*
|
||||
* @param array $translation
|
||||
* @param array<string,I18nValue|array<string,I18nValue>> $translation
|
||||
* @param string $prefix
|
||||
* @return array
|
||||
* @return array<string,I18nValue>
|
||||
*/
|
||||
private function flatten(array $translation, string $prefix = '') {
|
||||
private function flatten(array $translation, string $prefix = ''): array {
|
||||
$a = array();
|
||||
|
||||
if ('' !== $prefix) {
|
||||
@@ -104,10 +108,10 @@ class I18nFile {
|
||||
* The first key is dropped since it represents the filename and we have
|
||||
* no use of it.
|
||||
*
|
||||
* @param array $translation
|
||||
* @return array
|
||||
* @param array<string> $translation
|
||||
* @return array<string,array<string,I18nValue>>
|
||||
*/
|
||||
private function unflatten(array $translation) {
|
||||
private function unflatten(array $translation): array {
|
||||
$a = array();
|
||||
|
||||
ksort($translation, SORT_NATURAL);
|
||||
@@ -127,10 +131,9 @@ class I18nFile {
|
||||
* translation file. The array is first converted to a string then some
|
||||
* formatting regexes are applied to match the original content.
|
||||
*
|
||||
* @param array $translation
|
||||
* @return string
|
||||
* @param array<string> $translation
|
||||
*/
|
||||
private function format(array $translation) {
|
||||
private function format(array $translation): string {
|
||||
$translation = var_export($this->unflatten($translation), true);
|
||||
$patterns = array(
|
||||
'/ -> todo\',/',
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
<?php
|
||||
|
||||
class I18nValue {
|
||||
const STATE_DIRTY = 'dirty';
|
||||
const STATE_IGNORE = 'ignore';
|
||||
const STATE_TODO = 'todo';
|
||||
const STATES = [
|
||||
private const STATE_DIRTY = 'dirty';
|
||||
public const STATE_IGNORE = 'ignore';
|
||||
private const STATE_TODO = 'todo';
|
||||
private const STATES = [
|
||||
self::STATE_DIRTY,
|
||||
self::STATE_IGNORE,
|
||||
self::STATE_TODO,
|
||||
];
|
||||
|
||||
/** @var string */
|
||||
private $value;
|
||||
/** @var string|null */
|
||||
private $state;
|
||||
|
||||
public function __construct(string $data) {
|
||||
@@ -31,37 +33,37 @@ class I18nValue {
|
||||
$this->markAsTodo();
|
||||
}
|
||||
|
||||
public function equal(I18nValue $value) {
|
||||
public function equal(I18nValue $value): bool {
|
||||
return $this->value === $value->getValue();
|
||||
}
|
||||
|
||||
public function isIgnore() {
|
||||
public function isIgnore(): bool {
|
||||
return $this->state === self::STATE_IGNORE;
|
||||
}
|
||||
|
||||
public function isTodo() {
|
||||
public function isTodo(): bool {
|
||||
return $this->state === self::STATE_TODO;
|
||||
}
|
||||
|
||||
public function markAsDirty() {
|
||||
public function markAsDirty(): void {
|
||||
$this->state = self::STATE_DIRTY;
|
||||
}
|
||||
|
||||
public function markAsIgnore() {
|
||||
public function markAsIgnore(): void {
|
||||
$this->state = self::STATE_IGNORE;
|
||||
}
|
||||
|
||||
public function markAsTodo() {
|
||||
public function markAsTodo(): void {
|
||||
$this->state = self::STATE_TODO;
|
||||
}
|
||||
|
||||
public function unmarkAsIgnore() {
|
||||
public function unmarkAsIgnore(): void {
|
||||
if ($this->state === self::STATE_IGNORE) {
|
||||
$this->state = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
public function __toString(): string {
|
||||
if ($this->state === null) {
|
||||
return $this->value;
|
||||
}
|
||||
@@ -69,7 +71,7 @@ class I18nValue {
|
||||
return "{$this->value} -> {$this->state}";
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
public function getValue(): string {
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,4 @@
|
||||
./app/Models/FeedDAO.php
|
||||
./app/Models/TagDAO.php
|
||||
./app/Services/ImportService.php
|
||||
./cli/i18n/I18nData.php
|
||||
./cli/i18n/I18nFile.php
|
||||
./cli/i18n/I18nValue.php
|
||||
./lib/Minz/Paginator.php
|
||||
|
||||
Reference in New Issue
Block a user