From 06bff0b9b84d47c43f9bc8aff06a29d85ebb7ed0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 14 Nov 2023 14:47:55 +0100 Subject: Add function param/return types, fix a lot more phpstorm complaints --- modules-available/translation/page.inc.php | 135 +++++++++++++---------------- 1 file changed, 62 insertions(+), 73 deletions(-) (limited to 'modules-available/translation/page.inc.php') diff --git a/modules-available/translation/page.inc.php b/modules-available/translation/page.inc.php index da4205d2..578e27dc 100644 --- a/modules-available/translation/page.inc.php +++ b/modules-available/translation/page.inc.php @@ -1,5 +1,7 @@ builtInSections = array('template', 'messages', 'module', 'menucategory', 'custom'); } - private function isValidSection($section) + private function isValidSection(string $section): bool { return in_array($section, $this->builtInSections); } - private function loadCustomHandler($moduleName) + private function loadCustomHandler(string $moduleName): ?array { $path = 'modules/' . $moduleName . '/hooks/translation.inc.php'; $HANDLER = array(); @@ -75,7 +77,7 @@ class Page_Translation extends Page } } if (empty($backup)) - return false; + return null; return $backup; } @@ -83,7 +85,8 @@ class Page_Translation extends Page * Redirect to the closest matching page, as extracted from * get/post parameters. */ - private function redirect($level = 99) + #[NoReturn] + private function redirect(int $level = 99): void { $params = array('do' => 'translation'); if ($level > 0 && $this->module !== false) { @@ -329,9 +332,9 @@ class Page_Translation extends Page Render::addTemplate('menu-category-list', $data); } - private function showModuleCustom() + private function showModuleCustom(): void { - if ($this->customHandler === false) + if ($this->customHandler === null) return; foreach ($this->customHandler['subsections'] as $subsection) { $this->showModuleCustomSubsection($subsection); @@ -420,11 +423,11 @@ class Page_Translation extends Page /** * Get all tags used by templates of the given module. - * @param \Module $module module in question, false to use the one being edited. + * @param \Module $module module in question, null to use the one being edited. * * @return array of array(tag => array of templates using that tag) */ - private function loadUsedTemplateTags($module = false) + private function loadUsedTemplateTags(Module $module = null): array { if ($module === false) { $module = $this->module; @@ -451,10 +454,10 @@ class Page_Translation extends Page * 'files' => array(filename => occurencecount, ...) * ) * - * @param \Module $module module in question, false to use the one being edited + * @param \Module $module module in question, null to use the one being edited * @return array see above */ - private function loadUsedMessageTags($module = false) + private function loadUsedMessageTags(Module $module = null): array { if ($module === false) { $module = $this->module; @@ -484,10 +487,9 @@ class Page_Translation extends Page /** * Get all module tags used/required. * - * @param string $module - * @return array of array(tagname => (bool)required) + * @return array ( => (bool)required) */ - private function loadUsedModuleTags($module = false) + private function loadUsedModuleTags(Module $module = null): array { if ($module === false) { $module = $this->module; @@ -499,7 +501,7 @@ class Page_Translation extends Page } unset($tag); // Fixup special tags - if ($module->getCategory() === false) { + if ($module->getCategory() === null) { unset($tags['module_name']); unset($tags['page_title']); } else { @@ -509,7 +511,7 @@ class Page_Translation extends Page return $tags; } - private function loadUsedMenuCategories() + private function loadUsedMenuCategories(): array { $module = $this->module; $skip = strlen($module->getIdentifier()) + 1; @@ -517,7 +519,7 @@ class Page_Translation extends Page $want = array(); foreach (Module::getAll() as $module) { $cat = $module->getCategory(); - if (is_string($cat) && substr($cat, 0, $skip) === $match) { + if (substr($cat, 0, $skip) === $match) { $want[substr($cat, $skip)] = true; } } @@ -533,14 +535,14 @@ class Page_Translation extends Page * @param string $subsection Name of subsection * @return array|false List of tags as KEYS of array */ - private function loadUsedCustomTags($subsection) + private function loadUsedCustomTags(string $subsection) { if (!isset($this->customHandler['grep_'.$subsection])) return false; return $this->customHandler['grep_'.$subsection]($this->module); } - private function getTagsFromTemplate($templateFile) + private function getTagsFromTemplate(string $templateFile) { //checks if the template is valid if (!file_exists($templateFile)) { @@ -561,10 +563,10 @@ class Page_Translation extends Page * * @param string $lang lang to use * @param array $tags Array of tags, where the tag names are the keys - * @param \Module|false $module the module to work with, defaults to the currently edited module + * @param Module $module the module to work with, defaults to the currently edited module * @return array [missingCount, unusedCount] */ - private function getModuleTemplateStatus($lang, $tags = false, $module = false) + private function getModuleTemplateStatus(string $lang, array $tags, Module $module = null): array { return $this->getModuleTranslationStatus($lang, 'template-tags', true, $tags, $module); } @@ -576,12 +578,12 @@ class Page_Translation extends Page * * @param string $lang lang cc to use * @param string $file the name of the translation file to load for checking - * @param boolean $fallback whether to check the global-tags of the main module as fallback + * @param bool $fallback whether to check the global-tags of the main module as fallback * @param array $tags list of tags that are expected to exist. Tags are the array keys! - * @param \Module|false $module the module to work with, defaults to the currently edited module + * @param Module $module the module to work with, defaults to the currently edited module * @return array [missingCount, unusedCount] */ - private function getModuleTranslationStatus($lang, $file, $fallback, $tags, $module = false) + private function getModuleTranslationStatus(string $lang, string $file, bool $fallback, array $tags, Module $module = null): array { if ($module === false) { $module = $this->module; @@ -616,7 +618,7 @@ class Page_Translation extends Page return array($missing, $unused); } - private function checkModuleTranslation($module) + private function checkModuleTranslation(Module $module): string { $templateTags = $this->loadUsedTemplateTags($module); $messageTags = $this->loadUsedMessageTags($module); @@ -651,7 +653,7 @@ class Page_Translation extends Page * * @return array of all php file names */ - private function getAllFiles($dir, $extension) + private function getAllFiles(string $dir, string $extension): array { $php = array(); $extLen = -strlen($extension); @@ -671,10 +673,10 @@ class Page_Translation extends Page /** * Finds and returns all PHP files of current module. * - * @param \Module $module Module to get the php files of + * @param Module $module Module to get the php files of * @return array of php file names */ - private function getModulePhpFiles($module) + private function getModulePhpFiles(Module $module): array { return $this->getAllFiles('modules/' . $module->getIdentifier(), '.php'); } @@ -682,10 +684,9 @@ class Page_Translation extends Page /** * Get array to pass to edit page with all the tags and translations. * - * @param string $path the template's path * @return array structure to pass to the tags list in the edit template */ - private function loadTemplateEditArray() + private function loadTemplateEditArray(): array { $tags = $this->loadUsedTemplateTags(); $table = $this->buildTranslationTable('template-tags', array_keys($tags), true); @@ -704,10 +705,9 @@ class Page_Translation extends Page /** * Get array to pass to edit page with all the message ids. * - * @param string $path the template's path * @return array structure to pass to the tags list in the edit template */ - private function loadMessagesEditArray() + private function loadMessagesEditArray(): array { $tags = $this->loadUsedMessageTags(); $table = $this->buildTranslationTable('messages', array_keys($tags), true); @@ -731,16 +731,15 @@ class Page_Translation extends Page /** * Get array to pass to edit page with all the message ids. * - * @param string $path the template's path * @return array structure to pass to the tags list in the edit template */ - private function loadModuleEditArray() + private function loadModuleEditArray(): array { $tags = $this->loadUsedModuleTags(); return $this->buildTranslationTable('module', array_keys($tags), true); } - private function loadMenuCategoryEditArray() + private function loadMenuCategoryEditArray(): array { $tags = $this->loadUsedMenuCategories(); return $this->buildTranslationTable('categories', array_keys($tags), true); @@ -749,10 +748,9 @@ class Page_Translation extends Page /** * Get array to pass to edit page with all the message ids. * - * @param string $path the template's path * @return array structure to pass to the tags list in the edit template */ - private function loadCustomEditArray() + private function loadCustomEditArray(): array { $tags = $this->loadUsedCustomTags($this->subsection); return $this->buildTranslationTable($this->subsection, array_keys($tags), true); @@ -767,7 +765,7 @@ class Page_Translation extends Page * @param string $str the partial method call * @return int number of arguments to the method, minus the message id */ - private function countMessageParams($str) + private function countMessageParams(string $str): int { $quote = false; $escape = false; @@ -826,11 +824,11 @@ class Page_Translation extends Page * )). * * @param string $regexp regular expression - * @param array $files list of files to scan - * @param array $tags existing tag array to append to + * @param string[] $files list of files to scan + * @param string[] $tags existing tag array to append to * @return array of all tags found, where the tag is the key, and the value is as described above */ - private function loadTagsFromPhp($regexp, $files, $tags = []) + private function loadTagsFromPhp(string $regexp, array $files, array $tags = []): array { // Get all php files, so we can find all strings that need to be translated // Now find all tags in all php files. Only works for literal usage, not something like $foo = 'bar'; Dictionary::translate($foo); @@ -855,32 +853,28 @@ class Page_Translation extends Page /** * @param string $file Source dictionary - * @param string[]|false $requiredTags Tags that are considered required + * @param string[] $requiredTags Tags that are considered required * @param bool $findAlreadyTranslated If true, try to find a translation for this string in another language * @return array numeric array suitable for passing to mustache */ - private function buildTranslationTable($file, $requiredTags = false, $findAlreadyTranslated = false) + private function buildTranslationTable(string $file, array $requiredTags, bool $findAlreadyTranslated = false): array { $tags = array(); - if (is_array($requiredTags)) { - foreach ($requiredTags as $tagName) { - $tags[$tagName] = array('tag' => $tagName, 'required' => true); - } + foreach ($requiredTags as $tagName) { + $tags[$tagName] = array('tag' => $tagName, 'required' => true); } // Sort here, so all tags known to be used are in alphabetical order ksort($tags); // Finds every tag within the JSON language file $jsonTags = Dictionary::getArray($this->module->getIdentifier(), $file, $this->destLang); - if (is_array($jsonTags)) { - // Sort these separately so unused tags will be at the bottom of the list, but still ordered alphabetically - ksort($jsonTags); - foreach ($jsonTags as $tag => $translation) { - $tags[$tag]['translation'] = $translation; - if (strpos($translation, "\n") !== false) { - $tags[$tag]['big'] = true; - } - $tags[$tag]['tag'] = $tag; + // Sort these separately so unused tags will be at the bottom of the list, but still ordered alphabetically + ksort($jsonTags); + foreach ($jsonTags as $tag => $translation) { + $tags[$tag]['translation'] = $translation; + if (strpos($translation, "\n") !== false) { + $tags[$tag]['big'] = true; } + $tags[$tag]['tag'] = $tag; } if ($findAlreadyTranslated) { // For each tag, include a translated string from another language as reference @@ -894,17 +888,15 @@ class Page_Translation extends Page $tagid = 0; foreach ($tags as &$tag) { $tag['tagid'] = $tagid++; - if ($requiredTags !== false) { - // We have a list of required tags, so mark those that are missing or unused - if (!isset($tag['required'])) { - $tag['unused'] = true; - } elseif (!isset($tag['translation']) && !isset($globals[$tag['tag']])) { - $tag['missing'] = true; - } - if (isset($globals[$tag['tag']])) { - $tag['isglobal'] = true; - $tag['placeholder'] = $globals[$tag['tag']]; - } + // We have a list of required tags, so mark those that are missing or unused + if (!isset($tag['required'])) { + $tag['unused'] = true; + } elseif (!isset($tag['translation']) && !isset($globals[$tag['tag']])) { + $tag['missing'] = true; + } + if (isset($globals[$tag['tag']])) { + $tag['isglobal'] = true; + $tag['placeholder'] = $globals[$tag['tag']]; } } // Finally remove tagname from the keys so mustache will iterate over them via {{#..}} @@ -919,7 +911,7 @@ class Page_Translation extends Page * @param string $file translation unit * @param array $tags list of tags, formatted as used in buildTranslationTable() */ - private function findTranslationSamples($file, &$tags) + private function findTranslationSamples(string $file, array &$tags): void { $srcLangs = array_unique(array_merge(array(LANG), array('en'), Dictionary::getLanguages())); if (($key = array_search($this->destLang, $srcLangs)) !== false) { @@ -927,8 +919,6 @@ class Page_Translation extends Page } foreach ($srcLangs as $lang) { $otherLang = Dictionary::getArray($this->module->getIdentifier(), $file, $lang); - if (!is_array($otherLang)) - continue; $missing = false; foreach (array_keys($tags) as $tag) { if (isset($tags[$tag]['samplelang'])) @@ -945,7 +935,7 @@ class Page_Translation extends Page } } - private function getJsonFile() + private function getJsonFile(): string { $prefix = 'modules/' . $this->module->getIdentifier() . '/lang/' . $this->destLang; // File @@ -963,7 +953,7 @@ class Page_Translation extends Page } // Custom submodule if ($this->section === 'custom') { - if ($this->customHandler === false || !isset($this->customHandler['subsections'])) { + if ($this->customHandler === null || !isset($this->customHandler['subsections'])) { Message::addError('no-custom-handlers'); $this->redirect(1); } @@ -975,13 +965,12 @@ class Page_Translation extends Page } Message::addError('invalid-section', $this->section); $this->redirect(1); - return false; } /** * Updates a JSON file with it's new tags or/and tags values */ - private function updateJson() + private function updateJson(): void { $this->ensureValidDestLanguage(); if ($this->module === false) { -- cgit v1.2.3-55-g7522